Skip to content

Commit

Permalink
ref(sdk/go): Refactor the Redis Go SDK to be more idiomatic
Browse files Browse the repository at this point in the history
- Introduces a client struct responsible for handling the connection
- Refactor the `Execute()` arguments

Signed-off-by: Adam Reese <[email protected]>
  • Loading branch information
adamreese committed Oct 26, 2023
1 parent fd3a9a7 commit d2322cb
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 150 deletions.
7 changes: 2 additions & 5 deletions examples/tinygo-outbound-redis/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
module github.com/fermyon/spin/templates/spin-http-tinygo-outbound-http

go 1.17
go 1.18

require github.com/fermyon/spin/sdk/go v0.0.0

require (
github.com/julienschmidt/httprouter v1.3.0 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
)
require github.com/julienschmidt/httprouter v1.3.0 // indirect

replace github.com/fermyon/spin/sdk/go v0.0.0 => ../../sdk/go/
33 changes: 0 additions & 33 deletions examples/tinygo-outbound-redis/go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,2 @@
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
45 changes: 22 additions & 23 deletions examples/tinygo-outbound-redis/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package main

import (
"fmt"
"net/http"
"os"
"strconv"
"reflect"
"fmt"
"sort"
"strconv"

"golang.org/x/exp/slices"
spin_http "github.com/fermyon/spin/sdk/go/http"
"github.com/fermyon/spin/sdk/go/redis"
)

func init() {

// handler for the http trigger
spin_http.Handle(func(w http.ResponseWriter, r *http.Request) {
spin_http.Handle(func(w http.ResponseWriter, _ *http.Request) {

// addr is the environment variable set in `spin.toml` that points to the
// address of the Redis server.
Expand All @@ -28,19 +28,21 @@ func init() {
// payload is the data publish to the redis channel.
payload := []byte(`Hello redis from tinygo!`)

if err := redis.Publish(addr, channel, payload); err != nil {
rdb := redis.NewClient(addr)

if err := rdb.Publish(channel, payload); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// set redis `mykey` = `myvalue`
if err := redis.Set(addr, "mykey", []byte("myvalue")); err != nil {
if err := rdb.Set("mykey", []byte("myvalue")); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// get redis payload for `mykey`
if payload, err := redis.Get(addr, "mykey"); err != nil {
if payload, err := rdb.Get("mykey"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
Expand All @@ -50,7 +52,7 @@ func init() {
}

// incr `spin-go-incr` by 1
if payload, err := redis.Incr(addr, "spin-go-incr"); err != nil {
if payload, err := rdb.Incr("spin-go-incr"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
Expand All @@ -60,27 +62,27 @@ func init() {
}

// delete `spin-go-incr` and `mykey`
if payload, err := redis.Del(addr, []string{"spin-go-incr", "mykey", "non-existing-key"}); err != nil {
if payload, err := rdb.Del("spin-go-incr", "mykey", "non-existing-key"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write([]byte("deleted keys num: "))
w.Write([]byte(strconv.FormatInt(payload, 10)))
w.Write([]byte("\n"))
}

if _, err := redis.Sadd(addr, "myset", []string{"foo", "bar"}); err != nil {
if _, err := rdb.Sadd("myset", "foo", "bar"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

{
expected := []string{"bar", "foo"}
payload, err := redis.Smembers(addr, "myset")
payload, err := rdb.Smembers("myset")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
slices.Sort(payload)
sort.Strings(payload)
if !reflect.DeepEqual(payload, expected) {
http.Error(
w,
Expand All @@ -95,14 +97,14 @@ func init() {
}
}

if _, err := redis.Srem(addr, "myset", []string{"bar"}); err != nil {
if _, err := rdb.Srem("myset", "bar"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

{
expected := []string{"foo"}
if payload, err := redis.Smembers(addr, "myset"); err != nil {
if payload, err := rdb.Smembers("myset"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if !reflect.DeepEqual(payload, expected) {
Expand All @@ -119,27 +121,24 @@ func init() {
}
}

message := redis.RedisParameter{Kind: redis.RedisParameterKindBinary, Val: []byte("message")}
hello := redis.RedisParameter{Kind: redis.RedisParameterKindBinary, Val: []byte("hello")}
if _, err := redis.Execute(addr, "set", []redis.RedisParameter{message, hello}); err != nil {
if _, err := rdb.Execute("set", "message", "hello"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

world := redis.RedisParameter{Kind: redis.RedisParameterKindBinary, Val: []byte(" world")}
if _, err := redis.Execute(addr, "append", []redis.RedisParameter{message, world}); err != nil {
if _, err := rdb.Execute("append", "message", " world"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if payload, err := redis.Execute(addr, "get", []redis.RedisParameter{message}); err != nil {
if payload, err := rdb.Execute("get", "message"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if !reflect.DeepEqual(
payload,
[]redis.RedisResult{redis.RedisResult{
Kind: redis.RedisResultKindBinary,
Val: []byte("hello world"),
[]*redis.Result{{
Kind: redis.ResultKindBinary,
Val: []byte("hello world"),
}}) {
http.Error(w, "unexpected GET result", http.StatusInternalServerError)
return
Expand Down
Loading

0 comments on commit d2322cb

Please sign in to comment.