-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove strings dependency, optimizations (#23)
- Loading branch information
Showing
7 changed files
with
138 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build std | ||
// +build std | ||
|
||
package main | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/extism/go-pdk" | ||
) | ||
|
||
// Currently, the standard Go compiler cannot export custom functions and is limited to exporting | ||
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will | ||
// invoke by explicitly calling `_start`. | ||
func main() { | ||
countVowels() | ||
} | ||
|
||
func countVowels() int32 { | ||
input := pdk.Input() | ||
|
||
count := 0 | ||
for _, a := range input { | ||
switch a { | ||
case 'A', 'I', 'E', 'O', 'U', 'a', 'e', 'i', 'o', 'u': | ||
count++ | ||
default: | ||
} | ||
} | ||
|
||
// test some extra pdk functionality | ||
if pdk.GetVar("a") == nil { | ||
pdk.SetVar("a", []byte("this is var a")) | ||
} | ||
varA := pdk.GetVar("a") | ||
thing, ok := pdk.GetConfig("thing") | ||
|
||
if !ok { | ||
thing = "<unset by host>" | ||
} | ||
|
||
output := `{"count": ` + strconv.Itoa(count) + `, "config": "` + thing + `", "a": "` + string(varA) + `"}` | ||
mem := pdk.AllocateString(output) | ||
|
||
// zero-copy output to host | ||
pdk.OutputMemory(mem) | ||
|
||
return 0 | ||
} |
3 changes: 3 additions & 0 deletions
3
example/countvowels/main.go → example/countvowels/tiny_main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
//go:build !std | ||
// +build !std | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//go:build std | ||
// +build std | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/extism/go-pdk" | ||
) | ||
|
||
// Currently, the standard Go compiler cannot export custom functions and is limited to exporting | ||
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will | ||
// invoke by explicitly calling `_start`. | ||
func main() { | ||
httpGet() | ||
} | ||
|
||
func httpGet() int32 { | ||
// create an HTTP Request (withuot relying on WASI), set headers as needed | ||
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://jsonplaceholder.typicode.com/todos/1") | ||
req.SetHeader("some-name", "some-value") | ||
req.SetHeader("another", "again") | ||
// send the request, get response back (can check status on response via res.Status()) | ||
res := req.Send() | ||
|
||
// zero-copy output to host | ||
pdk.OutputMemory(res.Memory()) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters