-
Notifications
You must be signed in to change notification settings - Fork 20
/
capability.go
102 lines (86 loc) · 2.75 KB
/
capability.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
package normal
import (
"encoding/json"
"math"
"github.com/fxamacker/cbor/v2"
"github.com/tliron/go-ard"
)
//
// Capability
//
type Capability struct {
NodeTemplate *NodeTemplate
Name string
Description string
Types EntityTypes
Properties Values
Attributes Values
MinRelationshipCount uint64
MaxRelationshipCount uint64
Location *Location
}
func (self *NodeTemplate) NewCapability(name string, location *Location) *Capability {
capability := &Capability{
NodeTemplate: self,
Name: name,
Types: make(EntityTypes),
Properties: make(Values),
Attributes: make(Values),
MaxRelationshipCount: math.MaxUint64,
Location: location,
}
self.Capabilities[name] = capability
return capability
}
type MarshalableCapability struct {
Description string `json:"description" yaml:"description"`
Types EntityTypes `json:"types" yaml:"types"`
Properties Values `json:"properties" yaml:"properties"`
Attributes Values `json:"attributes" yaml:"attributes"`
MinRelationshipCount int64 `json:"minRelationshipCount" yaml:"minRelationshipCount"`
MaxRelationshipCount int64 `json:"maxRelationshipCount" yaml:"maxRelationshipCount"`
Location *Location `json:"location" yaml:"location"`
}
func (self *Capability) Marshalable() any {
var minRelationshipCount int64
var maxRelationshipCount int64
minRelationshipCount = int64(self.MinRelationshipCount)
if self.MaxRelationshipCount == math.MaxUint64 {
maxRelationshipCount = -1
} else {
maxRelationshipCount = int64(self.MaxRelationshipCount)
}
return &MarshalableCapability{
Description: self.Description,
Types: self.Types,
Properties: self.Properties,
Attributes: self.Attributes,
MinRelationshipCount: minRelationshipCount,
MaxRelationshipCount: maxRelationshipCount,
Location: self.Location,
}
}
// ([json.Marshaler] interface)
func (self *Capability) MarshalJSON() ([]byte, error) {
return json.Marshal(self.Marshalable())
}
// ([yaml.Marshaler] interface)
func (self *Capability) MarshalYAML() (any, error) {
return self.Marshalable(), nil
}
// ([cbor.Marshaler] interface)
func (self *Capability) MarshalCBOR() ([]byte, error) {
return cbor.Marshal(self.Marshalable())
}
// ([msgpack.Marshaler] interface)
func (self *Capability) MarshalMsgpack() ([]byte, error) {
return ard.MarshalMessagePack(self.Marshalable())
}
// ([ard.ToARD] interface)
func (self *Capability) ToARD(reflector *ard.Reflector) (any, error) {
return reflector.Unpack(self.Marshalable())
}
//
// Capabilities
//
type Capabilities map[string]*Capability