Skip to content

Commit

Permalink
feat: add json utils to pdk (#30)
Browse files Browse the repository at this point in the history
* feat: add json utils to pdk

* chore: rename Json -> JSON for common go naming

* feat: update docs to include new json helpers
  • Loading branch information
nilslice authored Feb 2, 2024
1 parent 6e7ff40 commit fae85da
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,19 @@ type Sum struct {
//export add
func add() int32 {
params := Add{}
err := json.Unmarshal(pdk.Input(), &params)
// use json input helper, which automatically unmarshals the plugin input into your struct
err := pdk.InputJSON(&params)
if err != nil {
pdk.SetError(err)
return 1
}
sum := Sum{Sum: params.A + params.B}
output, err := json.Marshal(sum)
// use json output helper, which automatically marshals your struct to the plugin output
output, err := pdk.OutputJSON(sum)
if err != nil {
pdk.SetError(err)
return 1
}
pdk.Output(output)
return 0
}
```
Expand Down
40 changes: 38 additions & 2 deletions example/countvowels/std_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
// "fmt"
"strconv"

"github.com/extism/go-pdk"
Expand All @@ -13,10 +14,45 @@ import (
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
// invoke by explicitly calling `_start`.
func main() {
countVowels()
count_vowels()
count_vowels_typed()
count_vowels_json_output()
}

func countVowels() int32 {
type CountVowelsInput struct {
Input string `json:"input"`
}

type CountVowelsOuptut struct {
Count int `json:"count"`
Total int `json:"total"`
Vowels string `json:"vowels"`
}

//export count_vowels_typed
func count_vowels_typed() int32 {
var input CountVowelsInput
if err := pdk.InputJSON(&input); err != nil {
pdk.SetError(err)
return -1
}

pdk.OutputString(input.Input)
return 0
}

//export count_vowels_json_output
func count_vowels_json_output() int32 {
output := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
err := pdk.OutputJSON(output)
if err != nil {
pdk.SetError(err)
return -1
}
return 0
}

func count_vowels() int32 {
input := pdk.Input()

count := 0
Expand Down
33 changes: 33 additions & 0 deletions example/countvowels/tiny_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ import (
"github.com/extism/go-pdk"
)

type CountVowelsInput struct {
Input string `json:"input"`
}

type CountVowelsOuptut struct {
Count int `json:"count"`
Total int `json:"total"`
Vowels string `json:"vowels"`
}

//export count_vowels_typed
func count_vowels_typed() int32 {
var input CountVowelsInput
if err := pdk.InputJSON(&input); err != nil {
pdk.SetError(err)
return -1
}

pdk.OutputString(input.Input)
return 0
}

//export count_vowels_json_output
func count_vowels_json_output() int32 {
output := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
err := pdk.OutputJSON(output)
if err != nil {
pdk.SetError(err)
return -1
}
return 0
}

//export count_vowels
func count_vowels() int32 {
input := pdk.Input()
Expand Down
19 changes: 19 additions & 0 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ func Input() []byte {
return loadInput()
}

func JSONFrom(offset uint64, v any) error {
mem := FindMemory(offset)
return json.Unmarshal(mem.ReadBytes(), v)
}

func InputJSON(v any) error {
return json.Unmarshal(Input(), v)
}

func OutputJSON(v any) error {
b, err := json.Marshal(v)
if err != nil {
return err
}

OutputMemory(AllocateBytes(b))
return nil
}

func Allocate(length int) Memory {
clength := uint64(length)
offset := extism_alloc(clength)
Expand Down

0 comments on commit fae85da

Please sign in to comment.