-
Notifications
You must be signed in to change notification settings - Fork 0
/
elements.go
56 lines (50 loc) · 1.27 KB
/
elements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package goetf
type binaryElement struct {
// tag type identifier
tag ExternalTagType
// if the element is array, slice or map
complex bool
// body hold the data of the type
body []byte
// items hold the elements for an array or slice
items []*binaryElement
// dict hold the pairs for a map
dict []*binaryElement
}
func newBinaryElement(tag ExternalTagType, body []byte) *binaryElement {
return &binaryElement{
tag: tag,
body: body,
complex: isComplexType(tag),
items: make([]*binaryElement, 0),
dict: make([]*binaryElement, 0),
}
}
func (be *binaryElement) append(tag ExternalTagType, elem *binaryElement) {
switch tag {
case EttList, EttSmallTuple, EttLargeTuple:
be.items = append(be.items, elem)
case EttMap:
be.dict = append(be.dict, elem)
default:
(*be) = *elem
}
}
func (be *binaryElement) put(tag ExternalTagType, data []byte) {
elem := newBinaryElement(tag, data)
switch tag {
case EttList, EttSmallTuple, EttLargeTuple:
be.items = append(be.items, elem)
case EttMap:
be.dict = append(be.dict, elem)
default:
be.body = data
}
}
func isComplexType(tag ExternalTagType) bool {
switch tag {
case EttMap, EttList, EttLargeTuple, EttSmallTuple:
return true
}
return false
}