Skip to content

Commit a32f9a3

Browse files
committed
add variables support for wasip2 sdk
Signed-off-by: Rajat Jindal <[email protected]>
1 parent 88c159a commit a32f9a3

File tree

10 files changed

+186
-2
lines changed

10 files changed

+186
-2
lines changed

v2/examples/variables/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/spinframework/spin-go-sdk/v2/examples/kv
2+
3+
go 1.24.1
4+
5+
require github.com/spinframework/spin-go-sdk/v2 v2.0.0
6+
7+
require (
8+
github.com/julienschmidt/httprouter v1.3.0 // indirect
9+
go.bytecodealliance.org/cm v0.2.2 // indirect
10+
)
11+
12+
replace github.com/spinframework/spin-go-sdk/v2 => ../../

v2/examples/variables/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
2+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
3+
go.bytecodealliance.org/cm v0.2.2 h1:M9iHS6qs884mbQbIjtLX1OifgyPG9DuMs2iwz8G4WQA=
4+
go.bytecodealliance.org/cm v0.2.2/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=

v2/examples/variables/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
spinhttp "github.com/spinframework/spin-go-sdk/v2/http"
8+
"github.com/spinframework/spin-go-sdk/v2/variables"
9+
)
10+
11+
func init() {
12+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
13+
14+
// Get variable value `message` defined in spin.toml.
15+
val, err := variables.Get("message")
16+
if err != nil {
17+
http.Error(w, err.Error(), http.StatusInternalServerError)
18+
return
19+
}
20+
fmt.Fprintln(w, "message: ", val)
21+
})
22+
}
23+
24+
func main() {}

v2/examples/variables/spin.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "variables-example"
5+
authors = ["Fermyon Engineering <[email protected]>"]
6+
description = "A simple Spin application written in (Tiny)Go."
7+
version = "1.0.0"
8+
9+
[variables]
10+
object = { default = "teapot" }
11+
12+
[[trigger.http]]
13+
route = "/..."
14+
component = "variables"
15+
16+
[component.variables]
17+
source = "main.wasm"
18+
[component.variables.variables]
19+
message = "I'm a {{object}}"
20+
21+
[component.hello.build]
22+
command = "tinygo build -target=wasip2 --wit-package $(go list -mod=readonly -m -f '{{.Dir}}' github.com/spinframework/spin-go-sdk/v2)/wit --wit-world http-trigger -gc=leaking -no-debug -o main.wasm main.go"

v2/integration_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ type testSpin struct {
4242
cmd *exec.Cmd
4343
}
4444

45-
func startSpin(t *testing.T, dir string) *testSpin {
45+
func startSpin(t *testing.T, dir string, extraArgs ...string) *testSpin {
4646
buildApp(t, dir)
4747

4848
url := getFreePort(t)
4949

5050
// long timeout because... ci
5151
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
5252

53-
cmd := exec.CommandContext(ctx, spinBinary, "up", "--listen", url)
53+
cmd := exec.CommandContext(ctx, spinBinary, append([]string{"up", "--listen", url}, extraArgs...)...)
5454
cmd.Dir = dir
5555
stderr := new(bytes.Buffer)
5656
cmd.Stderr = stderr
@@ -144,6 +144,30 @@ func TestKeyValue(t *testing.T) {
144144
}
145145
}
146146

147+
func TestVariables(t *testing.T) {
148+
spin := startSpin(t, "variables/testdata/variables")
149+
defer spin.cancel()
150+
151+
resp := retryGet(t, spin.url)
152+
spin.cancel()
153+
if resp.Body == nil {
154+
t.Fatal("body is nil")
155+
}
156+
t.Log(resp.Status)
157+
b, err := io.ReadAll(resp.Body)
158+
resp.Body.Close()
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
163+
// assert response body
164+
want := "message: I'm a teapot\n"
165+
got := string(b)
166+
if want != got {
167+
t.Fatalf("body is not equal: want = %q got = %q", want, got)
168+
}
169+
}
170+
147171
// TestBuildExamples ensures that the tinygo examples will build successfully.
148172
func TestBuildExamples(t *testing.T) {
149173
examples, err := os.ReadDir("examples")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/spinframework/spin-go-sdk/v2/examples/kv
2+
3+
go 1.24.1
4+
5+
require github.com/spinframework/spin-go-sdk/v2 v2.0.0
6+
7+
require (
8+
github.com/julienschmidt/httprouter v1.3.0 // indirect
9+
go.bytecodealliance.org/cm v0.2.2 // indirect
10+
)
11+
12+
replace github.com/spinframework/spin-go-sdk/v2 => ../../../
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
2+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
3+
go.bytecodealliance.org/cm v0.2.2 h1:M9iHS6qs884mbQbIjtLX1OifgyPG9DuMs2iwz8G4WQA=
4+
go.bytecodealliance.org/cm v0.2.2/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
spinhttp "github.com/spinframework/spin-go-sdk/v2/http"
8+
"github.com/spinframework/spin-go-sdk/v2/variables"
9+
)
10+
11+
func init() {
12+
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
13+
// Get variable value `message` defined in spin.toml.
14+
val, err := variables.Get("message")
15+
if err != nil {
16+
http.Error(w, err.Error(), http.StatusInternalServerError)
17+
return
18+
}
19+
fmt.Fprintln(w, "message: ", val)
20+
})
21+
}
22+
23+
func main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "variables-example"
5+
authors = ["Fermyon Engineering <[email protected]>"]
6+
description = "A simple Spin application written in (Tiny)Go."
7+
version = "1.0.0"
8+
9+
[variables]
10+
object = { default = "teapot" }
11+
12+
[[trigger.http]]
13+
route = "/..."
14+
component = "variables"
15+
16+
[component.variables]
17+
source = "main.wasm"
18+
[component.variables.variables]
19+
message = "I'm a {{object}}"
20+
21+
[component.variables.build]
22+
command = "tinygo build -target=wasip2 --wit-package $(go list -mod=readonly -m -f '{{.Dir}}' github.com/spinframework/spin-go-sdk/v2)/wit --wit-world http-trigger -gc=leaking -no-debug -o main.wasm main.go"

v2/variables/variables.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package variables
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spinframework/spin-go-sdk/v2/internal/fermyon/spin/v2.0.0/variables"
7+
)
8+
9+
// Get an application variable value for the current component.
10+
//
11+
// The name must match one defined in in the component manifest.
12+
func Get(key string) (string, error) {
13+
result := variables.Get(key)
14+
if result.IsErr() {
15+
return "", errorVariantToError(*result.Err())
16+
}
17+
18+
// stdout.GetStdout().Write(cm.ToList([]byte(fmt.Sprintf("key: %s, val is %q \n", key, *result.OK()))))
19+
return *result.OK(), nil
20+
}
21+
22+
func errorVariantToError(err variables.Error) error {
23+
switch {
24+
case err.InvalidName() != nil:
25+
return fmt.Errorf(*err.InvalidName())
26+
case err.Provider() != nil:
27+
return fmt.Errorf(*err.Provider())
28+
case err.Undefined() != nil:
29+
return fmt.Errorf(*err.Undefined())
30+
default:
31+
if err.Other() != nil {
32+
return fmt.Errorf(*err.Other())
33+
}
34+
35+
return fmt.Errorf("no error provided by host implementation")
36+
}
37+
}

0 commit comments

Comments
 (0)