-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.go
85 lines (70 loc) · 1.54 KB
/
render.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/extism/go-sdk"
"os"
)
type Template struct {
Name string `json:"name"`
Code string `json:"code"`
}
type Invocation struct {
Name string `json:"name"`
Props map[string]interface{} `json:"props"`
}
func main() {
manifest := extism.Manifest{
Wasm: []extism.Wasm{
extism.WasmUrl{
Url: "https://github.com/dylibso/reactables/releases/latest/download/reactable.core.wasm",
},
},
}
ctx := context.Background()
config := extism.PluginConfig{
EnableWasi: true,
}
reactable, err := extism.NewPlugin(ctx, manifest, config, []extism.HostFunction{})
if err != nil {
fmt.Printf("Failed to initialize plugin: %v\n", err)
os.Exit(1)
}
jsx_code := `
function App(props) {
return <h1>Hello, {props.customerName}!</h1>
}
`
templ := Template{
Name: "greeting-template",
Code: jsx_code,
}
templJson, err := json.Marshal(templ)
if err != nil {
fmt.Printf("Failed to initialize plugin: %v\n", err)
os.Exit(1)
}
exit, _, err := reactable.Call("compileTemplate", templJson)
if err != nil {
fmt.Println(err)
os.Exit(int(exit))
}
var props map[string]interface{}
props["customerName"] = "Benjamin"
inv := Invocation{
Name: "greeting-template",
Props: props,
}
invJson, err := json.Marshal(inv)
if err != nil {
fmt.Printf("Failed to marshal invocation: %v\n", err)
os.Exit(1)
}
exit, out, err := reactable.Call("render", invJson)
if err != nil {
fmt.Println(err)
os.Exit(int(exit))
}
fmt.Println(string(out))
}