forked from streamingfast/binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
39 lines (36 loc) · 801 Bytes
/
parse.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
package bin
import (
"encoding/binary"
"reflect"
"strings"
)
type fieldTag struct {
SizeOf []string
Skip bool
Order binary.ByteOrder
Optional bool
BinaryExtension bool
}
func parseFieldTag(tag reflect.StructTag) *fieldTag {
t := &fieldTag{
Order: binary.LittleEndian,
}
tagStr := tag.Get("bin")
for _, s := range strings.Split(tagStr, " ") {
if strings.HasPrefix(s, "sizeof=") {
_, tmp, _ := strings.Cut(s, "=")
t.SizeOf = strings.Split(tmp, ",")
} else if s == "big" {
t.Order = binary.BigEndian
} else if s == "little" {
t.Order = binary.LittleEndian
} else if s == "optional" {
t.Optional = true
} else if s == "binary_extension" {
t.BinaryExtension = true
} else if s == "-" {
t.Skip = true
}
}
return t
}