-
Notifications
You must be signed in to change notification settings - Fork 20
/
data-type.go
174 lines (146 loc) · 4.88 KB
/
data-type.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 tosca_v2_0
import (
"reflect"
"github.com/tliron/kutil/ard"
"github.com/tliron/puccini/tosca"
"github.com/tliron/puccini/tosca/normal"
)
type HasComparer interface {
SetComparer(comparer string)
}
//
// DataType
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.7.6
// [TOSCA-Simple-Profile-YAML-v1.2] @ 3.7.6
// [TOSCA-Simple-Profile-YAML-v1.1] @ 3.6.6
// [TOSCA-Simple-Profile-YAML-v1.0] @ 3.6.5
//
type DataType struct {
*Type `name:"data type"`
PropertyDefinitions PropertyDefinitions `read:"properties,PropertyDefinition" inherit:"properties,Parent"`
ConstraintClauses ConstraintClauses `read:"constraints,[]ConstraintClause"`
KeySchema *Schema `read:"key_schema,Schema"` // introduced in TOSCA 1.3
EntrySchema *Schema `read:"entry_schema,Schema"` // introduced in TOSCA 1.3
Parent *DataType `lookup:"derived_from,ParentName" json:"-" yaml:"-"`
}
func NewDataType(context *tosca.Context) *DataType {
return &DataType{
Type: NewType(context),
PropertyDefinitions: make(PropertyDefinitions),
}
}
// tosca.Reader signature
func ReadDataType(context *tosca.Context) tosca.EntityPtr {
self := NewDataType(context)
context.ValidateUnsupportedFields(context.ReadFields(self))
return self
}
// tosca.Hierarchical interface
func (self *DataType) GetParent() tosca.EntityPtr {
return self.Parent
}
// tosca.Inherits interface
func (self *DataType) Inherit() {
logInherit.Debugf("data type: %s", self.Name)
if _, ok := self.GetInternalTypeName(); ok && (len(self.PropertyDefinitions) > 0) {
// Doesn't make sense to be an internal type (non-complex) and also have properties (complex)
self.Context.ReportPrimitiveType()
self.PropertyDefinitions = make(PropertyDefinitions)
return
}
if self.Parent == nil {
return
}
if (self.KeySchema == nil) && (self.Parent.KeySchema != nil) {
self.KeySchema = self.Parent.KeySchema
}
if (self.EntrySchema == nil) && (self.Parent.EntrySchema != nil) {
self.EntrySchema = self.Parent.EntrySchema
}
if self.Parent.ConstraintClauses != nil {
self.ConstraintClauses = self.Parent.ConstraintClauses.Append(self.ConstraintClauses)
}
self.PropertyDefinitions.Inherit(self.Parent.PropertyDefinitions)
}
// parser.Renderable interface
func (self *DataType) Render() {
logRender.Debugf("data type: %s", self.Name)
self.ConstraintClauses.Render(self)
if internalTypeName, ok := self.GetInternalTypeName(); ok {
if _, ok := ard.TypeValidators[internalTypeName]; !ok {
if _, ok := self.Context.Grammar.Readers[string(internalTypeName)]; !ok {
self.Context.ReportUnsupportedType()
}
}
}
}
func (self *DataType) GetInternalTypeName() (ard.TypeName, bool) {
if typeName, ok := self.GetMetadataValue("puccini.type"); ok {
return ard.TypeName(typeName), ok
} else if self.Parent != nil {
// The internal type metadata is inherited
return self.Parent.GetInternalTypeName()
} else {
return ard.NoType, false
}
}
func (self *DataType) GetInternal() (ard.TypeName, ard.TypeValidator, tosca.Reader, bool) {
if internalTypeName, ok := self.GetInternalTypeName(); ok {
if typeValidator, ok := ard.TypeValidators[internalTypeName]; ok {
return internalTypeName, typeValidator, nil, true
} else if reader, ok := self.Context.Grammar.Readers[string(internalTypeName)]; ok {
return internalTypeName, nil, reader, true
}
}
return ard.NoType, nil, nil, false
}
// Note that this may change the data (if it's a map), but that should be fine, because we intend
// for the data to be complete. For the same reason, this action is idempotent (subsequent calls to
// the same data will not have an effect).
func (self *DataType) Complete(context *tosca.Context) {
map_, ok := context.Data.(ard.Map)
if !ok {
// Only for complex data types
return
}
for key, definition := range self.PropertyDefinitions {
childContext := context.MapChild(key, nil)
var data ard.Value
if data, ok = map_[key]; ok {
childContext.Data = data
} else if definition.Default != nil {
// Assign default value
data = definition.Default.Context.Data
childContext.Data = data
map_[key] = data
}
if ToFunctionCall(childContext) {
map_[key] = childContext.Data
} else if definition.DataType != nil {
definition.DataType.Complete(childContext)
}
}
}
func (self *DataType) GetTypeInformation() *normal.TypeInformation {
information := normal.NewTypeInformation()
information.Name = tosca.GetCanonicalName(self)
information.Metadata = tosca.GetInformationMetadata(self.Metadata)
if self.Description != nil {
information.Description = *self.Description
}
return information
}
func GetDataType(context *tosca.Context, name string) (*DataType, bool) {
var dataType *DataType
if entityPtr, ok := context.Namespace.LookupForType(name, reflect.TypeOf(dataType)); ok {
return entityPtr.(*DataType), true
} else {
return nil, false
}
}
//
// DataTypes
//
type DataTypes []*DataType