/
schema.go
318 lines (275 loc) · 6.8 KB
/
schema.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/t0yv0/complang"
)
type Schema struct {
packageSpec *schema.PackageSpec
}
func (s *Schema) Name() string {
return s.packageSpec.Name
}
func (s *Schema) DisplayName() string {
return s.packageSpec.DisplayName
}
func (s *Schema) Version() string {
return s.packageSpec.Version
}
func (s *Schema) Description() string {
return s.packageSpec.Description
}
func (s *Schema) Keywords() []string {
return s.packageSpec.Keywords
}
func (s *Schema) Homepage() string {
return s.packageSpec.Homepage
}
func (s *Schema) License() string {
return s.packageSpec.License
}
func (s *Schema) Attribution() string {
return s.packageSpec.Attribution
}
func (s *Schema) Repository() string {
return s.packageSpec.Repository
}
func (s Schema) LogoURL() string {
return s.packageSpec.LogoURL
}
func (s *Schema) PluginDownloadURL() string {
return s.packageSpec.PluginDownloadURL
}
func (s *Schema) Publisher() string {
return s.packageSpec.Publisher
}
func (s *Schema) AllowedPackageNames() []string {
return s.packageSpec.AllowedPackageNames
}
func (s *Schema) Language() complang.Value {
return complang.BindValue(mustJ(s.packageSpec.Language).v)
}
func (s *Schema) Config() complang.Value {
return complang.BindValue(inlineRefs(s.packageSpec, mustJ(s.packageSpec.Config)).v)
}
func (s *Schema) Meta() complang.Value {
return complang.BindValue(inlineRefs(s.packageSpec, mustJ(s.packageSpec.Meta)).v)
}
func (s *Schema) Provider() complang.Value {
return complang.BindValue(inlineRefs(s.packageSpec, mustJ(s.packageSpec.Provider)).v)
}
func (s *Schema) Resources() map[string]complang.Value {
m := map[string]complang.Value{}
for k, v := range s.packageSpec.Resources {
m[k] = newResource(s, k, v)
}
return m
}
func (s *Schema) Functions() map[string]complang.Value {
m := map[string]complang.Value{}
for k, v := range s.packageSpec.Functions {
m[k] = newFunction(s, k, v)
}
return m
}
func (s *Schema) Types() map[string]complang.Value {
m := map[string]complang.Value{}
for k, v := range s.packageSpec.Types {
m[k] = newType(s, k, v)
}
return m
}
type Resource struct {
schema *Schema
token string
res schema.ResourceSpec
desc string
}
func newResource(s *Schema, tok string, res schema.ResourceSpec) complang.Value {
copy := res
copy.Description = ""
schema := inlineRefs(s.packageSpec, mustJ(copy)).v
return complang.OverloadedValue(
complang.BindValue(Resource{s, tok, res, res.Description}),
complang.BindValue(schema),
)
}
func (r Resource) Description() string {
return r.desc
}
func (r Resource) Token() string {
return r.token
}
type Function struct {
schema *Schema
token string
fu schema.FunctionSpec
desc string
}
func newFunction(s *Schema, tok string, fu schema.FunctionSpec) complang.Value {
copy := fu
copy.Description = ""
schema := inlineRefs(s.packageSpec, mustJ(copy)).v
return complang.OverloadedValue(
complang.BindValue(Function{s, tok, fu, fu.Description}),
complang.BindValue(schema),
)
}
func (f Function) Token() string {
return f.token
}
func (f Function) Description() string {
return f.desc
}
type Type struct {
schema *Schema
token string
ty schema.ComplexTypeSpec
}
func newType(s *Schema, tok string, ty schema.ComplexTypeSpec) complang.Value {
return complang.OverloadedValue(
complang.BindValue(Type{s, tok, ty}),
complang.BindValue(inlineRefs(s.packageSpec, mustJ(ty)).v),
)
}
func (f Type) Token() string {
return f.token
}
func LoadSchema() (*Schema, error) {
spec, err := autoloadSchema()
if err != nil {
return nil, err
}
return &Schema{spec}, nil
}
func detectSchemaFile() (string, error) {
if _, err := os.ReadFile("schema.json"); err == nil {
return "schema.json", nil
}
return detectSchemaGitPath()
}
func loadSchemaAtGitRef(ref string) (*schema.PackageSpec, error) {
schemaPath, err := detectSchemaGitPath()
if err != nil {
return nil, err
}
var buf bytes.Buffer
cmd := exec.Command("git", "show", fmt.Sprintf("%s:%s", ref, schemaPath))
cmd.Stdout = &buf
cmd.Stderr = &bytes.Buffer{}
if err := cmd.Run(); err != nil {
return nil, err
}
var s schema.PackageSpec
if err := json.Unmarshal(buf.Bytes(), &s); err != nil {
return nil, err
}
return &s, nil
}
func autoloadSchema() (*schema.PackageSpec, error) {
var packageSpec schema.PackageSpec
f, err := detectSchemaFile()
if err != nil {
return nil, err
}
bytes, err := os.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("Error reading schema file %q: %w", f, err)
}
if err := json.Unmarshal(bytes, &packageSpec); err != nil {
return nil, fmt.Errorf("Error unmarshalling schema: %w", err)
}
return &packageSpec, nil
}
func (s *Schema) diffRef(ref string) complang.Value {
baseline, err := loadSchemaAtGitRef(ref)
if err != nil {
return &complang.Error{
ErrorMessage: fmt.Sprintf("%v", err),
}
}
diff, hasDiff := mustJ(baseline).diff(mustJ(s.packageSpec))
if hasDiff {
return complang.BindValue(diff.v)
}
return complang.BindValue("No schema changes between " + ref + " and current version")
}
// Diff between baseline value retrieved as follows, and the given value.
//
// git show HEAD:..schema.json
func (s *Schema) Diff() complang.Value {
return s.diffRef("HEAD")
}
func (s *Schema) DiffTag() map[string]complang.Value {
result := map[string]complang.Value{}
tags, err := detectGitTags()
if err != nil {
return result
}
for _, t := range tags {
if !strings.HasPrefix(t, "v") {
continue
}
t := t
result[t] = complang.LazyValue(func() complang.Value {
return s.diffRef(t)
})
}
return result
}
// Inlines local type references for easier viewing and diffing.
//
// Given input:
//
// filter:
// $ref: '#/types/cloudflare:index/getZonesFilter:getZonesFilter'
//
// Will lookup type definition for "cloudflare:index/getZonesFilter:getZonesFilter" in s.Types
// and inline it:
//
// filter:
// $ref: '#/types/cloudflare:index/getZonesFilter:getZonesFilter'
// type: object
// properties:
// accountId:
// description: |
// The account identifier to target for the resource.
// type: string
func inlineRefs(s *schema.PackageSpec, v j) j {
return v.transform(func(inj j) j {
switch injv := inj.v.(type) {
case map[string]interface{}:
ref, hasRef := injv["$ref"]
if !hasRef {
return inj
}
refs, ok := ref.(string)
if !ok {
return inj
}
if !strings.HasPrefix(refs, "#/types/") {
return inj
}
tref := strings.TrimPrefix(refs, "#/types/")
t, gotT := s.Types[tref]
if !gotT {
return inj
}
copy := map[string]interface{}{}
for k, v := range injv {
copy[k] = v
}
for k, v := range mustJ(t).v.(map[string]interface{}) {
copy[k] = v
}
return j{copy}
default:
return inj
}
})
}