-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvegagoja.go
500 lines (458 loc) · 12.6 KB
/
vegagoja.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Package vegagoja renders [Vega] and [Vega-Lite] visualizations as SVGs using
// the [goja] JavaScript runtime. Developed for use by [usql] for rendering
// charts.
//
// [Vega]: https://vega.github.io/vega/examples/
// [Vega-Lite]: https://vega.github.io/vega-lite/examples/
// [goja]: https://github.com/dop251/goja
// [usql]: https://github.com/xo/usql
package vegagoja
import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"strings"
"sync"
"github.com/dop251/goja"
gojaparser "github.com/dop251/goja/parser"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/dop251/goja_nodejs/require"
"github.com/mitchellh/mapstructure"
)
// Vega handles rendering [Vega] and [Vega-Lite] visualizations as SVGs,
// wrapping a [goja] runtime vm.
//
// [Vega]: https://vega.github.io/vega/examples/
// [Vega-Lite]: https://vega.github.io/vega-lite/examples/
// [goja]: https://github.com/dop251/goja
type Vega struct {
logger func(...interface{})
vegaJs *goja.Program
vegaLiteJs *goja.Program
vegagojaJs *goja.Program
params map[string]interface{}
source fs.FS
once sync.Once
err error
}
// New creates a new vega instance.
func New(opts ...Option) *Vega {
vm := new(Vega)
for _, o := range opts {
o(vm)
}
return vm
}
// init initializes the vega instance.
func (vm *Vega) init() error {
vm.once.Do(func() {
if vm.vegaJs, vm.err = compileEmbeddedScript("vega.min.js"); vm.err != nil {
return
}
if vm.vegaLiteJs, vm.err = compileEmbeddedScript("vega-lite.min.js"); vm.err != nil {
return
}
if vm.vegagojaJs, vm.err = compileEmbeddedScript("vegagoja.js"); vm.err != nil {
return
}
})
return vm.err
}
// run runs the embedded javascripts on the goja runtime, and exports a symbol
// name to v.
func (vm *Vega) run(r *goja.Runtime, name string, v interface{}) error {
if _, err := r.RunProgram(vm.vegaJs); err != nil {
return err
}
if _, err := r.RunProgram(vm.vegaLiteJs); err != nil {
return err
}
if _, err := r.RunProgram(vm.vegagojaJs); err != nil {
return err
}
if name != "" {
return r.ExportTo(r.Get(name), v)
}
return nil
}
// loop instantiates a new loop, and waits for it to terminate, or until the
// context is closed.
func (vm *Vega) loop(ctx context.Context, f func(*goja.Runtime) error) error {
// init
if err := vm.init(); err != nil {
return err
}
mod := console.RequireWithPrinter(vm)
registry := new(require.Registry)
registry.RegisterNativeModule(console.ModuleName, mod)
loop := eventloop.NewEventLoop(
eventloop.WithRegistry(registry),
)
errch := make(chan error, 1)
go func() {
loop.Run(func(r *goja.Runtime) {
defer close(errch)
errch <- f(r)
})
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errch:
return err
}
}
// Version returns the embedded vega version.
func (vm *Vega) Version() (string, string, error) {
if err := vm.init(); err != nil {
return "", "", err
}
r := goja.New()
var f func() ([]string, error)
if err := vm.run(r, "version", &f); err != nil {
return "", "", err
}
ver, err := f()
switch {
case err != nil:
return "", "", err
case len(ver) != 2:
return "", "", ErrInvalidResult
}
return ver[0], ver[1], nil
}
// CompileSpec compiles a vega-lite specification to a vega specification,
// returning the entire raw compiled json containing the "spec" and
// "normalized" fields.
func (vm *Vega) CompileSpec(spec string) (string, error) {
if err := vm.init(); err != nil {
return "", err
}
var compile compileFunc
if err := vm.run(goja.New(), "compile", &compile); err != nil {
return "", err
}
return compile(vm.log, spec)
}
// Compile compiles a vega-lite specification to a vega specification.
//
// Wraps [CompileSpec], returning only the compiled "spec".
func (vm *Vega) Compile(spec string) (string, error) {
spec, err := vm.CompileSpec(spec)
if err != nil {
return "", err
}
var res map[string]interface{}
if err := json.Unmarshal([]byte(spec), &res); err != nil {
return "", ErrInvalidCompiledSpec
}
s, ok := res["spec"]
if !ok {
return "", ErrInvalidCompiledSpec
}
return jsonEncode(s)
}
// CheckSpec takes a spec and checks that it is a vega or vega-lite spec. If it
// is a vega-lite spec, it will returned the compiled vega spec, and
// interpolate params.
func (vm *Vega) CheckSpec(spec string) (string, error) {
// unmarshal
var m map[string]interface{}
if err := json.Unmarshal([]byte(spec), &m); err != nil {
return "", ErrInvalidJSON
}
// check schema
s, ok := m["$schema"]
if !ok {
return "", ErrMissingSchema
}
schema, ok := s.(string)
switch {
case !ok:
return "", ErrSchemaInvalid
case !strings.HasPrefix(schema, "https://vega.github.io/schema/vega"):
return "", ErrNotVegaOrVegaLiteSchema
case strings.HasPrefix(schema, "https://vega.github.io/schema/vega/"):
return spec, nil
}
// interpolate
if vm.params != nil {
var p struct {
Params []map[string]interface{} `mapstructure:"params"`
}
switch err := mapstructure.Decode(m, &p); {
case err != nil:
return "", err
case len(p.Params) != 0:
if spec, err = paramInterpolate(m, p.Params, vm.params); err != nil {
return "", err
}
}
}
// convert vega-lite -> vega
spec, err := vm.Compile(spec)
if err != nil {
return "", fmt.Errorf("unable to compile vega-lite spec: %w", err)
}
return spec, nil
}
// ParamExtract extracts the params from the spec.
func (vm *Vega) ParamExtract(spec string) ([]Param, error) {
// unmarshal
var m map[string]interface{}
if err := json.Unmarshal([]byte(spec), &m); err != nil {
return nil, ErrInvalidJSON
}
var p struct {
Params []Param `mapstructure:"params"`
Other map[string]interface{} `mapstructure:",remain"`
}
md := new(mapstructure.Metadata)
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: md,
Result: &p,
})
if err != nil {
return nil, err
}
if err := dec.Decode(m); err == nil {
return p.Params, nil
}
return nil, nil
}
// Render renders a vega visualization spec as a SVG.
func (vm *Vega) Render(ctx context.Context, spec string) (res string, err error) {
if spec, err = vm.CheckSpec(spec); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
if ex, ok := e.(*goja.Exception); ok {
res, err = "", ex.Unwrap()
} else {
res, err = "", fmt.Errorf("recovered from: %v", e)
}
}
}()
err = vm.loop(ctx, func(r *goja.Runtime) error {
var f renderFunc
if err := vm.run(r, "render", &f); err != nil {
return err
}
promise := f(vm.log, spec, vm.load)
switch state := promise.State(); state {
case goja.PromiseStateFulfilled:
result := promise.Result()
if result == nil {
return ErrInvalidResult
}
res = result.String()
case goja.PromiseStateRejected:
result := promise.Result()
if obj, ok := result.(*goja.Object); ok {
if stack := obj.Get("stack"); stack != nil {
return fmt.Errorf("caught error during rendering: %s", stack.String())
}
}
return fmt.Errorf("unknown rejected promise result: %v", result)
default:
return fmt.Errorf("unknown promise state: %v", state)
}
return nil
})
return
}
// Log satisfies the [goja.Printer] interface.
func (vm *Vega) Log(s string) {
vm.log([]string{"LOG", s})
}
// Warn satisfies the [goja.Printer] interface.
func (vm *Vega) Warn(s string) {
vm.log([]string{"WARN", s})
}
// Error satisfies the [goja.Printer] interface.
func (vm *Vega) Error(s string) {
vm.log([]string{"ERROR", s})
}
// log is the script callback for logging a message.
func (vm *Vega) log(s []string) {
if vm.logger != nil {
v := make([]interface{}, len(s))
for i, ss := range s {
v[i] = ss
}
vm.logger(v...)
}
}
// load loads data from sources.
func (vm *Vega) load(name string) (string, error) {
if vm.source != nil {
f, err := vm.source.Open(name)
if err != nil {
return "", fmt.Errorf("could not open from data %s: %w", name, err)
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
return "", fmt.Errorf("unable to read all data for %s: %w", name, err)
}
return string(buf), nil
}
return "", fmt.Errorf("no loader for %s: %w", name, os.ErrNotExist)
}
// Option is a vega option.
type Option func(*Vega)
// WithLogger is a vega option to set the logger.
func WithLogger(logger func(...interface{})) Option {
return func(vm *Vega) {
vm.logger = logger
}
}
// WithParams is a vega option to set interpolated variables.
func WithParams(params map[string]interface{}) Option {
return func(vm *Vega) {
vm.params = params
}
}
// WithResultSet is a vega option to set the data from a result set.
func WithResultSet(resultSet ResultSet) Option {
return func(vm *Vega) {
}
}
// WithRecords is a vega option to set the data from a set of headers and
// records.
func WithRecords(headers []string, records [][]string) Option {
return func(vm *Vega) {
}
}
// WithCSV is a vega option to read csv data from the supplied reader.
func WithCSV(r io.Reader) Option {
return func(vm *Vega) {
}
}
// WithCSVString is a vega option to read csv data from the string.
func WithCSVString(s string) Option {
return func(vm *Vega) {
WithCSV(strings.NewReader(s))(vm)
}
}
// WithCSVBytes is a vega option to read csv data from the bytes.
func WithCSVBytes(buf []byte) Option {
return func(vm *Vega) {
WithCSV(bytes.NewReader(buf))(vm)
}
}
// WithSources is a vega option to set the source file systems ([fs.FS]) from
// which to load data.
func WithSources(sources ...fs.FS) Option {
return func(vm *Vega) {
vm.source = NewSource(sources...)
}
}
// WithDataDir is a vega option to add a data source that loads data from a
// directory.
func WithDataDir(dir string) Option {
return func(vm *Vega) {
vm.source = os.DirFS(dir)
}
}
// WithPrefixedSourceDir is a vega option to add a data source that loads data
// from a specified prefixed directory name.
func WithPrefixedSourceDir(prefix, dir string) Option {
return func(vm *Vega) {
vm.source = NewPrefixedSourceDir(prefix, dir)
}
}
// Error is a error.
type Error string
// Errors.
const (
// ErrInvalidCompiledSpec is the invalid compiled spec error.
ErrInvalidCompiledSpec Error = "invalid compiled spec"
// ErrInvalidJSON is the invalid json error.
ErrInvalidJSON Error = "invalid json"
// ErrMissingSchema is the missing $schema error.
ErrMissingSchema Error = "missing $schema"
// ErrSchemaInvalid is the $schema invalid error.
ErrSchemaInvalid Error = "$schema invalid"
// ErrNotVegaOrVegaLiteSchema is the not vega or vega-lite schema error.
ErrNotVegaOrVegaLiteSchema Error = "not vega or vega-lite schema"
// ErrInvalidResult is the invalid result error.
ErrInvalidResult Error = "invalid result"
)
// Error satisfies the [error] interface.
func (err Error) Error() string {
return string(err)
}
// compile compiles a goja program.
func compile(name, src string) (*goja.Program, error) {
prg, err := goja.Parse(name, src, gojaparser.WithDisableSourceMaps)
if err != nil {
return nil, fmt.Errorf("unable to parse %s: %w", name, err)
}
p, err := goja.CompileAST(prg, true)
if err != nil {
return nil, fmt.Errorf("unable to compile %s: %w", name, err)
}
return p, nil
}
// compileEmbeddedScript compiles the embedded script as a goja program.
func compileEmbeddedScript(name string) (*goja.Program, error) {
buf, err := jsScripts.ReadFile(name)
if err != nil {
return nil, fmt.Errorf("unable to load %s: %w", name, err)
}
return compile(name, string(buf))
}
// paramInterpolate interpolates param values.
func paramInterpolate(m map[string]interface{}, params []map[string]interface{}, values map[string]interface{}) (string, error) {
for _, param := range params {
name, ok := param["name"]
if !ok {
continue
}
s, ok := name.(string)
if !ok {
continue
}
if value, ok := values[s]; ok {
param["value"] = value
}
}
m["params"] = params
return jsonEncode(m)
}
// jsonEncode encodes v as json.
func jsonEncode(v interface{}) (string, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
return "", err
}
return buf.String(), nil
}
// loggerFunc is the signature for the log func.
type loggerFunc func([]string)
// renderFunc is the signature for the render func.
type renderFunc func(logf loggerFunc, spec string, data interface{}) *goja.Promise
// compileFunc is the signature for the compile func.
type compileFunc func(logf loggerFunc, spec string) (string, error)
// vegaVersionTxt is the embedded vega-version.txt.
//
//go:embed vega-version.txt
var vegaVersionTxt string
// liteVersionTxt is the embedded vega-lite-version.txt.
//
//go:embed vega-lite-version.txt
var liteVersionTxt string
// jsScripts are the embedded vega javascripts.
//
//go:embed *.js
var jsScripts embed.FS