-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_object_parser.go
155 lines (130 loc) · 3.54 KB
/
yaml_object_parser.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package yop
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
var maxDepthLimit = 2
var (
errObject = errors.New("parameter")
ErrObjectNotFound = fmt.Errorf("%w not found", errObject)
ErrObjectNotAcceptable = fmt.Errorf("%w not acceptable", errObject)
ErrObjectUndefined = fmt.Errorf("%w undefined", errObject)
ErrMaxDepthLimitExceeded = fmt.Errorf("%w max depth limit (%d) exceeded", errObject, maxDepthLimit)
)
type Object struct {
Name string
Level int
Children []*Object
Arguments string
}
var validObjectNames = []string{}
func SetMaxDepthLimit(limit int) {
maxDepthLimit = limit
}
func AddObjectName(names ...string) {
if len(names) > 0 {
validObjectNames = append(validObjectNames, names...)
}
}
func RemoveObjectName(name string) {
result := []string{}
for _, objectName := range validObjectNames {
if !strings.EqualFold(objectName, name) {
result = append(result, objectName)
}
}
validObjectNames = result
}
func ParseObjectFromJSON(jsonObjectSection []byte) (*Object, error) {
if err := isObjectValid(jsonObjectSection); err != nil {
return nil, err
}
obj := &Object{}
if err := parseObject(obj, jsonObjectSection); err != nil {
return nil, err
}
return obj, nil
}
func isObjectValid(b []byte) error {
if string(b) == "\"\"" {
return fmt.Errorf("%w: object has empty string", ErrObjectUndefined)
}
if string(b) == "null" {
return fmt.Errorf("%w: object is null object", ErrObjectUndefined)
}
if string(b[0]) == "[" {
return fmt.Errorf("%w: %s, please choose one", ErrObjectNotAcceptable, b)
}
return nil
}
func parseObject(parent *Object, b []byte) (err error) {
if parent.Level > maxDepthLimit {
return ErrMaxDepthLimitExceeded
}
err = parseObjectFromString(parent, b)
if err == nil || errors.Is(err, errObject) {
return err
}
err = parseObjectWithChildren(parent, b)
if err == nil || errors.Is(err, errObject) {
return err
}
return parseObjectWithArguments(parent, b)
}
func parseObjectFromString(parent *Object, b []byte) error {
var objectName string
if err := json.Unmarshal(b, &objectName); err != nil {
return err // ignore error, json: cannot unmarshal object into Go value of type
}
if err := isSupported(objectName); err != nil {
return err
}
parent.Name = objectName
return nil
}
func parseObjectWithChildren(parent *Object, b []byte) error {
var listObject map[string][]json.RawMessage
if err := json.Unmarshal(b, &listObject); err != nil {
return err // ignore error, json: cannot unmarshal object into Go value of type
}
for objectName, children := range listObject {
if err := isSupported(objectName); err != nil {
return err
}
parent.Name = objectName
for _, child := range children {
childObject := &Object{
Level: parent.Level + 1,
}
if err := parseObject(childObject, child); err != nil {
return err
}
parent.Children = append(parent.Children, childObject)
}
}
return nil
}
func parseObjectWithArguments(parent *Object, b []byte) error {
var objectObject map[string]json.RawMessage
if err := json.Unmarshal(b, &objectObject); err != nil {
return err // ignore error, json: cannot unmarshal object into Go value of type
}
for objectName, arguments := range objectObject {
if err := isSupported(objectName); err != nil {
return err
}
parent.Name = objectName
parent.Arguments = string(arguments)
}
return nil
}
func isSupported(key string) error {
for _, name := range validObjectNames {
if key == name {
return nil
}
}
return fmt.Errorf("'%s' %w", key, ErrObjectNotFound)
}