1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:04:52 -07:00

template: use new reflect interface (CL 31107)

R=r
DELTA=16  (3 added, 1 deleted, 12 changed)
OCL=31121
CL=31288
This commit is contained in:
Russ Cox 2009-07-07 11:04:34 -07:00
parent f1bc712035
commit e37f81b49c

View File

@ -569,12 +569,12 @@ func (st *state) findVar(s string) reflect.Value {
return st.data return st.data
} }
data := reflect.Indirect(st.data); data := reflect.Indirect(st.data);
typ, ok := data.Type().(reflect.StructType); typ, ok := data.Type().(*reflect.StructType);
if ok { if ok {
for i := 0; i < typ.Len(); i++ { for i := 0; i < typ.NumField(); i++ {
name, ftyp, tag, offset := typ.Field(i); f := typ.Field(i);
if name == s { if f.Name == s {
return data.(reflect.StructValue).Field(i) return data.(*reflect.StructValue).Field(i)
} }
} }
} }
@ -587,13 +587,15 @@ func empty(v reflect.Value, indirect_ok bool) bool {
if v == nil { if v == nil {
return true return true
} }
switch v.Type().Kind() { switch v := v.(type) {
case reflect.StringKind: case *reflect.StringValue:
return v.(reflect.StringValue).Get() == ""; return v.Get() == "";
case reflect.StructKind: case *reflect.StructValue:
return false; return false;
case reflect.ArrayKind: case *reflect.ArrayValue:
return v.(reflect.ArrayValue).Len() == 0; return v.Len() == 0;
case *reflect.SliceValue:
return v.Len() == 0;
} }
return true; return true;
} }
@ -701,7 +703,8 @@ func (t *Template) executeRepeated(r *repeatedElement, st *state) {
field = reflect.Indirect(field); field = reflect.Indirect(field);
// Must be an array/slice // 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()); t.execError(st, r.linenum, ".repeated: %s has bad type %s", r.field, field.Type());
} }
if empty(field, true) { if empty(field, true) {
@ -724,7 +727,6 @@ func (t *Template) executeRepeated(r *repeatedElement, st *state) {
end = r.altstart end = r.altstart
} }
if field != nil { if field != nil {
array := field.(reflect.ArrayValue);
for j := 0; j < array.Len(); j++ { for j := 0; j < array.Len(); j++ {
newst := st.clone(array.Elem(j)); newst := st.clone(array.Elem(j));
for i := start; i < end; { for i := start; i < end; {