forked from tliron/puccini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.go
94 lines (79 loc) · 2.01 KB
/
mapping.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
package normal
import (
"encoding/json"
"github.com/fxamacker/cbor/v2"
)
//
// Mapping
//
type Mapping struct {
NodeTemplate *NodeTemplate
Relationship *Relationship
TargetType string
Target string
Value Constrainable
}
func NewMapping(targetType string, target string) *Mapping {
return &Mapping{
TargetType: targetType,
Target: target,
}
}
func NewMappingValue(targetType string, value Constrainable) *Mapping {
return &Mapping{
TargetType: targetType,
Value: value,
}
}
func (self *NodeTemplate) NewMapping(targetType string, target string) *Mapping {
return &Mapping{
NodeTemplate: self,
TargetType: targetType,
Target: target,
}
}
func (self *Relationship) NewMapping(targetType string, target string) *Mapping {
return &Mapping{
Relationship: self,
TargetType: targetType,
Target: target,
}
}
type MarshalableMapping struct {
NodeTemplateName string `json:"nodeTemplateName,omitempty" yaml:"nodeTemplateName,omitempty"`
TargetType string `json:"targetType" yaml:"targetType"`
Target string `json:"target,omitempty" yaml:"target,omitempty"`
Value Constrainable `json:"value,omitempty" yaml:"value,omitempty"`
}
func (self *Mapping) Marshalable() any {
if self.NodeTemplate != nil {
return &MarshalableMapping{
NodeTemplateName: self.NodeTemplate.Name,
TargetType: self.TargetType,
Target: self.Target,
Value: self.Value,
}
} else {
return &MarshalableMapping{
TargetType: self.TargetType,
Target: self.Target,
Value: self.Value,
}
}
}
// json.Marshaler interface
func (self *Mapping) MarshalJSON() ([]byte, error) {
return json.Marshal(self.Marshalable())
}
// yaml.Marshaler interface
func (self *Mapping) MarshalYAML() (any, error) {
return self.Marshalable(), nil
}
// cbor.Marshaler interface
func (self *Mapping) MarshalCBOR() ([]byte, error) {
return cbor.Marshal(self.Marshalable())
}
//
// Mappings
//
type Mappings map[string]*Mapping