From 1b1fb32cf40a4e8044acc025130cd4e8a011c48b Mon Sep 17 00:00:00 2001 From: Miguel Wolf Date: Mon, 2 Dec 2024 14:44:40 +0100 Subject: [PATCH] unescape json values correctly, updated go version and packages --- conditions.go | 10 +++------- conditions_test.go | 15 ++++++++++++++- go.mod | 4 ++-- go.sum | 4 ++-- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/conditions.go b/conditions.go index ba22249..f363ea4 100644 --- a/conditions.go +++ b/conditions.go @@ -2,7 +2,6 @@ package hypermatch import ( "encoding/json" - "strings" ) // ConditionSet represents a rule and consists of one or more items of type Condition @@ -108,12 +107,9 @@ func (p *Pattern) UnmarshalJSON(data []byte) error { } p.Sub = ps default: - d := string(v) - if strings.HasPrefix(d, "\"") { - d = d[1:] - } - if strings.HasSuffix(d, "\"") { - d = d[:len(d)-1] + var d string + if err := json.Unmarshal(v, &d); err != nil { + return err } p.Value = d } diff --git a/conditions_test.go b/conditions_test.go index 7a42bcf..7594ad5 100644 --- a/conditions_test.go +++ b/conditions_test.go @@ -2,9 +2,10 @@ package hypermatch import ( "encoding/json" - "gotest.tools/v3/assert" "log" "testing" + + "gotest.tools/v3/assert" ) func TestConditions_MarshalJSON(t *testing.T) { @@ -62,3 +63,15 @@ func TestConditions_UnmarshalJSON(t *testing.T) { assert.Check(t, c[0].Pattern.Type == PatternEquals) assert.Check(t, c[0].Pattern.Value == "true") } + +func TestConditions_UnmarshalJSON_WithBackslashes(t *testing.T) { + data := []byte(`{"name": {"equals": "te\\st"}}`) + + var c ConditionSet + err := json.Unmarshal(data, &c) + assert.NilError(t, err) + assert.Check(t, len(c) == 1) + assert.Check(t, c[0].Path == "name") + assert.Check(t, c[0].Pattern.Type == PatternEquals) + assert.Check(t, c[0].Pattern.Value == "te\\st") +} diff --git a/go.mod b/go.mod index df8d816..a9af9ed 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/SchwarzIT/hypermatch -go 1.21.0 +go 1.23.3 require gotest.tools/v3 v3.5.1 -require github.com/google/go-cmp v0.5.9 // indirect +require github.com/google/go-cmp v0.6.0 // indirect diff --git a/go.sum b/go.sum index 7dd4ab5..3a593fb 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=