-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathvalue-meta.go
174 lines (154 loc) · 4.94 KB
/
value-meta.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
163
164
165
166
167
168
169
170
171
172
173
174
package normal
import (
"github.com/tliron/puccini/tosca/parsing"
)
//
// ValueMeta
//
type ValueMeta struct {
// Element, Key+Value, and Fields cannot be set together
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Element *ValueMeta `json:"element,omitempty" yaml:"element,omitempty"`
Key *ValueMeta `json:"key,omitempty" yaml:"key,omitempty"`
Value *ValueMeta `json:"value,omitempty" yaml:"value,omitempty"`
Fields ValueMetaMap `json:"fields,omitempty" yaml:"fields,omitempty"`
Validators FunctionCalls `json:"validators,omitempty" yaml:"validators,omitempty"`
Converter *FunctionCall `json:"converter,omitempty" yaml:"converter,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
TypeDescription string `json:"typeDescription,omitempty" yaml:"typeDescription,omitempty"`
LocalDescription string `json:"localDescription,omitempty" yaml:"localDescription,omitempty"`
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
TypeMetadata map[string]string `json:"typeMetadata,omitempty" yaml:"typeMetadata,omitempty"`
LocalMetadata map[string]string `json:"localMetadata,omitempty" yaml:"localMetadata,omitempty"`
}
func NewValueMeta() *ValueMeta {
return &ValueMeta{
Fields: make(ValueMetaMap),
Metadata: make(map[string]string),
TypeMetadata: make(map[string]string),
LocalMetadata: make(map[string]string),
}
}
func (self *ValueMeta) AddValidator(validator *parsing.FunctionCall) {
self.Validators = append(self.Validators, NewFunctionCall(validator))
}
func (self *ValueMeta) SetConverter(converter *parsing.FunctionCall) {
self.Converter = NewFunctionCall(converter)
}
func CopyValueMeta(valueMeta *ValueMeta) *ValueMeta {
if valueMeta != nil {
self := ValueMeta{
Type: valueMeta.Type,
Element: CopyValueMeta(valueMeta.Element),
Key: CopyValueMeta(valueMeta.Key),
Value: CopyValueMeta(valueMeta.Value),
Fields: CopyValueMetaMap(valueMeta.Fields),
Converter: valueMeta.Converter,
Description: valueMeta.Description,
TypeDescription: valueMeta.TypeDescription,
LocalDescription: valueMeta.LocalDescription,
Metadata: make(map[string]string),
TypeMetadata: make(map[string]string),
LocalMetadata: make(map[string]string),
}
self.Validators = append(valueMeta.Validators[:0:0], valueMeta.Validators...)
for key, value := range valueMeta.Metadata {
self.Metadata[key] = value
}
for key, value := range valueMeta.TypeMetadata {
self.TypeMetadata[key] = value
}
for key, value := range valueMeta.LocalMetadata {
self.LocalMetadata[key] = value
}
if !self.Empty() {
return &self
} else {
return nil
}
} else {
return nil
}
}
func (self *ValueMeta) Empty() bool {
return (self.Type == "") &&
((self.Element == nil) || self.Element.Empty()) &&
((self.Key == nil) || self.Key.Empty()) &&
((self.Value == nil) || self.Value.Empty()) &&
((self.Fields == nil) || self.Fields.Empty()) &&
((self.Validators == nil) || (len(self.Validators) == 0)) &&
(self.Converter == nil) &&
(self.Description == "") &&
(self.TypeDescription == "") &&
(self.LocalDescription == "") &&
((self.Metadata == nil) || (len(self.Metadata) == 0)) &&
((self.TypeMetadata == nil) || (len(self.TypeMetadata) == 0)) &&
((self.LocalMetadata == nil) || (len(self.LocalMetadata) == 0))
}
func (self *ValueMeta) Prune() {
if self.Element != nil {
if self.Element.Prune(); self.Element.Empty() {
self.Element = nil
}
}
if self.Key != nil {
if self.Key.Prune(); self.Key.Empty() {
self.Key = nil
}
}
if self.Value != nil {
if self.Value.Prune(); self.Value.Empty() {
self.Value = nil
}
}
if self.Fields != nil {
if self.Fields.Prune(); self.Fields.Empty() {
self.Fields = nil
}
}
if len(self.Validators) == 0 {
self.Validators = nil
}
if (self.Metadata != nil) && (len(self.Metadata) == 0) {
self.Metadata = nil
}
if (self.TypeMetadata != nil) && (len(self.TypeMetadata) == 0) {
self.TypeMetadata = nil
}
if (self.LocalMetadata != nil) && (len(self.LocalMetadata) == 0) {
self.LocalMetadata = nil
}
}
//
// ValueMetaMap
//
type ValueMetaMap map[string]*ValueMeta
func CopyValueMetaMap(valueMetaMap ValueMetaMap) ValueMetaMap {
if (valueMetaMap == nil) || (len(valueMetaMap) == 0) {
return nil
}
self := make(ValueMetaMap)
for key, valueMeta := range valueMetaMap {
self[key] = CopyValueMeta(valueMeta)
}
if !self.Empty() {
return self
} else {
return nil
}
}
func (self ValueMetaMap) Empty() bool {
for _, valueMeta := range self {
if !valueMeta.Empty() {
return false
}
}
return true
}
func (self ValueMetaMap) Prune() {
for key, valueMeta := range self {
if valueMeta.Prune(); valueMeta.Empty() {
delete(self, key)
}
}
}