-
Notifications
You must be signed in to change notification settings - Fork 20
/
interface-definition.go
107 lines (88 loc) · 3.46 KB
/
interface-definition.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
package tosca_v2_0
import (
"github.com/tliron/puccini/tosca"
)
//
// InterfaceDefinition
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.6.20
// [TOSCA-Simple-Profile-YAML-v1.2] @ 3.6.16
// [TOSCA-Simple-Profile-YAML-v1.1] @ 3.5.14
// [TOSCA-Simple-Profile-YAML-v1.0] @ 3.5.14
//
type InterfaceDefinition struct {
*Entity `name:"interface definition" json:"-" yaml:"-"`
Name string
InterfaceTypeName *string `read:"type"` // required only if cannot be inherited
InputDefinitions PropertyDefinitions `read:"inputs,PropertyDefinition" inherit:"inputs,InterfaceType"`
OperationDefinitions OperationDefinitions `read:"operations,OperationDefinition" inherit:"operations,InterfaceType"` // keyword since TOSCA 1.3
NotificationDefinitions NotificationDefinitions `read:"notifications,NotificationDefinition" inherit:"notifications,InterfaceType"` // introduced in TOSCA 1.3
InterfaceType *InterfaceType `lookup:"type,InterfaceTypeName" json:"-" yaml:"-"`
typeMissingProblemReported bool
}
func NewInterfaceDefinition(context *tosca.Context) *InterfaceDefinition {
return &InterfaceDefinition{
Entity: NewEntity(context),
Name: context.Name,
InputDefinitions: make(PropertyDefinitions),
OperationDefinitions: make(OperationDefinitions),
NotificationDefinitions: make(NotificationDefinitions),
}
}
// tosca.Reader signature
func ReadInterfaceDefinition(context *tosca.Context) tosca.EntityPtr {
self := NewInterfaceDefinition(context)
context.ValidateUnsupportedFields(context.ReadFields(self))
return self
}
// tosca.Mappable interface
func (self *InterfaceDefinition) GetKey() string {
return self.Name
}
func (self *InterfaceDefinition) Inherit(parentDefinition *InterfaceDefinition) {
logInherit.Debugf("interface definition: %s", self.Name)
// Validate type compatibility
if (self.InterfaceType != nil) && (parentDefinition.InterfaceType != nil) && !self.Context.Hierarchy.IsCompatible(parentDefinition.InterfaceType, self.InterfaceType) {
self.Context.ReportIncompatibleType(self.InterfaceType, parentDefinition.InterfaceType)
return
}
if (self.InterfaceTypeName == nil) && (parentDefinition.InterfaceTypeName != nil) {
self.InterfaceTypeName = parentDefinition.InterfaceTypeName
}
if (self.InterfaceType == nil) && (parentDefinition.InterfaceType != nil) {
self.InterfaceType = parentDefinition.InterfaceType
}
self.InputDefinitions.Inherit(parentDefinition.InputDefinitions)
self.OperationDefinitions.Inherit(parentDefinition.OperationDefinitions)
self.NotificationDefinitions.Inherit(parentDefinition.NotificationDefinitions)
}
// parser.Renderable interface
func (self *InterfaceDefinition) Render() {
logRender.Debugf("interface definition: %s", self.Name)
if self.InterfaceTypeName == nil {
// Avoid reporting more than once
if !self.typeMissingProblemReported {
self.Context.FieldChild("type", nil).ReportFieldMissing()
self.typeMissingProblemReported = true
}
}
}
//
// InterfaceDefinitions
//
type InterfaceDefinitions map[string]*InterfaceDefinition
func (self InterfaceDefinitions) Inherit(parentDefinitions InterfaceDefinitions) {
for name, definition := range parentDefinitions {
if _, ok := self[name]; !ok {
self[name] = definition
}
}
for name, definition := range self {
if parentDefinition, ok := parentDefinitions[name]; ok {
if definition != parentDefinition {
definition.Inherit(parentDefinition)
}
}
}
}