Skip to content

Commit

Permalink
fix: json use number (#419)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Aug 20, 2021
1 parent 946c5c8 commit 961aa8f
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 24 deletions.
3 changes: 1 addition & 2 deletions executors/amqp/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package amqp

import (
"context"
"encoding/json"
"errors"
"fmt"

Expand Down Expand Up @@ -157,7 +156,7 @@ func consumeMessage(ctx context.Context, recv *amqp.Receiver) (msgString string,
return "", nil, fmt.Errorf("consuming message: %w", err)
}

if err := json.Unmarshal(msg.GetData(), &msgJSON); err != nil {
if err := venom.JSONUnmarshal(msg.GetData(), &msgJSON); err != nil {
return string(msg.GetData()), nil, nil
}

Expand Down
5 changes: 2 additions & 3 deletions executors/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exec
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -205,12 +204,12 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
result.Systemerr = venom.RemoveNotPrintableChar(strings.TrimRight(result.Systemerr, "\n"))

var outJSON interface{}
if err := json.Unmarshal([]byte(result.Systemout), &outJSON); err == nil {
if err := venom.JSONUnmarshal([]byte(result.Systemout), &outJSON); err == nil {
result.SystemoutJSON = outJSON
}

var errJSON interface{}
if err := json.Unmarshal([]byte(result.Systemerr), &errJSON); err == nil {
if err := venom.JSONUnmarshal([]byte(result.Systemerr), &errJSON); err == nil {
result.SystemerrJSON = errJSON
}

Expand Down
8 changes: 4 additions & 4 deletions executors/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro

// parse stdout as JSON
var outJSONArray []interface{}
if err := json.Unmarshal([]byte(result.Systemout), &outJSONArray); err != nil {
if err := venom.JSONUnmarshal([]byte(result.Systemout), &outJSONArray); err != nil {
outJSONMap := map[string]interface{}{}
if err2 := json.Unmarshal([]byte(result.Systemout), &outJSONMap); err2 == nil {
if err2 := venom.JSONUnmarshal([]byte(result.Systemout), &outJSONMap); err2 == nil {
result.SystemoutJSON = outJSONMap
}
} else {
Expand All @@ -206,9 +206,9 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro

// parse stderr output as JSON
var errJSONArray []interface{}
if err := json.Unmarshal([]byte(result.Systemout), &errJSONArray); err != nil {
if err := venom.JSONUnmarshal([]byte(result.Systemout), &errJSONArray); err != nil {
errJSONMap := map[string]interface{}{}
if err2 := json.Unmarshal([]byte(result.Systemout), &errJSONMap); err2 == nil {
if err2 := venom.JSONUnmarshal([]byte(result.Systemout), &errJSONMap); err2 == nil {
result.SystemoutJSON = errJSONMap
}
} else {
Expand Down
11 changes: 5 additions & 6 deletions executors/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kafka

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -193,7 +192,7 @@ func (e Executor) produceMessages(workdir string) error {
return err
}
messages := []Message{}
err = json.Unmarshal(content, &messages)
err = venom.JSONUnmarshal(content, &messages)
if err != nil {
return err
}
Expand Down Expand Up @@ -450,10 +449,10 @@ func convertFromMessage2JSON(message *Message, msgJSON *MessageJSON) {
// unmarshall the message.Value
listMessageJSON := []MessageJSON{}
// try to unmarshall into an array
if err := json.Unmarshal([]byte(message.Value), &listMessageJSON); err != nil {
if err := venom.JSONUnmarshal([]byte(message.Value), &listMessageJSON); err != nil {
// try to unmarshall into a map
mapMessageJSON := map[string]interface{}{}
if err2 := json.Unmarshal([]byte(message.Value), &mapMessageJSON); err2 != nil {
if err2 := venom.JSONUnmarshal([]byte(message.Value), &mapMessageJSON); err2 != nil {
// try to unmarshall into a string
msgJSON.Value = message.Value
} else {
Expand All @@ -466,10 +465,10 @@ func convertFromMessage2JSON(message *Message, msgJSON *MessageJSON) {
// unmarshall the message.Key
listMessageJSON = []MessageJSON{}
// try to unmarshall into an array
if err := json.Unmarshal([]byte(message.Key), &listMessageJSON); err != nil {
if err := venom.JSONUnmarshal([]byte(message.Key), &listMessageJSON); err != nil {
// try to unmarshall into a map
mapMessageJSON := map[string]interface{}{}
if err2 := json.Unmarshal([]byte(message.Key), &mapMessageJSON); err2 != nil {
if err2 := venom.JSONUnmarshal([]byte(message.Key), &mapMessageJSON); err2 != nil {
// try to unmarshall into a string
msgJSON.Key = message.Key
} else {
Expand Down
5 changes: 2 additions & 3 deletions executors/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mqtt

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -264,9 +263,9 @@ func (e Executor) consumeMessages(ctx context.Context) (messages []interface{},
venom.Debug(ctx, "message received. topic: %s len(%d), %s", t, len(m), s)

var bodyJSONArray []interface{}
if err := json.Unmarshal(m, &bodyJSONArray); err != nil {
if err := venom.JSONUnmarshal(m, &bodyJSONArray); err != nil {
bodyJSONMap := map[string]interface{}{}
err := json.Unmarshal(m, &bodyJSONMap)
err := venom.JSONUnmarshal(m, &bodyJSONMap)
if err != nil {
venom.Debug(ctx, "unable to decode message as json")
}
Expand Down
2 changes: 1 addition & 1 deletion executors/ovhapi/ovhapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (e Executor) getRequestBody(workdir string) (res interface{}, err error) {
}
if len(bytes) > 0 {
res = new(interface{})
err = json.Unmarshal(bytes, res)
err = venom.JSONUnmarshal(bytes, res)
return
}
return nil, nil
Expand Down
5 changes: 2 additions & 3 deletions executors/rabbitmq/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rabbitmq

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -295,9 +294,9 @@ func (e Executor) consumeMessages(ctx context.Context) ([]string, []interface{},
body = append(body, string(msg.Body))

bodyJSONArray := []interface{}{}
if err := json.Unmarshal(msg.Body, &bodyJSONArray); err != nil {
if err := venom.JSONUnmarshal(msg.Body, &bodyJSONArray); err != nil {
bodyJSONMap := map[string]interface{}{}
json.Unmarshal(msg.Body, &bodyJSONMap) //nolint
venom.JSONUnmarshal(msg.Body, &bodyJSONMap) //nolint
bodyJSON = append(bodyJSON, bodyJSONMap)
} else {
bodyJSON = append(bodyJSON, bodyJSONArray)
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,29 @@ github.com/Azure/azure-amqp-common-go/v2 v2.1.0/go.mod h1:R8rea+gJRuJR6QxTir/XuE
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
github.com/Azure/azure-sdk-for-go v29.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go v30.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go v51.1.0+incompatible h1:7uk6GWtUqKg6weLv2dbKnzwb0ml1Qn70AdtRccZ543w=
github.com/Azure/azure-sdk-for-go v51.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-service-bus-go v0.9.1/go.mod h1:yzBx6/BUGfjfeqbRZny9AQIbIe3AcV9WZbAdpkoXOa0=
github.com/Azure/azure-storage-blob-go v0.8.0/go.mod h1:lPI3aLPpuLTeUwh1sViKXFxwl2B6teiRqI0deQUvsw0=
github.com/Azure/go-amqp v0.13.7 h1:ukcCtx138ZmOfHbdALuh9yoJhGtOY3+yaKApfzNvhSk=
github.com/Azure/go-amqp v0.13.7/go.mod h1:wbpCKA8tR5MLgRyIu+bb+S6ECdIDdYJ0NlpFE9xsBPI=
github.com/Azure/go-autorest v12.0.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM=
github.com/Azure/go-autorest/autorest v0.11.18/go.mod h1:dSiJPy22c3u0OtOKDNttNgqpNFY/GeWa7GH/Pz56QRA=
github.com/Azure/go-autorest/autorest/adal v0.9.13 h1:Mp5hbtOePIzM8pJVRa3YLrWWmZtoxRXqUEzCfJt3+/Q=
github.com/Azure/go-autorest/autorest/adal v0.9.13/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M=
github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
github.com/Azure/go-autorest/autorest/to v0.4.0 h1:oXVqrxakqqV1UZdSazDOPOLvOIz+XA683u8EctwboHk=
github.com/Azure/go-autorest/autorest/to v0.4.0/go.mod h1:fE8iZBn7LQR7zH/9XU2NcPR4o9jEImooCeWJcYV/zLE=
github.com/Azure/go-autorest/autorest/validation v0.3.1 h1:AgyqjAd94fwNAoTjl/WQXg4VvFeRFpO+UhNyRXqF1ac=
github.com/Azure/go-autorest/autorest/validation v0.3.1/go.mod h1:yhLgjC0Wda5DYXl6JAsWyUe4KVNffhoDhG0zVzUMo3E=
github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+ZtXWSmf4Tg=
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down Expand Up @@ -170,6 +179,7 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand Down
4 changes: 2 additions & 2 deletions types_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (ux UserExecutor) ZeroValueResult() interface{} {
}

result := make(map[string]interface{})
err = json.Unmarshal(outputS, &result)
err = JSONUnmarshal(outputS, &result)
if err != nil {
return ""
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (v *Venom) RunUserExecutor(ctx context.Context, runner ExecutorRunner, tcIn

for k, v := range resultS {
var outJSON interface{}
if err := json.Unmarshal([]byte(v), &outJSON); err == nil {
if err := JSONUnmarshal([]byte(v), &outJSON); err == nil {
result[k+"json"] = outJSON
}
}
Expand Down
8 changes: 8 additions & 0 deletions venom.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package venom

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -300,3 +302,9 @@ func AllVarsFromCtx(ctx context.Context) H {
}
return res
}

func JSONUnmarshal(btes []byte, i interface{}) error {
var d = json.NewDecoder(bytes.NewReader(btes))
d.UseNumber()
return d.Decode(i)
}

0 comments on commit 961aa8f

Please sign in to comment.