Skip to content

Commit

Permalink
feat: add json utils to pdk
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Jan 30, 2024
1 parent d48f368 commit 6432418
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
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 6432418

Please sign in to comment.