diff --git a/graph/bolt/all_iterator.go b/graph/bolt/all_iterator.go index 5d8bf89..4cd017e 100644 --- a/graph/bolt/all_iterator.go +++ b/graph/bolt/all_iterator.go @@ -182,7 +182,7 @@ func (it *AllIterator) Describe() graph.Description { size, _ := it.Size() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Size: size, Direction: it.dir, diff --git a/graph/bolt/iterator.go b/graph/bolt/iterator.go index 8709ebe..ec298de 100644 --- a/graph/bolt/iterator.go +++ b/graph/bolt/iterator.go @@ -294,7 +294,7 @@ func (it *Iterator) Describe() graph.Description { return graph.Description{ UID: it.UID(), Name: it.qs.NameOf(&Token{it.bucket, it.checkID}), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Size: it.size, Direction: it.dir, diff --git a/graph/iterator.go b/graph/iterator.go index 3a74dc6..4442ea5 100644 --- a/graph/iterator.go +++ b/graph/iterator.go @@ -17,11 +17,12 @@ package graph // Define the general iterator interface. import ( - "github.com/google/cayley/quad" + "fmt" "strings" "sync" "github.com/barakmich/glog" + "github.com/google/cayley/quad" ) type Tagger struct { @@ -144,7 +145,7 @@ type Iterator interface { type Description struct { UID uint64 `json:",omitempty"` Name string `json:",omitempty"` - Type string `json:",omitempty"` + Type Type `json:",omitempty"` Tags []string `json:",omitempty"` Size int64 `json:",omitempty"` Direction quad.Direction `json:",omitempty"` @@ -267,9 +268,27 @@ func (t Type) String() string { return types[t] } +func (t *Type) MarshalText() (text []byte, err error) { + if *t < 0 || int(*t) >= len(types) { + return nil, fmt.Errorf("graph: illegal iterator type: %d", *t) + } + return []byte(types[*t]), nil +} + +func (t *Type) UnmarshalText(text []byte) error { + s := string(text) + for i, c := range types[1:] { + if c == s { + *t = Type(i + 1) + return nil + } + } + return fmt.Errorf("graph: unknown iterator label: %q", text) +} + type StatsContainer struct { UID uint64 - Type string + Type Type IteratorStats SubIts []StatsContainer } @@ -277,7 +296,7 @@ type StatsContainer struct { func DumpStats(it Iterator) StatsContainer { var out StatsContainer out.IteratorStats = it.Stats() - out.Type = it.Type().String() + out.Type = it.Type() out.UID = it.UID() for _, sub := range it.SubIterators() { out.SubIts = append(out.SubIts, DumpStats(sub)) diff --git a/graph/iterator/all_iterator.go b/graph/iterator/all_iterator.go index c89c44c..d937ce4 100644 --- a/graph/iterator/all_iterator.go +++ b/graph/iterator/all_iterator.go @@ -81,7 +81,7 @@ func (it *Int64) TagResults(dst map[string]graph.Value) { func (it *Int64) Describe() graph.Description { return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), } } diff --git a/graph/iterator/and_iterator.go b/graph/iterator/and_iterator.go index 64c68f0..3764600 100644 --- a/graph/iterator/and_iterator.go +++ b/graph/iterator/and_iterator.go @@ -116,7 +116,7 @@ func (it *And) Describe() graph.Description { primary := it.primaryIt.Describe() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Iterator: &primary, Iterators: subIts, diff --git a/graph/iterator/fixed_iterator.go b/graph/iterator/fixed_iterator.go index 93d31b4..4a8afb9 100644 --- a/graph/iterator/fixed_iterator.go +++ b/graph/iterator/fixed_iterator.go @@ -107,7 +107,7 @@ func (it *Fixed) Describe() graph.Description { return graph.Description{ UID: it.UID(), Name: value, - Type: it.Type().String(), + Type: it.Type(), Tags: fixed, Size: int64(len(it.values)), } diff --git a/graph/iterator/hasa_iterator.go b/graph/iterator/hasa_iterator.go index fed6469..3788b40 100644 --- a/graph/iterator/hasa_iterator.go +++ b/graph/iterator/hasa_iterator.go @@ -131,7 +131,7 @@ func (it *HasA) Describe() graph.Description { primary := it.primaryIt.Describe() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Direction: it.dir, Iterator: &primary, diff --git a/graph/iterator/iterator.go b/graph/iterator/iterator.go index e1c98c9..954ddba 100644 --- a/graph/iterator/iterator.go +++ b/graph/iterator/iterator.go @@ -80,7 +80,7 @@ func (it *Null) Optimize() (graph.Iterator, bool) { return it, false } func (it *Null) Describe() graph.Description { return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), } } diff --git a/graph/iterator/linksto_iterator.go b/graph/iterator/linksto_iterator.go index 5952cd1..99a8df5 100644 --- a/graph/iterator/linksto_iterator.go +++ b/graph/iterator/linksto_iterator.go @@ -109,7 +109,7 @@ func (it *LinksTo) Describe() graph.Description { primary := it.primaryIt.Describe() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Direction: it.dir, Iterator: &primary, } diff --git a/graph/iterator/materialize_iterator.go b/graph/iterator/materialize_iterator.go index 7adb515..00b2102 100644 --- a/graph/iterator/materialize_iterator.go +++ b/graph/iterator/materialize_iterator.go @@ -119,7 +119,7 @@ func (it *Materialize) Describe() graph.Description { primary := it.subIt.Describe() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Size: int64(len(it.values)), Iterator: &primary, diff --git a/graph/iterator/optional_iterator.go b/graph/iterator/optional_iterator.go index 5622f59..bc65acc 100644 --- a/graph/iterator/optional_iterator.go +++ b/graph/iterator/optional_iterator.go @@ -121,7 +121,7 @@ func (it *Optional) Describe() graph.Description { primary := it.subIt.Describe() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Iterator: &primary, } diff --git a/graph/iterator/or_iterator.go b/graph/iterator/or_iterator.go index 805ede0..6c58238 100644 --- a/graph/iterator/or_iterator.go +++ b/graph/iterator/or_iterator.go @@ -117,7 +117,7 @@ func (it *Or) Describe() graph.Description { } return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Iterators: subIts, } diff --git a/graph/iterator/value_comparison_iterator.go b/graph/iterator/value_comparison_iterator.go index dae9956..627b853 100644 --- a/graph/iterator/value_comparison_iterator.go +++ b/graph/iterator/value_comparison_iterator.go @@ -192,7 +192,7 @@ func (it *Comparison) Describe() graph.Description { primary := it.subIt.Describe() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Iterator: &primary, } } diff --git a/graph/iterator_test.go b/graph/iterator_test.go new file mode 100644 index 0000000..3d6f14e --- /dev/null +++ b/graph/iterator_test.go @@ -0,0 +1,45 @@ +// Copyright 2014 The Cayley Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package graph + +import ( + "testing" +) + +func TestTypeEncodingRoundtrip(t *testing.T) { + for i := Invalid; i <= Materialize; i++ { + text, err := i.MarshalText() + if err != nil { + t.Errorf("Unexpected error when marshaling %s: %v", i, err) + } + if string(text) != i.String() { + t.Errorf("Unexpected MarshalText result, got:%q expect:%q", i, text) + } + var m Type + err = m.UnmarshalText(text) + if i == Invalid { + if err == nil || err.Error() != `graph: unknown iterator label: "invalid"` { + t.Errorf("Unexpected error when unmarshaling %q: %v", text, err) + } + } else { + if err != nil { + t.Errorf("Unexpected error when unmarshaling %q: %v", text, err) + } + } + if m != i { + t.Errorf("Unexpected UnmarshalText result, got:Type(%d) expect:Type(%d)", m, i) + } + } +} diff --git a/graph/leveldb/all_iterator.go b/graph/leveldb/all_iterator.go index 346a973..515133f 100644 --- a/graph/leveldb/all_iterator.go +++ b/graph/leveldb/all_iterator.go @@ -161,7 +161,7 @@ func (it *AllIterator) Describe() graph.Description { size, _ := it.Size() return graph.Description{ UID: it.UID(), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Size: size, Direction: it.dir, diff --git a/graph/leveldb/iterator.go b/graph/leveldb/iterator.go index 8bdd86d..6ccd3fe 100644 --- a/graph/leveldb/iterator.go +++ b/graph/leveldb/iterator.go @@ -253,7 +253,7 @@ func (it *Iterator) Describe() graph.Description { return graph.Description{ UID: it.UID(), Name: it.qs.NameOf(Token(it.checkID)), - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Size: size, Direction: it.dir, diff --git a/graph/memstore/iterator.go b/graph/memstore/iterator.go index fb1047e..d79e6c8 100644 --- a/graph/memstore/iterator.go +++ b/graph/memstore/iterator.go @@ -162,7 +162,7 @@ func (it *Iterator) Describe() graph.Description { return graph.Description{ UID: it.UID(), Name: it.data, - Type: it.Type().String(), + Type: it.Type(), Tags: it.tags.Tags(), Size: size, } diff --git a/graph/mongo/iterator.go b/graph/mongo/iterator.go index 40694fb..32bc472 100644 --- a/graph/mongo/iterator.go +++ b/graph/mongo/iterator.go @@ -217,7 +217,7 @@ func (it *Iterator) Describe() graph.Description { return graph.Description{ UID: it.UID(), Name: fmt.Sprintf("%s/%s", it.name, it.hash), - Type: it.Type().String(), + Type: it.Type(), Size: size, } }