Skip to content

Commit

Permalink
chore: adds support for json unmarshaler in tinygo.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed May 30, 2022
1 parent ff08384 commit 66531fd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
3 changes: 1 addition & 2 deletions bodyprocessors/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package bodyprocessors

import (
"encoding/json"
"fmt"
"io"
"strconv"
Expand Down Expand Up @@ -85,7 +84,7 @@ func jsonToMap(data []byte) (map[string]string, error) {
m map[string]string
err error
)
if err = json.Unmarshal(data, &result); err != nil {
if result, err = jsonUnmarshal(data); err != nil {
return nil, err
}

Expand Down
29 changes: 29 additions & 0 deletions bodyprocessors/json_unmarshaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bodyprocessors

import "encoding/json"

func jsonUnmarshal(data []byte) (interface{}, error) {
var result interface{}
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}

return result, nil
}
26 changes: 26 additions & 0 deletions bodyprocessors/json_unmarshaler_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build tinygo
// +build tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bodyprocessors

import (
"github.com/tidwall/gjson"
)

func jsonUnmarshal(data []byte) (interface{}, error) {
return gjson.Parse(string(data)).Value(), nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
)

require (
github.com/tidwall/gjson v1.14.1
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
Expand Down

0 comments on commit 66531fd

Please sign in to comment.