Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep for TinyGo's v0.28 encoding/json Support #9

Merged
merged 8 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package pdk

import (
"encoding/binary"
"encoding/json"
"strings"

"github.com/valyala/fastjson"
)

type Memory struct {
Expand Down Expand Up @@ -184,11 +183,15 @@ func RemoveVar(key string) {
extism_var_set(mem.offset, 0)
}

type HTTPRequestMeta struct {
Url string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
}

type HTTPRequest struct {
url string
headers map[string]string
method string
body []byte
meta HTTPRequestMeta
body []byte
}

type HTTPResponse struct {
Expand All @@ -211,14 +214,19 @@ func (r HTTPResponse) Status() uint16 {
}

func NewHTTPRequest(method string, url string) *HTTPRequest {
return &HTTPRequest{url: url, headers: nil, method: strings.ToUpper(method), body: nil}
return &HTTPRequest{
meta: HTTPRequestMeta{
Url: url,
Method: strings.ToUpper(method),
},
body: nil}
}

func (r *HTTPRequest) SetHeader(key string, value string) *HTTPRequest {
if r.headers == nil {
r.headers = map[string]string{}
if r.meta.Headers == nil {
r.meta.Headers = make(map[string]string)
}
r.headers[key] = value
r.meta.Headers[key] = value
return r
}

Expand All @@ -228,21 +236,7 @@ func (r *HTTPRequest) SetBody(body []byte) *HTTPRequest {
}

func (r *HTTPRequest) Send() HTTPResponse {
arena := &fastjson.Arena{}
json := arena.NewObject()
headers := arena.NewObject()
if r.headers != nil {
for k, v := range r.headers {
headers.Set(k, arena.NewString(v))
}

json.Set("header", headers)
}
json.Set("url", arena.NewString(r.url))
json.Set("method", arena.NewString(r.method))

var buf []byte
enc := json.MarshalTo(buf)
enc, _ := json.Marshal(r.meta)

req := AllocateBytes(enc)
defer req.Free()
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/extism/go-pdk

go 1.21.0

require github.com/valyala/fastjson v1.6.3
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
Loading