Skip to content

Commit

Permalink
Modifed Parse to handle Blank Nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
apr94 committed May 23, 2017
1 parent f778287 commit 13f891a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
46 changes: 30 additions & 16 deletions triple/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"github.com/pborman/uuid"
)

const (
slash = byte('/')
underscore = byte('_')
)

// Type describes the type of the node.
type Type string

Expand Down Expand Up @@ -74,25 +79,34 @@ func (n *Node) String() string {
return fmt.Sprintf("%s<%s>", n.t.String(), n.id.String())
}

// Parse returns a node given a pretty printed representation of Node.
// Parse returns a node given a pretty printed representation of a Node or a BlankNode.
func Parse(s string) (*Node, error) {
raw := strings.TrimSpace(s)
idx := strings.Index(raw, "<")
if idx < 0 {
return nil, fmt.Errorf("node.Parser: invalid format, could not find ID in %v", raw)
}
t, err := NewType(raw[:idx])
if err != nil {
return nil, fmt.Errorf("node.Parser: invalid type %q, %v", raw[:idx], err)
}
if raw[len(raw)-1] != '>' {
return nil, fmt.Errorf("node.Parser: pretty printing should finish with '>' in %q", raw)
}
id, err := NewID(raw[idx+1 : len(raw)-1])
if err != nil {
return nil, fmt.Errorf("node.Parser: invalid ID in %q, %v", raw, err)
switch raw[0] {
case slash:
idx := strings.Index(raw, "<")
if idx < 0 {
return nil, fmt.Errorf("node.Parser: invalid format, could not find ID in %v", raw)
}
t, err := NewType(raw[:idx])
if err != nil {
return nil, fmt.Errorf("node.Parser: invalid type %q, %v", raw[:idx], err)
}
if raw[len(raw)-1] != '>' {
return nil, fmt.Errorf("node.Parser: pretty printing should finish with '>' in %q", raw)
}
id, err := NewID(raw[idx+1 : len(raw)-1])
if err != nil {
return nil, fmt.Errorf("node.Parser: invalid ID in %q, %v", raw, err)
}
return NewNode(t, id), nil
case underscore:
id := ID(raw[2:len(raw)])
t := Type("/_")
return NewNode(&t, &id), nil
default:
return nil, fmt.Errorf("node.Parser: node representation should start with '/' or '_' in %v", raw)
}
return NewNode(t, id), nil
}

// Covariant checks if the types of two nodes is covariant.
Expand Down
44 changes: 32 additions & 12 deletions triple/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,42 @@ func TestParse(t *testing.T) {
id: "123",
v: true,
},
{
s: "_:v1",
t: "/_",
id: "v1",
v: true,
},
// Invalid text nodes.
{
s: "/foo<123",
t: "",
id: "",
v: false,
},
{
s: "foo<123>",
t: "",
id: "",
v: false,
},
}
for _, tc := range table {
n, err := Parse(tc.s)
if tc.v && err != nil {
t.Errorf("node.Parse: failed to parse %q; %v", tc.s, err)
}
if !tc.v && err == nil {
t.Errorf("node.Parse: failed to reject invalid %q", tc.s)
continue
}
if got, want := n.Type().String(), tc.t; got != want {
t.Errorf("node.Parse: failed to return proper type; got %q, want %q", got, want)
}
if got, want := n.ID().String(), tc.id; got != want {
t.Errorf("node.Parse: failed to return proper id; got %q, want %q", got, want)
if tc.v {
if err != nil {
t.Errorf("node.Parse: failed to parse %q; %v", tc.s, err)
}
if got, want := n.Type().String(), tc.t; got != want {
t.Errorf("node.Parse: failed to return proper type; got %q, want %q", got, want)
}
if got, want := n.ID().String(), tc.id; got != want {
t.Errorf("node.Parse: failed to return proper id; got %q, want %q", got, want)
}
} else {
if err == nil {
t.Errorf("node.Parse: failed to reject invalid %q", tc.s)
}
}
}
}
Expand Down

0 comments on commit 13f891a

Please sign in to comment.