-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Older devices are sending "gpc": 1 and this can't marshal. (#4070)
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package jsonutil | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/prebid/prebid-server/v3/util/ptrutil" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
// ParseIntoString Parse json bytes into a string pointer | ||
func ParseIntoString(b []byte, ppString **string) error { | ||
if ppString == nil { | ||
return errors.New("ppString is nil") | ||
} | ||
result := gjson.ParseBytes(b) | ||
if result.Exists() && result.Raw != `null` { | ||
*ppString = ptrutil.ToPtr(result.String()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 | ||
wantErr bool | ||
}{ | ||
{ | ||
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 | ||
err := ParseIntoString(tt.b, &got) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_ParseIntoNilStringError(t *testing.T) { | ||
assert.Error(t, ParseIntoString([]byte(`"123"`), nil)) | ||
} |