-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclout.go
123 lines (104 loc) · 2.95 KB
/
clout.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
package clout
import (
"encoding/json"
"errors"
"fmt"
"github.com/tliron/go-ard"
)
const Version = "1.0"
//
// Clout
//
type Clout struct {
Version string `json:"version" yaml:"version" ard:"version"`
Metadata ard.StringMap `json:"metadata" yaml:"metadata" ard:"metadata"`
Properties ard.StringMap `json:"properties" yaml:"properties" ard:"properties"`
Vertexes Vertexes `json:"vertexes" yaml:"vertexes" ard:"vertexes"`
}
func NewClout() *Clout {
return &Clout{
Version: Version,
Metadata: make(ard.StringMap),
Properties: make(ard.StringMap),
Vertexes: make(Vertexes),
}
}
type MarshalableCloutStringMaps Clout
func (self *Clout) MarshalableStringMaps() any {
return &MarshalableCloutStringMaps{
Version: self.Version,
Metadata: ard.CopyMapsToStringMaps(self.Metadata).(ard.StringMap),
Properties: ard.CopyMapsToStringMaps(self.Properties).(ard.StringMap),
Vertexes: self.Vertexes,
}
}
// ([json.Marshaler] interface)
func (self *Clout) MarshalJSON() ([]byte, error) {
// JavaScript requires keys to be strings, so we would lose complex keys
return json.Marshal(self.MarshalableStringMaps())
}
func (self *Clout) Resolve() error {
if self.Version == "" {
return errors.New("no Clout \"Version\"")
}
if self.Version != Version {
return fmt.Errorf("unsupported Clout version: %q", self.Version)
}
return self.ResolveEdges()
}
func (self *Clout) ResolveEdges() error {
for id, vertex := range self.Vertexes {
vertex.Clout = self
vertex.ID = id
for _, edge := range vertex.EdgesOut {
if edge.Target == nil {
var ok bool
if edge.Target, ok = self.Vertexes[edge.TargetID]; !ok {
return fmt.Errorf("could not resolve Clout, bad TargetID: %q", edge.TargetID)
}
edge.Source = vertex
edge.Target.EdgesIn = append(edge.Target.EdgesIn, edge)
}
}
}
return nil
}
// Creates a copy of the Clout in which ARD is used for all data.
func (self *Clout) Copy() (*Clout, error) {
return self.copy(true)
}
// Creates a copy of the Clout in which non-ARD data is left as is.
func (self *Clout) CopyAsIs() *Clout {
clout, _ := self.copy(false)
return clout
}
func (self *Clout) copy(toArd bool) (*Clout, error) {
clout := Clout{
Version: Version,
}
var err error
if clout.Vertexes, err = self.Vertexes.copy(toArd); err == nil {
if toArd {
if metadata, err := ard.ValidCopyMapsToStringMaps(self.Metadata, nil); err == nil {
if properties, err := ard.ValidCopyMapsToStringMaps(self.Properties, nil); err == nil {
clout.Metadata = metadata.(ard.StringMap)
clout.Properties = properties.(ard.StringMap)
} else {
return nil, err
}
} else {
return nil, err
}
} else {
clout.Metadata = ard.CopyMapsToStringMaps(self.Metadata).(ard.StringMap)
clout.Properties = ard.CopyMapsToStringMaps(self.Properties).(ard.StringMap)
}
} else {
return nil, err
}
if err := clout.ResolveEdges(); err == nil {
return &clout, nil
} else {
return nil, err
}
}