Skip to content

Commit

Permalink
manifest: load with regular json numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrode committed Nov 8, 2024
1 parent 82a63bd commit 8b84075
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions api_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (testCase Case) loadRequestSerialization() (api.Request, error) {
if err != nil {
return spec, fmt.Errorf("error marshaling req: %s", err)
}
err = util.Unmarshal(specBytes, &spec)
err = util.UnmarshalWithNumber(specBytes, &spec)
spec.ManifestDir = testCase.manifestDir
spec.DataStore = testCase.dataStore

Expand Down Expand Up @@ -565,7 +565,7 @@ func (testCase Case) loadResponseSerialization(genJSON any) (spec api.ResponseSe
return spec, fmt.Errorf("error marshaling res: %s", err)
}

err = util.Unmarshal(specBytes, &spec)
err = util.UnmarshalWithNumber(specBytes, &spec)
if err != nil {
return spec, fmt.Errorf("error unmarshaling res: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ func (ats *Suite) testGoroutine(

// Build list of test cases
var testCases []json.RawMessage
err = util.Unmarshal(testRendered, &testCases)
err = util.UnmarshalWithNumber(testRendered, &testCases)
if err != nil {
// Input could not be deserialized into list, try to deserialize into single object
var singleTest json.RawMessage
err = util.Unmarshal(testRendered, &singleTest)
err = util.UnmarshalWithNumber(testRendered, &singleTest)
if err != nil {
// Malformed json
r.SaveToReportLog(err.Error())
Expand All @@ -342,7 +342,7 @@ func (ats *Suite) testGoroutine(
// If testCase can be unmarshalled as string, we may have a
// reference to another test using @ notation at hand
var testCaseStr string
err = util.Unmarshal(testCase, &testCaseStr)
err = util.UnmarshalWithNumber(testCase, &testCaseStr)
if err == nil && util.IsPathSpec(testCaseStr) {
// Recurse if the testCase points to another file using @ notation
success = ats.parseAndRunTest(
Expand Down Expand Up @@ -382,7 +382,7 @@ func (ats *Suite) runLiteralTest(
r.SetName(testFilePath)

var test Case
jErr := util.Unmarshal(tc.CaseByte, &test)
jErr := util.UnmarshalWithNumber(tc.CaseByte, &test)
if jErr != nil {

r.SaveToReportLog(jErr.Error())
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/compare/comparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ func TestTrivialJsonComparer(t *testing.T) {
var json1, json2 any
for _, td := range trivialComparerTestData {
t.Run(td.name, func(t *testing.T) {
util.Unmarshal([]byte(td.want), &json1)
util.Unmarshal([]byte(td.have), &json2)
util.UnmarshalWithNumber([]byte(td.want), &json1)
util.UnmarshalWithNumber([]byte(td.have), &json2)
tjcMatch, err := JsonEqual(json1, json2, ComparisonContext{})
if err != nil {
t.Fatal("Error occurred: ", err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/lib/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func TestReportGetJSONResult(t *testing.T) {

var expJ, realJ any

util.Unmarshal(jsonResult, &realJ)
util.Unmarshal(expResult, &expJ)
util.UnmarshalWithNumber(jsonResult, &realJ)
util.UnmarshalWithNumber(expResult, &expJ)

equal, _ := compare.JsonEqual(expJ, realJ, compare.ComparisonContext{})

Expand Down Expand Up @@ -138,8 +138,8 @@ func TestReportGetJUnitResult(t *testing.T) {

var expJ, realJ any

util.Unmarshal(expJBytes, &expJ)
util.Unmarshal(realJBytes, &realJ)
util.UnmarshalWithNumber(expJBytes, &expJ)
util.UnmarshalWithNumber(realJBytes, &realJ)

equal, _ := compare.JsonEqual(expJ, realJ, compare.ComparisonContext{})

Expand Down Expand Up @@ -220,7 +220,7 @@ func TestReportGetStatsResult(t *testing.T) {

jsonResult := r.GetTestResult(ParseJSONStatsResult)
var statsRep statsReport
util.Unmarshal(jsonResult, &statsRep)
util.UnmarshalWithNumber(jsonResult, &statsRep)

if statsRep.Version != r.Version {
t.Fatalf("Got version %s, expected %s", statsRep.Version, r.Version)
Expand Down
6 changes: 3 additions & 3 deletions pkg/lib/template/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (path
var jsonObject util.JsonObject
var jsonArray util.JsonArray

if err = util.Unmarshal(requestBytes, &jsonObject); err != nil {
if err = util.Unmarshal(requestBytes, &jsonArray); err == nil {
if err = util.UnmarshalWithNumber(requestBytes, &jsonObject); err != nil {
if err = util.UnmarshalWithNumber(requestBytes, &jsonArray); err == nil {

return pathSpec, jsonArray, nil
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func LoadManifestDataAsRawJson(data any, manifestDir string) (pathSpec *util.Pat
if err != nil {
return nil, res, fmt.Errorf("error marshaling: %s", err)
}
if err = util.Unmarshal(jsonMar, &res); err != nil {
if err = util.UnmarshalWithNumber(jsonMar, &res); err != nil {
return nil, res, fmt.Errorf("error unmarshalling: %s", err)
}
return nil, res, nil
Expand Down
12 changes: 11 additions & 1 deletion pkg/lib/util/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ func init() {
coloredError = true
}

func UnmarshalWithNumber(input []byte, output any) error {
return unmarshal(input, output, true)
}

func Unmarshal(input []byte, output any) error {
return unmarshal(input, output, false)
}

func unmarshal(input []byte, output any, withNumber bool) error {

// Remove # comments from template
var commentRegex = regexp.MustCompile(`(?m)^[\t ]*#.*$`)
Expand All @@ -33,7 +41,9 @@ func Unmarshal(input []byte, output any) error {
tmplBytes = jsonc.ToJSON(tmplBytes)

dec := json.NewDecoder(bytes.NewReader(tmplBytes))
dec.UseNumber()
if withNumber {
dec.UseNumber()
}
dec.DisallowUnknownFields()

// unmarshal into object
Expand Down
8 changes: 4 additions & 4 deletions pkg/lib/util/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ welt:1

for _, v := range testCases {
var out JsonObject
oErr := Unmarshal([]byte(v.iJson), &out)
oErr := UnmarshalWithNumber([]byte(v.iJson), &out)

go_test_utils.AssertErrorEquals(t, oErr, v.eError)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestRemoveComments(t *testing.T) {

for _, v := range testCases {
var out JsonObject
Unmarshal([]byte(v.iJson), &out)
UnmarshalWithNumber([]byte(v.iJson), &out)
for k, v := range v.eOut {
if out[k] != v {
t.Errorf("[%s] Have '%v' != '%f' want", k, out[k], v)
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestCJSONUnmarshalSyntaxErr(t *testing.T) {

for _, v := range testCases {
oObject := JsonObject{}
oErr := Unmarshal([]byte(v.cjsonString), &oObject)
oErr := UnmarshalWithNumber([]byte(v.cjsonString), &oObject)

go_test_utils.AssertErrorEquals(t, oErr, v.eError)
if oErr == nil {
Expand All @@ -270,7 +270,7 @@ func TestCJSONUnmarshalTypeErr(t *testing.T) {

var oObject expectedStructure

oErr := Unmarshal(
oErr := UnmarshalWithNumber(
[]byte(cjsonString),
&oObject,
)
Expand Down

0 comments on commit 8b84075

Please sign in to comment.