Skip to content

Commit 0447778

Browse files
committed
Unquote string correctly.
1 parent 702458c commit 0447778

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

triple/predicate/predicate.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package predicate
1818
import (
1919
"encoding/base64"
2020
"fmt"
21+
"strconv"
2122
"strings"
2223
"time"
2324
)
@@ -80,7 +81,11 @@ func Parse(s string) (*Predicate, error) {
8081
if idx < 0 {
8182
return nil, fmt.Errorf("predicate.Parse could not find anchor definition in %s", raw)
8283
}
83-
id, ta := raw[1:idx], raw[idx+3:len(raw)-1]
84+
id, ta := raw[0:idx+1], raw[idx+3:len(raw)-1]
85+
id, err := strconv.Unquote(id)
86+
if err != nil {
87+
return nil, fmt.Errorf("predicate.Parse can't unquote id in %s: %v", raw, err)
88+
}
8489
// TODO: if id has \" inside, it should be unquoted.
8590
if ta == "" {
8691
return &Predicate{

triple/predicate/predicate_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestParse(t *testing.T) {
106106
pretty := fmt.Sprintf("\"bar\"@[%s]", date)
107107
got, err := Parse(pretty)
108108
if err != nil {
109-
t.Errorf("predicate.Parse could not create a predicate for %s with error %v", pretty, err)
109+
t.Fatalf("predicate.Parse could not create a predicate for %s with error %v", pretty, err)
110110
}
111111
if got.Type() != Temporal {
112112
t.Errorf("predicate.Parse should have returned a temporal predicate, instead returned %s", got)
@@ -121,9 +121,29 @@ func TestParse(t *testing.T) {
121121

122122
imm, err := Parse("\"foo\"@[]")
123123
if err != nil {
124-
t.Errorf("predicate.Parse failed to immutable predicate \"foo\"@[] with error %v", err)
124+
t.Errorf("predicate.Parse failed to parse immutable predicate \"foo\"@[] with error %v", err)
125125
}
126126
if imm.Type() != Immutable || imm.ID() != "foo" {
127-
t.Errorf("predicate.Parse failed to immutable predicate \"foo\"@[]; got %v instead", imm)
127+
t.Errorf("predicate.Parse failed to parse immutable predicate \"foo\"@[]; got %v instead", imm)
128+
}
129+
}
130+
131+
func TestQuotedID(t *testing.T) {
132+
const id = "ba\"r"
133+
const pretty = "\"ba\\\"r\"@[]"
134+
immut, err := NewImmutable(id)
135+
if err != nil {
136+
t.Fatal(err)
137+
}
138+
if got, want := immut.String(), pretty; got != want {
139+
t.Fatalf("predicate.String() = %v; want %v", got, want)
140+
}
141+
142+
immut, err = Parse(pretty)
143+
if err != nil {
144+
t.Fatalf("predicate.Parse failed to parse immutable predicate \"foo\"@[] with error %v", err)
145+
}
146+
if immut.Type() != Immutable || immut.ID() != id {
147+
t.Errorf("predicate.Parse failed to parse immutable predicate %v; got %v instead", pretty, immut)
128148
}
129149
}

0 commit comments

Comments
 (0)