/
body.go
162 lines (140 loc) · 3.86 KB
/
body.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
156
157
158
159
160
161
162
package v1alpha1
import "fmt"
//////////////////// BodyProfile ////////////////
// Exposes ValueProfile interface
type BodyProfile struct {
Unstructured *SimpleValProfile `json:"unstructured"`
Structured *StructuredProfile `json:"structured"`
}
func (profile *BodyProfile) profileI(args ...interface{}) {
switch v := args[0].(type) {
case string:
profile.ProfileUnstructured(v)
default:
profile.ProfileStructured(v)
}
}
func (profile *BodyProfile) ProfileUnstructured(str string) {
profile.Structured = nil
profile.Unstructured = new(SimpleValProfile)
profile.Unstructured.Profile(str)
}
func (profile *BodyProfile) ProfileStructured(data interface{}) {
profile.Unstructured = nil
profile.Structured = new(StructuredProfile)
profile.Structured.Profile(data)
}
//////////////////// BodyPile ////////////////
// Exposes ValuePile interface
type BodyPile struct {
Unstructured *SimpleValPile `json:"unstructured"`
Structured *StructuredPile `json:"structured"`
}
func (pile *BodyPile) addI(valProfile ValueProfile) {
pile.Add(valProfile.(*BodyProfile))
}
func (pile *BodyPile) Add(profile *BodyProfile) {
if profile.Structured != nil {
if pile.Structured == nil {
pile.Structured = new(StructuredPile)
}
pile.Structured.Add(profile.Structured)
}
if profile.Unstructured != nil {
if pile.Unstructured == nil {
pile.Unstructured = new(SimpleValPile)
}
pile.Unstructured.Add(profile.Unstructured)
}
}
func (pile *BodyPile) Clear() {
pile.Structured = nil
pile.Unstructured = nil
}
func (pile *BodyPile) mergeI(otherValPile ValuePile) {
pile.Merge(otherValPile.(*BodyPile))
}
func (pile *BodyPile) Merge(otherPile *BodyPile) {
if otherPile.Structured != nil {
if pile.Structured == nil {
pile.Structured = otherPile.Structured
} else {
pile.Structured.Merge(otherPile.Structured)
}
}
if otherPile.Unstructured != nil {
if pile.Unstructured == nil {
pile.Unstructured = otherPile.Unstructured
} else {
pile.Unstructured.Merge(otherPile.Unstructured)
}
}
}
//////////////////// BodyConfig ////////////////
// Exposes ValueConfig interface
type BodyConfig struct {
Unstructured *SimpleValConfig `json:"unstructured"`
Structured *StructuredConfig `json:"structured"`
}
func (config *BodyConfig) decideI(valProfile ValueProfile) string {
return config.Decide(valProfile.(*BodyProfile))
}
func (config *BodyConfig) Decide(profile *BodyProfile) string {
if profile.Structured != nil {
if config.Structured != nil {
str := config.Structured.Decide(profile.Structured)
if str != "" {
return fmt.Sprintf("Body %s", str)
}
} else {
return "Structured Body not allowed"
}
}
if profile.Unstructured != nil {
if config.Unstructured != nil {
str := config.Unstructured.Decide(profile.Unstructured)
if str != "" {
return fmt.Sprintf("Body %s", str)
}
} else {
return "Unstructured Body not allowed"
}
}
return ""
}
func (config *BodyConfig) learnI(valPile ValuePile) {
config.Learn(valPile.(*BodyPile))
}
func (config *BodyConfig) Learn(pile *BodyPile) {
if pile.Structured != nil {
if config.Structured == nil {
config.Structured = new(StructuredConfig)
}
config.Structured.Learn(pile.Structured)
}
if pile.Unstructured != nil {
if config.Unstructured == nil {
config.Unstructured = new(SimpleValConfig)
}
config.Unstructured.Learn(pile.Unstructured)
}
}
func (config *BodyConfig) fuseI(otherValConfig ValueConfig) {
config.Fuse(otherValConfig.(*BodyConfig))
}
func (config *BodyConfig) Fuse(otherConfig *BodyConfig) {
if otherConfig.Structured != nil {
if config.Structured == nil {
config.Structured = otherConfig.Structured
} else {
config.Structured.Fuse(otherConfig.Structured)
}
}
if otherConfig.Unstructured != nil {
if config.Unstructured == nil {
config.Unstructured = otherConfig.Unstructured
} else {
config.Unstructured.Fuse(otherConfig.Unstructured)
}
}
}