File tree 4 files changed +94
-5
lines changed
4 files changed +94
-5
lines changed Original file line number Diff line number Diff line change @@ -116,18 +116,19 @@ type Sum struct {
116
116
// export add
117
117
func add () int32 {
118
118
params := Add{}
119
- err := json.Unmarshal (pdk.Input (), ¶ms)
119
+ // use json input helper, which automatically unmarshals the plugin input into your struct
120
+ err := pdk.InputJSON (¶ms)
120
121
if err != nil {
121
122
pdk.SetError (err)
122
123
return 1
123
124
}
124
125
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)
126
128
if err != nil {
127
129
pdk.SetError (err)
128
130
return 1
129
131
}
130
- pdk.Output (output)
131
132
return 0
132
133
}
133
134
```
Original file line number Diff line number Diff line change 4
4
package main
5
5
6
6
import (
7
+ // "fmt"
7
8
"strconv"
8
9
9
10
"github.com/extism/go-pdk"
@@ -13,10 +14,45 @@ import (
13
14
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
14
15
// invoke by explicitly calling `_start`.
15
16
func main () {
16
- countVowels ()
17
+ count_vowels ()
18
+ count_vowels_typed ()
19
+ count_vowels_json_output ()
17
20
}
18
21
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 {
20
56
input := pdk .Input ()
21
57
22
58
count := 0
Original file line number Diff line number Diff line change @@ -9,6 +9,39 @@ import (
9
9
"github.com/extism/go-pdk"
10
10
)
11
11
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
+
12
45
//export count_vowels
13
46
func count_vowels () int32 {
14
47
input := pdk .Input ()
Original file line number Diff line number Diff line change @@ -77,6 +77,25 @@ func Input() []byte {
77
77
return loadInput ()
78
78
}
79
79
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
+
80
99
func Allocate (length int ) Memory {
81
100
clength := uint64 (length )
82
101
offset := extism_alloc (clength )
You can’t perform that action at this time.
0 commit comments