Skip to content

Commit fae85da

Browse files
authored
feat: add json utils to pdk (#30)
* feat: add json utils to pdk * chore: rename Json -> JSON for common go naming * feat: update docs to include new json helpers
1 parent 6e7ff40 commit fae85da

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,19 @@ type Sum struct {
116116
//export add
117117
func add() int32 {
118118
params := Add{}
119-
err := json.Unmarshal(pdk.Input(), &params)
119+
// use json input helper, which automatically unmarshals the plugin input into your struct
120+
err := pdk.InputJSON(&params)
120121
if err != nil {
121122
pdk.SetError(err)
122123
return 1
123124
}
124125
sum := Sum{Sum: params.A + params.B}
125-
output, err := json.Marshal(sum)
126+
// use json output helper, which automatically marshals your struct to the plugin output
127+
output, err := pdk.OutputJSON(sum)
126128
if err != nil {
127129
pdk.SetError(err)
128130
return 1
129131
}
130-
pdk.Output(output)
131132
return 0
132133
}
133134
```

example/countvowels/std_main.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
// "fmt"
78
"strconv"
89

910
"github.com/extism/go-pdk"
@@ -13,10 +14,45 @@ import (
1314
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
1415
// invoke by explicitly calling `_start`.
1516
func main() {
16-
countVowels()
17+
count_vowels()
18+
count_vowels_typed()
19+
count_vowels_json_output()
1720
}
1821

19-
func countVowels() int32 {
22+
type CountVowelsInput struct {
23+
Input string `json:"input"`
24+
}
25+
26+
type CountVowelsOuptut struct {
27+
Count int `json:"count"`
28+
Total int `json:"total"`
29+
Vowels string `json:"vowels"`
30+
}
31+
32+
//export count_vowels_typed
33+
func count_vowels_typed() int32 {
34+
var input CountVowelsInput
35+
if err := pdk.InputJSON(&input); err != nil {
36+
pdk.SetError(err)
37+
return -1
38+
}
39+
40+
pdk.OutputString(input.Input)
41+
return 0
42+
}
43+
44+
//export count_vowels_json_output
45+
func count_vowels_json_output() int32 {
46+
output := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
47+
err := pdk.OutputJSON(output)
48+
if err != nil {
49+
pdk.SetError(err)
50+
return -1
51+
}
52+
return 0
53+
}
54+
55+
func count_vowels() int32 {
2056
input := pdk.Input()
2157

2258
count := 0

example/countvowels/tiny_main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@ import (
99
"github.com/extism/go-pdk"
1010
)
1111

12+
type CountVowelsInput struct {
13+
Input string `json:"input"`
14+
}
15+
16+
type CountVowelsOuptut struct {
17+
Count int `json:"count"`
18+
Total int `json:"total"`
19+
Vowels string `json:"vowels"`
20+
}
21+
22+
//export count_vowels_typed
23+
func count_vowels_typed() int32 {
24+
var input CountVowelsInput
25+
if err := pdk.InputJSON(&input); err != nil {
26+
pdk.SetError(err)
27+
return -1
28+
}
29+
30+
pdk.OutputString(input.Input)
31+
return 0
32+
}
33+
34+
//export count_vowels_json_output
35+
func count_vowels_json_output() int32 {
36+
output := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
37+
err := pdk.OutputJSON(output)
38+
if err != nil {
39+
pdk.SetError(err)
40+
return -1
41+
}
42+
return 0
43+
}
44+
1245
//export count_vowels
1346
func count_vowels() int32 {
1447
input := pdk.Input()

extism_pdk.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ func Input() []byte {
7777
return loadInput()
7878
}
7979

80+
func JSONFrom(offset uint64, v any) error {
81+
mem := FindMemory(offset)
82+
return json.Unmarshal(mem.ReadBytes(), v)
83+
}
84+
85+
func InputJSON(v any) error {
86+
return json.Unmarshal(Input(), v)
87+
}
88+
89+
func OutputJSON(v any) error {
90+
b, err := json.Marshal(v)
91+
if err != nil {
92+
return err
93+
}
94+
95+
OutputMemory(AllocateBytes(b))
96+
return nil
97+
}
98+
8099
func Allocate(length int) Memory {
81100
clength := uint64(length)
82101
offset := extism_alloc(clength)

0 commit comments

Comments
 (0)