Skip to content

Commit

Permalink
Prep for TinyGo's v0.28 encoding/json Support (#9)
Browse files Browse the repository at this point in the history
* remove fastjson dependency in prep for TinyGo 0.28

* Delete .idea directory

* remove models

* fix merge conflicts, remove models pkg

* headers json field spelling

* remove .idea dir
  • Loading branch information
syke99 authored Oct 8, 2023
1 parent 75096db commit 5ad87d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
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=

0 comments on commit 5ad87d4

Please sign in to comment.