Skip to content

Commit

Permalink
unescape json values correctly, updated go version and packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Wolf committed Dec 2, 2024
1 parent 185b236 commit 1b1fb32
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
10 changes: 3 additions & 7 deletions conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hypermatch

import (
"encoding/json"
"strings"
)

// ConditionSet represents a rule and consists of one or more items of type Condition
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 1b1fb32

Please sign in to comment.