Define Len() for JSON Map.

R=rsc
APPROVED=rsc
DELTA=6  (5 added, 0 deleted, 1 changed)
OCL=28398
CL=28430
This commit is contained in:
David Symonds 2009-05-07 15:09:32 -07:00
parent 9ab8129ebe
commit abdf4853a7
2 changed files with 6 additions and 1 deletions

View File

@ -33,7 +33,7 @@ type Json interface {
Bool() bool; // boolean (BoolKind)
Get(s string) Json; // field lookup (MapKind)
Elem(i int) Json; // element lookup (ArrayKind)
Len() int; // length (ArrayKind)
Len() int; // length (ArrayKind, MapKind)
}
// JsonToString returns the textual JSON syntax representation
@ -112,6 +112,7 @@ func (j *_Bool) String() string {
type _Map struct { m map[string]Json; _Null }
func (j *_Map) Kind() int { return MapKind }
func (j *_Map) Len() int { return len(j.m) }
func (j *_Map) Get(s string) Json {
if j.m == nil {
return Null

View File

@ -64,6 +64,10 @@ func TestJsonMap(t *testing.T) {
if mapv == nil {
t.Fatalf("StringToJson(%#q) => nil, %v, %v", mapstr, ok, errtok);
}
if cnt := mapv.Len(); cnt != len(jsontests) {
t.Errorf("StringToJson(%#q).Len() => %v, want %v", mapstr, cnt,
len(jsontests));
}
for k,v := range values {
if v1 := mapv.Get(k); !Equal(v1, v) {
t.Errorf("MapTest: Walk(%#q) => %v, want %v", k, v1, v);