Browse Source

Fixed embedded fields shadowing

Dmitry Panov 9 years ago
parent
commit
64bfa769d0
2 changed files with 11 additions and 5 deletions
  1. 9 3
      object_goreflect.go
  2. 2 2
      object_goreflect_test.go

+ 9 - 3
object_goreflect.go

@@ -428,12 +428,18 @@ func (r *Runtime) buildFieldInfo(t reflect.Type, index []int, info *reflectTypeI
 			}
 			}
 		}
 		}
 
 
+		if inf, exists := info.Fields[name]; !exists {
+			info.FieldNames = append(info.FieldNames, name)
+		} else {
+			if len(inf.Index) <= len(index) {
+				continue
+			}
+		}
+
 		idx := make([]int, len(index)+1)
 		idx := make([]int, len(index)+1)
 		copy(idx, index)
 		copy(idx, index)
 		idx[len(idx)-1] = i
 		idx[len(idx)-1] = i
-		if _, exists := info.Fields[name]; !exists {
-			info.FieldNames = append(info.FieldNames, name)
-		}
+
 		info.Fields[name] = reflectFieldInfo{
 		info.Fields[name] = reflectFieldInfo{
 			Index:     idx,
 			Index:     idx,
 			Anonymous: field.Anonymous,
 			Anonymous: field.Anonymous,

+ 2 - 2
object_goreflect_test.go

@@ -453,9 +453,9 @@ func TestGoReflectEmbeddedStruct(t *testing.T) {
 	}
 	}
 
 
 	type Child struct {
 	type Child struct {
-		Parent
 		ParentField2 string
 		ParentField2 string
-		ChildField   int
+		Parent
+		ChildField int
 	}
 	}
 
 
 	vm := New()
 	vm := New()