-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
metadata.go
executable file
·222 lines (189 loc) · 4.3 KB
/
metadata.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package types
import (
"encoding/json"
"fmt"
"strings"
)
type Metadata struct {
rnge Range
ref string
isManaged bool
isDefault bool
isExplicit bool
isUnresolvable bool
parent *Metadata
internal interface{}
}
func (m Metadata) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"range": m.rnge,
"ref": m.ref,
"managed": m.isManaged,
"default": m.isDefault,
"explicit": m.isExplicit,
"unresolvable": m.isUnresolvable,
"parent": m.parent,
})
}
func (m *Metadata) UnmarshalJSON(data []byte) error {
var keys map[string]interface{}
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
if keys["range"] != nil {
raw, err := json.Marshal(keys["range"])
if err != nil {
return err
}
var r Range
if err := json.Unmarshal(raw, &r); err != nil {
return err
}
m.rnge = r
}
if keys["ref"] != nil {
m.ref = keys["ref"].(string)
}
if keys["managed"] != nil {
m.isManaged = keys["managed"].(bool)
}
if keys["default"] != nil {
m.isDefault = keys["default"].(bool)
}
if keys["explicit"] != nil {
m.isExplicit = keys["explicit"].(bool)
}
if keys["unresolvable"] != nil {
m.isUnresolvable = keys["unresolvable"].(bool)
}
if keys["parent"] != nil {
if _, ok := keys["parent"].(map[string]interface{}); ok {
raw, err := json.Marshal(keys["parent"])
if err != nil {
return err
}
var parent Metadata
if err := json.Unmarshal(raw, &parent); err != nil {
return err
}
m.parent = &parent
}
}
return nil
}
func (m *Metadata) ToRego() interface{} {
input := map[string]interface{}{
"filepath": m.Range().GetLocalFilename(),
"startline": m.Range().GetStartLine(),
"endline": m.Range().GetEndLine(),
"sourceprefix": m.Range().GetSourcePrefix(),
"managed": m.isManaged,
"explicit": m.isExplicit,
"fskey": CreateFSKey(m.Range().GetFS()),
"resource": m.Reference(),
}
if m.parent != nil {
input["parent"] = m.parent.ToRego()
}
return input
}
func NewMetadata(r Range, ref string) Metadata {
return Metadata{
rnge: r,
ref: ref,
isManaged: true,
}
}
func NewUnresolvableMetadata(r Range, ref string) Metadata {
unres := NewMetadata(r, ref)
unres.isUnresolvable = true
return unres
}
func NewExplicitMetadata(r Range, ref string) Metadata {
m := NewMetadata(r, ref)
m.isExplicit = true
return m
}
func (m Metadata) WithParent(p Metadata) Metadata {
m.parent = &p
return m
}
func (m *Metadata) SetParentPtr(p *Metadata) {
m.parent = p
}
func (m Metadata) Parent() *Metadata {
return m.parent
}
func (m Metadata) Root() Metadata {
meta := &m
for meta.Parent() != nil {
meta = meta.Parent()
}
return *meta
}
func (m Metadata) WithInternal(internal interface{}) Metadata {
m.internal = internal
return m
}
func (m Metadata) Internal() interface{} {
return m.internal
}
func (m Metadata) IsMultiLine() bool {
return m.rnge.GetStartLine() < m.rnge.GetEndLine()
}
func NewUnmanagedMetadata() Metadata {
m := NewMetadata(NewRange("", 0, 0, "", nil), "")
m.isManaged = false
return m
}
func NewTestMetadata() Metadata {
return NewMetadata(NewRange("test.test", 123, 123, "", nil), "")
}
func NewApiMetadata(provider string, parts ...string) Metadata {
return NewMetadata(NewRange(fmt.Sprintf("/%s/%s", provider, strings.Join(parts, "/")), 0, 0, "", nil), "")
}
func NewRemoteMetadata(id string) Metadata {
return NewMetadata(NewRange(id, 0, 0, "remote", nil), id)
}
func (m Metadata) IsDefault() bool {
return m.isDefault
}
func (m Metadata) IsResolvable() bool {
return !m.isUnresolvable
}
func (m Metadata) IsExplicit() bool {
return m.isExplicit
}
func (m Metadata) String() string {
return m.ref
}
func (m Metadata) Reference() string {
return m.ref
}
func (m Metadata) Range() Range {
return m.rnge
}
func (m Metadata) IsManaged() bool {
return m.isManaged
}
func (m Metadata) IsUnmanaged() bool {
return !m.isManaged
}
type BaseAttribute struct {
metadata Metadata
}
func (b BaseAttribute) GetMetadata() Metadata {
return b.metadata
}
func (m Metadata) GetMetadata() Metadata {
return m
}
func (m Metadata) GetRawValue() interface{} {
return nil
}
func (m *Metadata) SetReference(ref string) {
m.ref = ref
}
func (m *Metadata) SetRange(r Range) {
m.rnge = r
}