Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions triple/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ func (t *Type) String() string {
// Covariant checks for given two types A and B, A covariant B if B _is a_ A.
// In other word, A _covariant_ B if B is a prefix of A.
func (t *Type) Covariant(ot *Type) bool {
// TODO: is /ab covariant of /a ?
return strings.HasPrefix(t.String(), ot.String())
if !strings.HasPrefix(t.String(), ot.String()) {
return false
}
// /type/foo is covarian of /type, but /typefoo is not covatiant of /type.
return len(t.String()) == len(ot.String()) || t.String()[len(ot.String())] == '/'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing -1 at?

return len(t.String()) == len(ot.String()) || t.String()[len(ot.String())-1] == '/'

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope.
if B is "/type"
and A is "/type/foo"
we expect "/" in A[5] index.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do'h. Right. I was reading it the other way around. Thx!

}

// ID represents a node ID.
Expand Down
7 changes: 7 additions & 0 deletions triple/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ func TestNewTypeString(t *testing.T) {
if err != nil {
t.Errorf("node.NewType(\"/some/type/a\") should never fail with error %v", err)
}
tAB, err := NewType("/some/type/ab")
if err != nil {
t.Errorf("node.NewType(\"/some/type/ab\") should never fail with error %v", err)
}
if tA.Covariant(tB) {
t.Errorf("node.Covariant: %q should not be market as covariant of %q", tA, tB)
}
if !tB.Covariant(tA) {
t.Errorf("node.Covariant: %q should not be market as covariant of %q", tB, tA)
}
if tAB.Covariant(tB) {
t.Errorf("node.Covariant: %q should not be market as covariant of %q", tAB, tB)
}
}

func TestNewNodeFromString(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion triple/predicate/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package predicate
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -80,7 +81,11 @@ func Parse(s string) (*Predicate, error) {
if idx < 0 {
return nil, fmt.Errorf("predicate.Parse could not find anchor definition in %s", raw)
}
id, ta := raw[1:idx], raw[idx+3:len(raw)-1]
id, ta := raw[0:idx+1], raw[idx+3:len(raw)-1]
id, err := strconv.Unquote(id)
if err != nil {
return nil, fmt.Errorf("predicate.Parse can't unquote id in %s: %v", raw, err)
}
// TODO: if id has \" inside, it should be unquoted.
if ta == "" {
return &Predicate{
Expand Down
26 changes: 23 additions & 3 deletions triple/predicate/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestParse(t *testing.T) {
pretty := fmt.Sprintf("\"bar\"@[%s]", date)
got, err := Parse(pretty)
if err != nil {
t.Errorf("predicate.Parse could not create a predicate for %s with error %v", pretty, err)
t.Fatalf("predicate.Parse could not create a predicate for %s with error %v", pretty, err)
}
if got.Type() != Temporal {
t.Errorf("predicate.Parse should have returned a temporal predicate, instead returned %s", got)
Expand All @@ -121,9 +121,29 @@ func TestParse(t *testing.T) {

imm, err := Parse("\"foo\"@[]")
if err != nil {
t.Errorf("predicate.Parse failed to immutable predicate \"foo\"@[] with error %v", err)
t.Errorf("predicate.Parse failed to parse immutable predicate \"foo\"@[] with error %v", err)
}
if imm.Type() != Immutable || imm.ID() != "foo" {
t.Errorf("predicate.Parse failed to immutable predicate \"foo\"@[]; got %v instead", imm)
t.Errorf("predicate.Parse failed to parse immutable predicate \"foo\"@[]; got %v instead", imm)
}
}

func TestQuotedID(t *testing.T) {
const id = "ba\"r"
const pretty = "\"ba\\\"r\"@[]"
immut, err := NewImmutable(id)
if err != nil {
t.Fatal(err)
}
if got, want := immut.String(), pretty; got != want {
t.Fatalf("predicate.String() = %v; want %v", got, want)
}

immut, err = Parse(pretty)
if err != nil {
t.Fatalf("predicate.Parse failed to parse immutable predicate \"foo\"@[] with error %v", err)
}
if immut.Type() != Immutable || immut.ID() != id {
t.Errorf("predicate.Parse failed to parse immutable predicate %v; got %v instead", pretty, immut)
}
}