-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfunctions.go
More file actions
319 lines (275 loc) · 9.11 KB
/
functions.go
File metadata and controls
319 lines (275 loc) · 9.11 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
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
319
//go:build js && wasm
package main
import (
"encoding/json"
"fmt"
"reflect"
"syscall/js"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/token"
"github.com/speakeasy-api/openapi/overlay"
"gopkg.in/yaml.v3"
)
func CalculateOverlay(originalYAML, targetYAML, existingOverlay string) (string, error) {
var orig yaml.Node
err := yaml.Unmarshal([]byte(originalYAML), &orig)
if err != nil {
return "", fmt.Errorf("failed to parse source schema: %w", err)
}
var target yaml.Node
err = yaml.Unmarshal([]byte(targetYAML), &target)
if err != nil {
return "", fmt.Errorf("failed to parse target schema: %w", err)
}
// we go from the original to a new version, then look at the extra overlays on top
// of that, then add that to the existing overlay
var existingOverlayDocument overlay.Overlay
err = yaml.Unmarshal([]byte(existingOverlay), &existingOverlayDocument)
if err != nil {
return "", fmt.Errorf("failed to parse overlay schema in CalculateOverlay: %w", err)
}
existingOverlayDocument.JSONPathVersion = "rfc9535" // force this in the playground.
// now modify the original using the existing overlay
err = existingOverlayDocument.ApplyTo(&orig)
if err != nil {
return "", fmt.Errorf("failed to apply existing overlay: %w", err)
}
newOverlay, err := overlay.Compare("example overlay", &orig, target)
if err != nil {
return "", fmt.Errorf("failed to compare schemas: %w", err)
}
// special case, is there only one action and it targets the same as the last overlayDocument.Actions item entry, we'll just replace it.
if len(newOverlay.Actions) == 1 && len(existingOverlayDocument.Actions) > 0 && newOverlay.Actions[0].Target == existingOverlayDocument.Actions[len(existingOverlayDocument.Actions)-1].Target {
existingOverlayDocument.Actions[len(existingOverlayDocument.Actions)-1] = newOverlay.Actions[0]
} else {
// Otherwise, we'll just append the new overlay to the existing overlay
existingOverlayDocument.Actions = append(existingOverlayDocument.Actions, newOverlay.Actions...)
}
out, err := yaml.Marshal(existingOverlayDocument)
if err != nil {
return "", fmt.Errorf("failed to marshal schema: %w", err)
}
return string(out), nil
}
func GetInfo(originalYAML string) (string, error) {
var orig yaml.Node
err := yaml.Unmarshal([]byte(originalYAML), &orig)
if err != nil {
return "", fmt.Errorf("failed to parse source schema: %w", err)
}
titlePath, err := jsonpath.NewPath("$.info.title")
if err != nil {
return "", err
}
versionPath, err := jsonpath.NewPath("$.info.version")
if err != nil {
return "", err
}
descriptionPath, err := jsonpath.NewPath("$.info.version")
if err != nil {
return "", err
}
toString := func(node []*yaml.Node) string {
if len(node) == 0 {
return ""
}
return node[0].Value
}
return `{
"title": "` + toString(titlePath.Query(&orig)) + `",
"version": "` + toString(versionPath.Query(&orig)) + `",
"description": "` + toString(descriptionPath.Query(&orig)) + `"
}`, nil
}
type ApplyOverlaySuccess struct {
Type string `json:"type"`
Result string `json:"result"`
}
func ApplyOverlay(originalYAML, overlayYAML string) (string, error) {
var orig yaml.Node
err := yaml.Unmarshal([]byte(originalYAML), &orig)
if err != nil {
return "", fmt.Errorf("failed to parse original schema: %w", err)
}
var overlay overlay.Overlay
err = yaml.Unmarshal([]byte(overlayYAML), &overlay)
if err != nil {
return "", fmt.Errorf("failed to parse overlay schema in ApplyOverlay: %w", err)
}
err = overlay.Validate()
if err != nil {
return "", fmt.Errorf("failed to validate overlay schema in ApplyOverlay: %w", err)
}
hasFilterExpression := false
// check to see if we have an overlay with an error, or a partial overlay: i.e. any overlay actions are missing an update or remove
for i, action := range overlay.Actions {
tokenized := token.NewTokenizer(action.Target, config.WithPropertyNameExtension()).Tokenize()
for _, tok := range tokenized {
if tok.Token == token.FILTER {
hasFilterExpression = true
break
}
}
parsed, pathErr := jsonpath.NewPath(action.Target, config.WithPropertyNameExtension())
var node *yaml.Node
if pathErr != nil {
node, err = lookupOverlayActionTargetNode(overlayYAML, i)
if err != nil {
return "", err
}
return applyOverlayJSONPathError(pathErr, node)
}
if reflect.ValueOf(action.Update).IsZero() && action.Remove == false {
result := parsed.Query(&orig)
node, err = lookupOverlayActionTargetNode(overlayYAML, i)
if err != nil {
return "", err
}
return applyOverlayJSONPathIncomplete(result, node)
}
}
if hasFilterExpression && overlay.JSONPathVersion != "rfc9535" {
return "", fmt.Errorf("invalid overlay schema: must have `x-speakeasy-jsonpath: rfc9535`")
}
err = overlay.ApplyTo(&orig)
if err != nil {
return "", fmt.Errorf("failed to apply overlay: %w", err)
}
// Unwrap the document node if it exists and has only one content node
if orig.Kind == yaml.DocumentNode && len(orig.Content) == 1 {
orig = *orig.Content[0]
}
out, err := yaml.Marshal(&orig)
if err != nil {
return "", fmt.Errorf("failed to marshal result: %w", err)
}
out, err = json.Marshal(ApplyOverlaySuccess{
Type: "success",
Result: string(out),
})
return string(out), err
}
type IncompleteOverlayErrorMessage struct {
Type string `json:"type"`
Line int `json:"line"`
Col int `json:"col"`
Result string `json:"result"`
}
func applyOverlayJSONPathIncomplete(result []*yaml.Node, node *yaml.Node) (string, error) {
yamlResult, err := yaml.Marshal(&result)
if err != nil {
return "", err
}
out, err := json.Marshal(IncompleteOverlayErrorMessage{
Type: "incomplete",
Line: node.Line,
Col: node.Column,
Result: string(yamlResult),
})
return string(out), err
}
type JSONPathErrorMessage struct {
Type string `json:"type"`
Line int `json:"line"`
Col int `json:"col"`
ErrMessage string `json:"error"`
}
func applyOverlayJSONPathError(err error, node *yaml.Node) (string, error) {
// first lets see if we can find a target expression
out, err := json.Marshal(JSONPathErrorMessage{
Type: "error",
Line: node.Line,
Col: node.Column,
ErrMessage: err.Error(),
})
return string(out), err
}
func lookupOverlayActionTargetNode(overlayYAML string, i int) (*yaml.Node, error) {
var node struct {
Actions []struct {
Target yaml.Node `yaml:"target"`
} `yaml:"actions"`
}
err := yaml.Unmarshal([]byte(overlayYAML), &node)
if err != nil {
return nil, fmt.Errorf("failed to parse overlay schema in lookupOverlayActionTargetNode: %w", err)
}
if len(node.Actions) <= i {
return nil, fmt.Errorf("no action at index %d", i)
}
if reflect.ValueOf(node.Actions[i].Target).IsZero() {
return nil, fmt.Errorf("no target at index %d", i)
}
return &node.Actions[i].Target, nil
}
func Query(currentYAML, path string) (string, error) {
var orig yaml.Node
err := yaml.Unmarshal([]byte(currentYAML), &orig)
if err != nil {
return "", fmt.Errorf("failed to parse original schema in Query: %w", err)
}
parsed, err := jsonpath.NewPath(path, config.WithPropertyNameExtension())
if err != nil {
return "", err
}
result := parsed.Query(&orig)
// Marshal it back out
out, err := yaml.Marshal(result)
if err != nil {
return "", err
}
return string(out), nil
}
func promisify(fn func(args []js.Value) (string, error)) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
// Handler for the Promise
handler := js.FuncOf(func(this js.Value, promiseArgs []js.Value) interface{} {
resolve := promiseArgs[0]
reject := promiseArgs[1]
// Run this code asynchronously
go func() {
result, err := fn(args)
if err != nil {
errorConstructor := js.Global().Get("Error")
errorObject := errorConstructor.New(err.Error())
reject.Invoke(errorObject)
return
}
resolve.Invoke(result)
}()
// The handler of a Promise doesn't return any value
return nil
})
// Create and return the Promise object
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
}
func main() {
js.Global().Set("CalculateOverlay", promisify(func(args []js.Value) (string, error) {
if len(args) != 3 {
return "", fmt.Errorf("CalculateOverlay: expected 3 args, got %v", len(args))
}
return CalculateOverlay(args[0].String(), args[1].String(), args[2].String())
}))
js.Global().Set("ApplyOverlay", promisify(func(args []js.Value) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("ApplyOverlay: expected 2 args, got %v", len(args))
}
return ApplyOverlay(args[0].String(), args[1].String())
}))
js.Global().Set("GetInfo", promisify(func(args []js.Value) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("GetInfo: expected 1 arg, got %v", len(args))
}
return GetInfo(args[0].String())
}))
js.Global().Set("QueryJSONPath", promisify(func(args []js.Value) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("Query: expected 2 args, got %v", len(args))
}
return Query(args[0].String(), args[1].String())
}))
<-make(chan bool)
}