From e37f81b49cda47d30b47f0ed97c4a48f0ab6ba72 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 7 Jul 2009 11:04:34 -0700 Subject: [PATCH] template: use new reflect interface (CL 31107) R=r DELTA=16 (3 added, 1 deleted, 12 changed) OCL=31121 CL=31288 --- src/pkg/template/template.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/pkg/template/template.go b/src/pkg/template/template.go index d44756b2d2..6db58442ac 100644 --- a/src/pkg/template/template.go +++ b/src/pkg/template/template.go @@ -569,12 +569,12 @@ func (st *state) findVar(s string) reflect.Value { return st.data } data := reflect.Indirect(st.data); - typ, ok := data.Type().(reflect.StructType); + typ, ok := data.Type().(*reflect.StructType); if ok { - for i := 0; i < typ.Len(); i++ { - name, ftyp, tag, offset := typ.Field(i); - if name == s { - return data.(reflect.StructValue).Field(i) + for i := 0; i < typ.NumField(); i++ { + f := typ.Field(i); + if f.Name == s { + return data.(*reflect.StructValue).Field(i) } } } @@ -587,13 +587,15 @@ func empty(v reflect.Value, indirect_ok bool) bool { if v == nil { return true } - switch v.Type().Kind() { - case reflect.StringKind: - return v.(reflect.StringValue).Get() == ""; - case reflect.StructKind: + switch v := v.(type) { + case *reflect.StringValue: + return v.Get() == ""; + case *reflect.StructValue: return false; - case reflect.ArrayKind: - return v.(reflect.ArrayValue).Len() == 0; + case *reflect.ArrayValue: + return v.Len() == 0; + case *reflect.SliceValue: + return v.Len() == 0; } return true; } @@ -701,7 +703,8 @@ func (t *Template) executeRepeated(r *repeatedElement, st *state) { field = reflect.Indirect(field); // Must be an array/slice - if field != nil && field.Kind() != reflect.ArrayKind { + array, ok := field.(reflect.ArrayOrSliceValue); + if !ok { t.execError(st, r.linenum, ".repeated: %s has bad type %s", r.field, field.Type()); } if empty(field, true) { @@ -724,7 +727,6 @@ func (t *Template) executeRepeated(r *repeatedElement, st *state) { end = r.altstart } if field != nil { - array := field.(reflect.ArrayValue); for j := 0; j < array.Len(); j++ { newst := st.clone(array.Elem(j)); for i := start; i < end; { -- 2.48.1