-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (242 loc) · 5.9 KB
/
main.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
//go:build js && wasm
// +build js,wasm
package main
import (
"bytes"
"encoding/json"
"reflect"
"syscall/js"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/encoding/openapi"
"github.com/mitchellh/mapstructure"
)
func main() {
wait := make(chan struct{}, 0)
api := js.Global().Get("CueWasmAPI")
api.Set("_toJSONImpl", js.FuncOf(toJSON))
api.Set("_toOpenAPIImpl", js.FuncOf(toOpenAPI))
api.Set("_toASTImpl", js.FuncOf(toAST))
<-wait
}
func toJSON(this js.Value, args []js.Value) interface{} {
ctx := cuecontext.New()
value := ctx.CompileString(args[0].String())
err:= value.Err()
if err != nil {
return map[string]interface{}{
"value": "",
"error": err.Error(),
}
}
jsonBytes, err := value.MarshalJSON()
if err != nil {
return map[string]interface{}{
"value": "",
"error": err.Error(),
}
}
return map[string]interface{}{
"value": string(jsonBytes),
"error": nil,
}
}
func genOpenAPI(inst *cue.Instance) ([]byte, error) {
b, err := openapi.Gen(inst, nil)
if err != nil {
return nil, err
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func toOpenAPI(this js.Value, args []js.Value) interface{} {
var r cue.Runtime
inst, err := r.Compile("", args[0].String())
if err != nil {
return map[string]interface{}{
"value": "",
"error": err.Error(),
}
}
jsonBytes, err := genOpenAPI(inst)
if err != nil {
return map[string]interface{}{
"value": "",
"error": err.Error(),
}
}
return map[string]interface{}{
"value": string(jsonBytes),
"error": nil,
}
}
func With(v interface{}) {
panic(v)
}
func On(err error) {
if err != nil {
panic(err)
}
}
func Expect(value interface{}, err error) interface{} {
On(err)
return value
}
func toAST(this js.Value, args []js.Value) interface{} {
ctx := cuecontext.New()
value := ctx.CompileString(args[0].String())
err:= value.Err()
if err != nil {
return map[string]interface{}{
"value": "",
"error": err.Error(),
}
}
astBytes, err := json.Marshal(encodeToPrimitives(value.Source()))
return map[string]interface{}{
"value": string(astBytes),
"error": err,
}
}
func newNodeEncoder(result *map[string]interface{}) *mapstructure.Decoder {
return Expect(mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: encodeHook,
ErrorUnused: true,
Result: result,
})).(*mapstructure.Decoder)
}
func newMapEncoder(result *map[string]interface{}) *mapstructure.Decoder {
return Expect(mapstructure.NewDecoder(&mapstructure.DecoderConfig{
ErrorUnused: true,
Result: result,
})).(*mapstructure.Decoder)
}
func encodeNode(node ast.Node) map[string]interface{} {
var result map[string]interface{}
On(newNodeEncoder(&result).Decode(node))
return result
}
func encodeToPrimitives(node ast.Node) map[string]interface{} {
return encodeNode(node)
}
func unwrapNode(node ast.Node) map[string]interface{} {
switch x := node.(type) {
default:
return encodeMap(x)
}
}
func encodeMap(source interface{}) map[string]interface{} {
var result map[string]interface{}
On(newMapEncoder(&result).Decode(source))
return result
}
func mapIdent(ident *ast.Ident) interface{} {
mapped := map[string]interface{}{
"Name": ident.Name,
"NamePos": ident.NamePos,
}
return mapped
}
func mapIdents(idents []*ast.Ident) []interface{} {
items := make([]interface{}, len(idents))
for i, ident := range idents {
items[i] = mapIdent(ident)
}
return items
}
func DeclsToNodes(decls []ast.Decl) []ast.Node {
nodes := make([]ast.Node, len(decls))
for i, decl := range decls {
nodes[i] = decl
}
return nodes
}
func ExprsToNodes(exprs []ast.Expr) []ast.Node {
nodes := make([]ast.Node, len(exprs))
for i, expr := range exprs {
nodes[i] = expr
}
return nodes
}
func AttributesToNodes(attrs []*ast.Attribute) []ast.Node {
nodes := make([]ast.Node, len(attrs))
for i, attr := range attrs {
nodes[i] = attr
}
return nodes
}
func ClausesToNodes(clauses []ast.Clause) []ast.Node {
nodes := make([]ast.Node, len(clauses))
for i, clause := range clauses {
nodes[i] = clause
}
return nodes
}
func ImportSpecsToNodes(importSpecs []*ast.ImportSpec) []ast.Node {
nodes := make([]ast.Node, len(importSpecs))
for i, importSpec := range importSpecs {
nodes[i] = importSpec
}
return nodes
}
func mapNodes(nodes []ast.Node) []interface{} {
items := make([]interface{}, len(nodes))
for i, item := range nodes {
if node, ok := item.(ast.Node); ok {
items[i] = encodeNode(node)
} else {
items[i] = item
}
}
return items
}
func nodeInner(nodeMap map[string]interface{}) map[string]interface{} {
for k, v := range nodeMap {
if node, ok := v.(ast.Node); ok {
nodeMap[k] = encodeNode(node)
}
}
return nodeMap
}
func encodeHook(sourceType reflect.Type, targetType reflect.Type, source interface{}) (interface{}, error) {
if ident, ok := source.(*ast.Ident); ok {
mapped := map[string]interface{}{
"Name": ident.Name,
"NamePos": ident.NamePos, // todo resolve Node and Scope (cyclic graph).
}
return mapped, nil
}
if node, ok := source.(ast.Node); ok {
mapped := unwrapNode(node) // will still have Node interface values in map values.
return nodeInner(mapped), nil // remap inner Node interface values
}
if decls, ok := source.([]ast.Decl); ok {
nodes := DeclsToNodes(decls)
return mapNodes(nodes), nil
}
if exprs, ok := source.([]ast.Expr); ok {
nodes := ExprsToNodes(exprs)
return mapNodes(nodes), nil
}
if idents, ok := source.([]*ast.Ident); ok {
return mapIdents(idents), nil
}
if importSpecs, ok := source.([]*ast.ImportSpec); ok {
nodes := ImportSpecsToNodes(importSpecs)
return mapNodes(nodes), nil
}
if attrs, ok := source.([]*ast.Attribute); ok {
nodes := AttributesToNodes(attrs)
return mapNodes(nodes), nil
}
if clauses, ok := source.([]ast.Clause); ok {
nodes := ClausesToNodes(clauses)
return mapNodes(nodes), nil
}
return source, nil
}