This repository was archived by the owner on Jun 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjecttype.go
More file actions
171 lines (152 loc) · 4.13 KB
/
Copy pathobjecttype.go
File metadata and controls
171 lines (152 loc) · 4.13 KB
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
package main
import (
"fmt"
"sort"
)
// TODO: merge TypeName/GetProperties() with PropertyType for objects.
type ObjectType interface {
TypeName() string
Help() string
CanList() bool
List(cfg *Config, op Output, hc httpClient) ([]*ObjectInfo, error)
CanGet() bool
Get(cfg *Config, op Output, hc httpClient, id string) (ObjectInstance, error)
CanCreate() bool
Create(cfg *Config, op Output, hc httpClient, input object) (ObjectInstance, error)
CanUpdate() bool
Update(cfg *Config, op Output, hc httpClient, id string, input object) (ObjectInstance, error)
CanDelete() bool
Delete(cfg *Config, op Output, hc httpClient, id string) error
GetPresentationLabels() []string
GetProperties() []PropertyDesc
}
type ObjectInfo struct {
Id string
Name string
Presentation []string
Object ObjectInstance
}
type ObjectInstance interface {
GetInfo() *ObjectInfo
GetValues() []PropertyInstance
PrintToYaml(op Output, otyp ObjectType, obj ObjectInstance) error
}
var objectTypes = map[string]ObjectType{}
type object = map[string]any
type array = []any
func RegisterObjectType(t ObjectType, tmpl any) {
if _, has := objectTypes[t.TypeName()]; has {
panic(fmt.Errorf("Attempt to re-register object type %q", t.TypeName()))
}
objectTypes[t.TypeName()] = t
}
func GetObjectType(name string) ObjectType {
if typ, has := objectTypes[name]; has {
return typ
}
return nil
}
func GetObjectTypes() []ObjectType {
l := make([]ObjectType, 0, len(objectTypes))
for _, typ := range objectTypes {
l = append(l, typ)
}
sort.Slice(l, func(i, j int) bool {
return l[i].TypeName() < l[j].TypeName()
})
return l
}
func ForeachObjectType(fn func(ot ObjectType)) {
for _, typ := range GetObjectTypes() {
fn(typ)
}
}
// rsp is a map[string]any from a web request
// tgt is the destination to assign to
func unpackObject(rsp object, tgt any, tname string) ObjectInstance {
// Get the info for this type
otype := objectTypes[tname]
for k, v := range rsp {
pd := getpropdesc(otype, k)
pd.Setter(tgt, pd.Type.FromGQL(v))
}
// Return the result
return tgt.(ObjectInstance)
}
func calcHasConfig(props []PropertyDesc) bool {
for _, p := range props {
if !p.IsId && !p.IsComputed {
return true
}
}
return false
}
func calcHasState(props []PropertyDesc) bool {
for _, p := range props {
if !p.IsId && p.IsComputed {
return true
}
}
return false
}
func writeObjectTypeDocs(op Output, ot ObjectType) {
props := ot.GetProperties()
hasConfig := calcHasConfig(props)
hasState := calcHasState(props)
fmt.Fprintf(op, "\n%s:\n", ot.TypeName())
for _, p := range props {
if p.IsId {
fmt.Fprintf(op, " %s: %s\n", p.Name, p.Type.TypeName())
}
}
if hasConfig {
fmt.Fprint(op, " config:\n")
for _, p := range props {
if !p.IsId && !p.IsComputed {
fmt.Fprintf(op, " %s: %s\n", p.Name, p.Type.TypeName())
}
}
}
if hasState {
fmt.Fprint(op, " state:\n")
for _, p := range props {
if !p.IsId && p.IsComputed {
fmt.Fprintf(op, " %s: %s\n", p.Name, p.Type.TypeName())
}
}
}
}
// given an input object in "deep" form, unpack it given the propmap, to "flat"
// form.
func propmapObject(src any, propmap PropertyMap) (ret object, err error) {
ret = make(object)
for dstKey, srcPath := range propmap {
ret[dstKey], err = unpackProppath(src, srcPath)
if err != nil {
return nil, NewObserveError(err, "property %q", dstKey)
}
}
return ret, nil
}
// given a recursive object, unpack it based on the path, returning the
// terminal "leaf" value.
func unpackProppath(cur any, srcPath proppath) (any, error) {
for _, key := range srcPath {
if cur == nil {
// if we try to index a nil, that's no bueno
return nil, NewObserveError(ErrNotAnObject, "path %q", srcPath)
}
obj, is := cur.(object)
if !is {
// if we try to index a non-object, that's no bueno
return nil, NewObserveError(ErrNotAnObject, "path %q", srcPath)
}
cur, is = obj[key]
if !is {
// if we don't have the property at all, that's no bueno
return nil, NewObserveError(ErrNotAnObject, "path %q", srcPath)
}
// but it's OK if we have the property, with a nil value, if it's the final leaf
}
return cur, nil
}