Skip to content

Commit

Permalink
Move use of gjson into jsonutil.
Browse files Browse the repository at this point in the history
  • Loading branch information
scr-oath committed Dec 2, 2024
1 parent 4af0b37 commit 5c8d751
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/rs/cors v1.11.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.1
github.com/tidwall/gjson v1.17.1
github.com/vrischmann/go-metrics-influxdb v0.1.1
github.com/xeipuuv/gojsonschema v1.2.0
github.com/yudai/gojsondiff v1.0.0
Expand Down Expand Up @@ -70,7 +71,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
Expand Down
7 changes: 1 addition & 6 deletions openrtb_ext/request_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/util/jsonutil"
"github.com/prebid/prebid-server/v3/util/ptrutil"
"github.com/tidwall/gjson"
)

// RequestWrapper wraps the OpenRTB request to provide a storage location for unmarshalled ext fields, so they
Expand Down Expand Up @@ -1279,11 +1278,7 @@ func (re *RegExt) unmarshal(extJson json.RawMessage) error {

gpcJson, hasGPC := re.ext[gpcKey]
if hasGPC && gpcJson != nil {
gpcResult := gjson.ParseBytes(gpcJson)
if gpcResult.Exists() {
gpc := gpcResult.String()
re.gpc = &gpc
}
jsonutil.ParseIntoString(gpcJson, &re.gpc)
}

return nil
Expand Down
15 changes: 15 additions & 0 deletions util/jsonutil/forcestring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package jsonutil

import "github.com/tidwall/gjson"

// ParseIntoString Parse json bytes into a string pointer
func ParseIntoString(b []byte, ppString **string) {
if ppString == nil {
panic("ppString is nil")
}
result := gjson.ParseBytes(b)
if result.Exists() && result.Raw != `null` {
*ppString = new(string)
**ppString = result.String()
}
}
57 changes: 57 additions & 0 deletions util/jsonutil/forcestring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package jsonutil

import (
"testing"

"github.com/prebid/prebid-server/v3/util/ptrutil"
"github.com/stretchr/testify/assert"
)

func Test_ParseIntoString(t *testing.T) {
tests := []struct {
name string
b []byte
want *string
}{
{
name: "empty",
},
{
name: "quoted_1",
b: []byte(`"1"`),
want: ptrutil.ToPtr("1"),
},
{
name: "unquoted_1",
b: []byte(`1`),
want: ptrutil.ToPtr("1"),
},
{
name: "null",
b: []byte(`null`),
},
{
name: "quoted_null",
b: []byte(`"null"`),
want: ptrutil.ToPtr("null"),
},
{
name: "quoted_hello",
b: []byte(`"hello"`),
want: ptrutil.ToPtr("hello"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got *string
ParseIntoString(tt.b, &got)
assert.Equal(t, tt.want, got)
})
}
}

func Test_ParseIntoStringPanic(t *testing.T) {
assert.Panics(t, func() {
ParseIntoString([]byte(`"123"`), nil)
})
}

0 comments on commit 5c8d751

Please sign in to comment.