-
Notifications
You must be signed in to change notification settings - Fork 43
/
result-formater.go
71 lines (68 loc) · 1.63 KB
/
result-formater.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package stream_parser
import (
"errors"
"github.com/yaklang/yaklang/common/bin-parser/parser/base"
)
var formatters = map[string]func(node *base.Node) (*base.NodeValue, error){}
func init() {
formatters["default"] = ToMap
}
func ToMap(node *base.Node) (*base.NodeValue, error) {
isPackage := func(node *base.Node) bool {
if node.Name == "Package" && node.Cfg.GetItem(CfgParent) == node.Ctx.GetItem("root") {
return true
}
return false
}
if NodeHasResult(node) {
return newNodeValue(node, GetResultByNode(node)), nil
}
if node.Cfg.GetBool(CfgIsList) {
res := newListNodeValue(node)
for _, sub := range node.Children {
d, err := sub.Result()
if err != nil {
if errors.Is(err, noResultError) {
continue
}
return nil, err
}
res.AppendSub(d)
}
if len(res.Children()) == 0 {
return nil, noResultError
}
return res, nil
} else {
res := newStructNodeValue(node)
//res := map[string]any{}
var getSubs func(node *base.Node) []*base.Node
getSubs = func(node *base.Node) []*base.Node {
children := []*base.Node{}
for _, sub := range node.Children {
if sub.Cfg.GetBool(CfgIsRefType) || sub.Cfg.GetBool("unpack") || isPackage(sub) {
children = append(children, getSubs(sub)...)
} else {
children = append(children, sub)
}
}
return children
}
children := getSubs(node)
for _, sub := range children {
d, err := sub.Result()
if err != nil {
if errors.Is(err, noResultError) {
continue
}
return nil, err
}
res.AppendSub(d)
//res[sub.Name] = d
}
if len(res.Children()) == 0 {
return nil, noResultError
}
return res, nil
}
}