From 8b840758a611e3d1f6949e9aba20aa6f2f7f15ef Mon Sep 17 00:00:00 2001 From: Martin Rode Date: Fri, 8 Nov 2024 16:28:17 +0100 Subject: [PATCH] manifest: load with regular json numbers --- api_testcase.go | 4 ++-- api_testsuite.go | 8 ++++---- pkg/lib/compare/comparer_test.go | 4 ++-- pkg/lib/report/report_test.go | 10 +++++----- pkg/lib/template/util.go | 6 +++--- pkg/lib/util/json.go | 12 +++++++++++- pkg/lib/util/json_test.go | 8 ++++---- 7 files changed, 31 insertions(+), 21 deletions(-) diff --git a/api_testcase.go b/api_testcase.go index 56e17e46..bf4abcb9 100644 --- a/api_testcase.go +++ b/api_testcase.go @@ -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 @@ -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) } diff --git a/api_testsuite.go b/api_testsuite.go index bd3ad3a0..d68b3c1c 100644 --- a/api_testsuite.go +++ b/api_testsuite.go @@ -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()) @@ -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( @@ -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()) diff --git a/pkg/lib/compare/comparer_test.go b/pkg/lib/compare/comparer_test.go index 45194dcd..3a545486 100644 --- a/pkg/lib/compare/comparer_test.go +++ b/pkg/lib/compare/comparer_test.go @@ -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) diff --git a/pkg/lib/report/report_test.go b/pkg/lib/report/report_test.go index 33cfd356..f75e6ba5 100644 --- a/pkg/lib/report/report_test.go +++ b/pkg/lib/report/report_test.go @@ -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{}) @@ -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{}) @@ -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) diff --git a/pkg/lib/template/util.go b/pkg/lib/template/util.go index f899d476..e60f9a3f 100644 --- a/pkg/lib/template/util.go +++ b/pkg/lib/template/util.go @@ -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 } @@ -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 diff --git a/pkg/lib/util/json.go b/pkg/lib/util/json.go index 269bdc07..3bf10f47 100644 --- a/pkg/lib/util/json.go +++ b/pkg/lib/util/json.go @@ -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 ]*#.*$`) @@ -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 diff --git a/pkg/lib/util/json_test.go b/pkg/lib/util/json_test.go index a074c994..1588b597 100644 --- a/pkg/lib/util/json_test.go +++ b/pkg/lib/util/json_test.go @@ -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) } @@ -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) @@ -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 { @@ -270,7 +270,7 @@ func TestCJSONUnmarshalTypeErr(t *testing.T) { var oObject expectedStructure - oErr := Unmarshal( + oErr := UnmarshalWithNumber( []byte(cjsonString), &oObject, )