MCPcopy Create free account
hub / github.com/brimdata/super / run

Function run

book/super-example/main.go:47–98  ·  view source on GitHub ↗
(opts opts)

Source from the content-addressed store, hash-verified

45var errInvalidInput = errors.New("only string or ReadableStream accept as input")
46
47func run(opts opts) wasm.Promise {
48 return wasm.NewPromise(func() (interface{}, error) {
49 flowgraph, err := parser.ParseText(opts.Program)
50 if err != nil {
51 return "", err
52 }
53 var r io.Reader
54 switch typ := opts.Input.Type(); typ {
55 case js.TypeString:
56 if s := opts.Input.String(); strings.TrimSpace(s) != "" {
57 r = strings.NewReader(s)
58 }
59 case js.TypeObject:
60 if !opts.Input.InstanceOf(js.Global().Get("ReadableStream")) {
61 return nil, errInvalidInput
62 }
63 r = readableStream(opts.Input)
64 default:
65 return "", errInvalidInput
66 }
67 zctx := super.NewContext()
68 var readers []sio.Reader
69 if r != nil {
70 rc, err := anyio.NewReader(zctx, r, anyio.ReaderOpts{Format: opts.InputFormat})
71 if err != nil {
72 return "", err
73 }
74 defer rc.Close()
75 readers = []sio.Reader{rc}
76 }
77 var buf bytes.Buffer
78 zwc, err := anyio.NewWriter(sio.NopCloser(&buf), anyio.WriterOpts{Format: opts.OutputFormat})
79 if err != nil {
80 return "", err
81 }
82 defer zwc.Close()
83 local := storage.NewLocalEngine()
84 comp := compiler.NewCompiler(local)
85 query, err := runtime.CompileQuery(context.Background(), zctx, comp, flowgraph, readers)
86 if err != nil {
87 return "", err
88 }
89 defer query.Pull(true)
90 if err := vio.Copy(zwc, query); err != nil {
91 return "", err
92 }
93 if err := zwc.Close(); err != nil {
94 return "", err
95 }
96 return buf.String(), nil
97 })
98}
99
100func parse(program string) (interface{}, error) {
101 ast, err := parser.ParseText(program)

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
CloseMethod · 0.95
ParseTextFunction · 0.92
NewContextFunction · 0.92
NewReaderFunction · 0.92
NewWriterFunction · 0.92
NopCloserFunction · 0.92
NewLocalEngineFunction · 0.92
NewCompilerFunction · 0.92
CompileQueryFunction · 0.92
CopyFunction · 0.92
readableStreamFunction · 0.85

Tested by

no test coverage detected