-
Notifications
You must be signed in to change notification settings - Fork 20
/
constraint-clause.go
198 lines (170 loc) · 6.63 KB
/
constraint-clause.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package tosca_v2_0
import (
"github.com/tliron/kutil/ard"
"github.com/tliron/puccini/tosca"
"github.com/tliron/puccini/tosca/normal"
profile "github.com/tliron/puccini/tosca/profiles/implicit/v2_0"
"github.com/tliron/yamlkeys"
)
// Built-in constraint functions
var ConstraintClauseScriptlets = map[string]string{
"tosca.constraint.equal": profile.Profile["/tosca/implicit/2.0/js/constraints/equal.js"],
"tosca.constraint.greater_than": profile.Profile["/tosca/implicit/2.0/js/constraints/greater_than.js"],
"tosca.constraint.greater_or_equal": profile.Profile["/tosca/implicit/2.0/js/constraints/greater_or_equal.js"],
"tosca.constraint.less_than": profile.Profile["/tosca/implicit/2.0/js/constraints/less_than.js"],
"tosca.constraint.less_or_equal": profile.Profile["/tosca/implicit/2.0/js/constraints/less_or_equal.js"],
"tosca.constraint.in_range": profile.Profile["/tosca/implicit/2.0/js/constraints/in_range.js"],
"tosca.constraint.valid_values": profile.Profile["/tosca/implicit/2.0/js/constraints/valid_values.js"],
"tosca.constraint.length": profile.Profile["/tosca/implicit/2.0/js/constraints/length.js"],
"tosca.constraint.min_length": profile.Profile["/tosca/implicit/2.0/js/constraints/min_length.js"],
"tosca.constraint.max_length": profile.Profile["/tosca/implicit/2.0/js/constraints/max_length.js"],
"tosca.constraint.pattern": profile.Profile["/tosca/implicit/2.0/js/constraints/pattern.js"],
"tosca.constraint.schema": profile.Profile["/tosca/implicit/2.0/js/constraints/schema.js"], // introduced in TOSCA 1.3
}
var ConstraintClauseNativeArgumentIndexes = map[string][]uint{
"tosca.constraint.equal": {0},
"tosca.constraint.greater_than": {0},
"tosca.constraint.greater_or_equal": {0},
"tosca.constraint.less_than": {0},
"tosca.constraint.less_or_equal": {0},
"tosca.constraint.in_range": {0, 1},
}
//
// ConstraintClause
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.6.3
// [TOSCA-Simple-Profile-YAML-v1.2] @ 3.6.3
// [TOSCA-Simple-Profile-YAML-v1.1] @ 3.5.2
// [TOSCA-Simple-Profile-YAML-v1.0] @ 3.5.2
//
type ConstraintClause struct {
*Entity `name:"constraint clause"`
Operator string
Arguments ard.List
NativeArgumentIndexes []uint
DataType *DataType `traverse:"ignore" json:"-" yaml:"-"`
}
func NewConstraintClause(context *tosca.Context) *ConstraintClause {
return &ConstraintClause{Entity: NewEntity(context)}
}
// tosca.Reader signature
func ReadConstraintClause(context *tosca.Context) tosca.EntityPtr {
self := NewConstraintClause(context)
if context.ValidateType(ard.TypeMap) {
map_ := context.Data.(ard.Map)
if len(map_) != 1 {
context.ReportValueMalformed("constraint clause", "map length not 1")
return self
}
for key, value := range map_ {
operator := yamlkeys.KeyString(key)
scriptletName := "tosca.constraint." + operator
scriptlet, ok := context.ScriptletNamespace.Lookup(scriptletName)
if !ok {
context.Clone(operator).ReportValueMalformed("constraint clause", "unsupported operator")
return self
}
self.Operator = operator
if list, ok := value.(ard.List); ok {
self.Arguments = list
} else {
self.Arguments = ard.List{value}
}
self.NativeArgumentIndexes = scriptlet.NativeArgumentIndexes
// We have only one key
break
}
}
return self
}
func (self *ConstraintClause) ToFunctionCall(context *tosca.Context, strict bool) *tosca.FunctionCall {
arguments := make([]interface{}, len(self.Arguments))
for index, argument := range self.Arguments {
if self.IsNativeArgument(uint(index)) {
if _, ok := argument.(*Value); !ok {
if self.DataType != nil {
value := ReadValue(context.ListChild(index, argument)).(*Value)
value.RenderAttribute(self.DataType, nil, true, false) // bare
argument = value
} else if strict {
panic("no data type for native argument")
}
}
}
arguments[index] = argument
}
return context.NewFunctionCall("tosca.constraint."+self.Operator, arguments)
}
func (self *ConstraintClause) IsNativeArgument(index uint) bool {
for _, i := range self.NativeArgumentIndexes {
if i == index {
return true
}
}
return false
}
//
// ConstraintClauses
//
type ConstraintClauses []*ConstraintClause
func (self ConstraintClauses) Append(constraints ConstraintClauses) ConstraintClauses {
var r ConstraintClauses
length := len(self)
if length > 0 {
r = make(ConstraintClauses, length)
copy(r, self)
}
return append(r, constraints...)
}
func (self ConstraintClauses) Render(dataType *DataType) {
for _, constraint := range self {
constraint.DataType = dataType
}
}
/*
func (self ConstraintClauses) Validate(dataType *DataType) {
for _, constraintClause := range self {
if (constraintClause.DataType != nil) && (constraintClause.DataType != dataType) {
panic(fmt.Sprintf("constraint clause for data type %q cannot be used with data type %q", constraintClause.DataType.Name, dataType.Name))
}
}
}
*/
func (self ConstraintClauses) Normalize(context *tosca.Context) normal.FunctionCalls {
var normalFunctionCalls normal.FunctionCalls
for _, constraintClause := range self {
functionCall := constraintClause.ToFunctionCall(context, false)
NormalizeFunctionCallArguments(functionCall, context)
normalFunctionCalls = append(normalFunctionCalls, normal.NewFunctionCall(functionCall))
}
return normalFunctionCalls
}
func (self ConstraintClauses) NormalizeConstrainable(context *tosca.Context, normalConstrainable normal.Constrainable) {
for _, constraintClause := range self {
functionCall := constraintClause.ToFunctionCall(context, true)
NormalizeFunctionCallArguments(functionCall, context)
normalConstrainable.AddConstraint(functionCall)
}
}
func (self ConstraintClauses) NormalizeListEntries(context *tosca.Context, normalList *normal.List) {
for _, constraintClause := range self {
functionCall := constraintClause.ToFunctionCall(context, true)
NormalizeFunctionCallArguments(functionCall, context)
normalList.AddEntryConstraint(functionCall)
}
}
func (self ConstraintClauses) NormalizeMapKeys(context *tosca.Context, normalMap *normal.Map) {
for _, constraintClause := range self {
functionCall := constraintClause.ToFunctionCall(context, true)
NormalizeFunctionCallArguments(functionCall, context)
normalMap.AddKeyConstraint(functionCall)
}
}
func (self ConstraintClauses) NormalizeMapValues(context *tosca.Context, normalMap *normal.Map) {
for _, constraintClause := range self {
functionCall := constraintClause.ToFunctionCall(context, true)
NormalizeFunctionCallArguments(functionCall, context)
normalMap.AddValueConstraint(functionCall)
}
}