-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (55 loc) · 1.48 KB
/
main.go
File metadata and controls
68 lines (55 loc) · 1.48 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
//go:build js && wasm
package main
import (
"encoding/json"
"syscall/js"
ast "github.com/tinybluerobots/jsonforms-parser"
"github.com/tinybluerobots/jsonforms-html/renderer"
)
func renderHTML(this js.Value, args []js.Value) interface{} {
if len(args) != 1 {
return map[string]interface{}{
"error": "renderHTML expects 1 argument: JSON string with schema and uischema",
}
}
jsonString := args[0].String()
// Parse the input JSON
var input struct {
Schema json.RawMessage `json:"schema"`
UISchema json.RawMessage `json:"uischema"`
}
if err := json.Unmarshal([]byte(jsonString), &input); err != nil {
return map[string]interface{}{
"error": "Invalid JSON: " + err.Error(),
}
}
// Validate we have both schema and uischema
if len(input.Schema) == 0 || len(input.UISchema) == 0 {
return map[string]interface{}{
"error": "JSON must contain both 'schema' and 'uischema' properties",
}
}
// Parse using jsonforms-parser
tree, err := ast.Parse(input.UISchema, input.Schema)
if err != nil {
return map[string]interface{}{
"error": "Failed to parse schemas: " + err.Error(),
}
}
// Render to HTML
html, err := renderer.Render(tree, nil)
if err != nil {
return map[string]interface{}{
"error": "Failed to render HTML: " + err.Error(),
}
}
return map[string]interface{}{
"html": html,
}
}
func main() {
// Make the Go function available to JavaScript
js.Global().Set("renderHTML", js.FuncOf(renderHTML))
// Keep the program running
select {}
}