Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to decode array and map #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ n.GetNodes() // parses the value as TLV and returns a Nodes structure (or
n.GetUint8() // parses the value as uint8 (returns error if value is too small)
n.GetPaddedUint8() // parses the value as uint8 and pads it if too small

// all available types: bool, uint8, uint16, uint32, uint64, string, time.Time and Nodes
// all available types: bool, uint8, uint16, uint32, uint64, string, time.Time, Nodes and map[string]*Node
```

### Custom Decoder with different sizes and endianness
Expand Down
14 changes: 14 additions & 0 deletions tlv/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Decoder interface {
NewNode(tag Tag, value []byte) Node
// GetByteOrder returns the decoder endianness configuration.
GetByteOrder() binary.ByteOrder
// GetTagSize returns the decoder tag size configuration.
GetTagSize() uint8
// GetLengthSize returns the decoder length size configuration.
GetLengthSize() uint8
}

type decoder struct {
Expand Down Expand Up @@ -136,3 +140,13 @@ func (d *decoder) NewNode(tag Tag, value []byte) Node {
func (d *decoder) GetByteOrder() binary.ByteOrder {
return d.byteOrder
}

// GetTagSize returns the decoder tag size configuration.
func (d *decoder) GetTagSize() uint8 {
return d.tagSize
}

// GetLengthSize returns the decoder length size configuration.
func (d *decoder) GetLengthSize() uint8 {
return d.lengthSize
}
109 changes: 109 additions & 0 deletions tlv/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"time"

"github.com/pauloavelar/go-tlv/tlv/internal/errors"
"github.com/pauloavelar/go-tlv/tlv/internal/sizes"
"github.com/pauloavelar/go-tlv/tlv/internal/utils"
)
Expand Down Expand Up @@ -131,6 +132,114 @@ func (n *Node) GetPaddedUint64() uint64 {
return n.getByteOrder().Uint64(append(padding, n.Value...))
}

// GetVariantArray parses the value as an array. All nodes in the array have individual tag.
func (n *Node) GetVariantArray() (Nodes, error) {
lengthSize := uint64(n.decoder.GetLengthSize())
if len(n.Value) < int(lengthSize) {
return nil, errors.NewMessageTooShortError(n.Value)
}
arrayLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[:lengthSize])
nodes := make([]Node, 0, arrayLength)
offset := lengthSize

for i := uint64(0); i < arrayLength; i++ {
node, read, err := n.decoder.DecodeSingle(n.Value[offset:])
if err != nil {
return nil, err
}
offset += read
nodes = append(nodes, node)
}
return nodes, nil
}

// GetArray parses the value as an array. All nodes in the array have the same tag.
func (n *Node) GetArray() (Nodes, error) {
tagSize := uint64(n.decoder.GetTagSize())
lengthSize := uint64(n.decoder.GetLengthSize())
if len(n.Value) < int(lengthSize+tagSize) {
return nil, errors.NewMessageTooShortError(n.Value)
}
arrayTag := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[:tagSize])
arrayLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[tagSize:tagSize+lengthSize])
nodes := make([]Node, 0, arrayLength)
offset := tagSize + lengthSize

for i := uint64(0); i < arrayLength; i++ {
itemLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[offset:offset+lengthSize])
offset += lengthSize
nodes = append(nodes, Node{
Tag: Tag(arrayTag),
Length: Length(itemLength),
Value: n.Value[offset : offset+itemLength],
// All the array items use the same tag, so we do not provide raw bytes.
Raw: nil,
})
offset += itemLength
}
return nodes, nil
}

// GetStringMap parses the value as an key-value pair, the key type is string, and
// each value have individual tag.
func (n *Node) GetVariantStringMap() (map[string]*Node, error) {
lengthSize := uint64(n.decoder.GetLengthSize())
if len(n.Value) < int(lengthSize) {
return nil, errors.NewMessageTooShortError(n.Value)
}
mapLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[:lengthSize])
nodes := make(map[string]*Node, mapLength)
offset := lengthSize

for i := uint64(0); i < mapLength; i++ {
labelLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[offset:offset+lengthSize])
offset += lengthSize
labelString := string(n.Value[offset : offset+labelLength])
offset += labelLength

node, read, err := n.decoder.DecodeSingle(n.Value[offset:])
if err != nil {
return nil, err
}
offset += read
nodes[labelString] = &node
}
return nodes, nil
}

// GetStringMap parses the value as an key-value pair, the key type is string, and
// each value have the same tag.
func (n *Node) GetStringMap() (map[string]*Node, error) {
tagSize := uint64(n.decoder.GetTagSize())
lengthSize := uint64(n.decoder.GetLengthSize())
if len(n.Value) < int(lengthSize+tagSize) {
return nil, errors.NewMessageTooShortError(n.Value)
}
mapTag := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[:tagSize])
mapLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[tagSize:tagSize+lengthSize])
nodes := make(map[string]*Node, mapLength)
offset := tagSize + lengthSize

for i := uint64(0); i < mapLength; i++ {
labelLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[offset:offset+lengthSize])
offset += lengthSize
labelString := string(n.Value[offset : offset+labelLength])
offset += labelLength

itemLength := utils.GetPaddedUint64(n.decoder.GetByteOrder(), n.Value[offset:offset+lengthSize])
offset += lengthSize
nodes[labelString] = &Node{
Tag: Tag(mapTag),
Length: Length(itemLength),
Value: n.Value[offset : offset+itemLength],
// All the map items use the same tag, so we do not provide raw bytes.
Raw: nil,
}
offset += itemLength
}
return nodes, nil
}

func (n *Node) getSafeDecoder() Decoder {
if n.decoder != nil {
return n.decoder
Expand Down