internal/reflectlite, reflect: handle different StructField

This commit is contained in:
Randy Reddig
2025-03-08 15:45:37 -08:00
committed by Ron Evans
parent 99a618385b
commit c8d0b87a67
4 changed files with 74 additions and 6 deletions
+1 -1
View File
@@ -294,7 +294,6 @@ func (t *RawType) String() string {
} }
return s return s
} }
switch t.Kind() { switch t.Kind() {
case Chan: case Chan:
elem := t.elem().String() elem := t.elem().String()
@@ -977,6 +976,7 @@ func (t *RawType) FieldByIndex(index []int) StructField {
} }
// A StructField describes a single field in a struct. // A StructField describes a single field in a struct.
// This must be kept in sync with [reflect.StructField].
type StructField struct { type StructField struct {
// Name indicates the field name. // Name indicates the field name.
Name string Name string
+7
View File
@@ -0,0 +1,7 @@
package reflect
import "internal/reflectlite"
func DeepEqual(x, y interface{}) bool {
return reflectlite.DeepEqual(x, y)
}
+60 -3
View File
@@ -342,6 +342,9 @@ type rawType struct {
} }
func toType(t reflectlite.Type) Type { func toType(t reflectlite.Type) Type {
if t == nil {
return nil
}
return (*rawType)(unsafe.Pointer(t.(*reflectlite.RawType))) return (*rawType)(unsafe.Pointer(t.(*reflectlite.RawType)))
} }
@@ -395,6 +398,30 @@ func (t *rawType) ConvertibleTo(u Type) bool {
panic("unimplemented: (reflect.Type).ConvertibleTo()") panic("unimplemented: (reflect.Type).ConvertibleTo()")
} }
func (t *rawType) Elem() Type {
return toType(t.RawType.Elem())
}
func (t *rawType) Field(i int) StructField {
f := t.RawType.Field(i)
return toStructField(f)
}
func (t *rawType) FieldByIndex(index []int) StructField {
f := t.RawType.FieldByIndex(index)
return toStructField(f)
}
func (t *rawType) FieldByName(name string) (StructField, bool) {
f, ok := t.RawType.FieldByName(name)
return toStructField(f), ok
}
func (t *rawType) FieldByNameFunc(match func(string) bool) (StructField, bool) {
f, ok := t.RawType.FieldByNameFunc(match)
return toStructField(f), ok
}
func (t *rawType) Implements(u Type) bool { func (t *rawType) Implements(u Type) bool {
return t.RawType.Implements(&(u.(*rawType).RawType)) return t.RawType.Implements(&(u.(*rawType).RawType))
} }
@@ -431,11 +458,41 @@ func (t *rawType) Out(i int) Type {
panic("unimplemented: (reflect.Type).Out()") panic("unimplemented: (reflect.Type).Out()")
} }
func (t *rawType) Elem() Type { // A StructField describes a single field in a struct.
return toType(t.RawType.Elem()) // This must be kept in sync with [reflectlite.StructField].
type StructField struct {
// Name indicates the field name.
Name string
// PkgPath is the package path where the struct containing this field is
// declared for unexported fields, or the empty string for exported fields.
PkgPath string
Type Type
Tag StructTag // field tag string
Offset uintptr
Index []int // index sequence for Type.FieldByIndex
Anonymous bool
} }
type StructField = reflectlite.StructField func toStructField(f reflectlite.StructField) StructField {
return StructField{
Name: f.Name,
PkgPath: f.PkgPath,
Type: toType(f.Type),
Tag: f.Tag,
Offset: f.Offset,
Index: f.Index,
Anonymous: f.Anonymous,
}
}
// IsExported reports whether the field is exported.
func (f StructField) IsExported() bool {
return f.PkgPath == ""
}
type StructTag = reflectlite.StructTag
func TypeFor[T any]() Type { func TypeFor[T any]() Type {
return toType(reflectlite.TypeFor[T]()) return toType(reflectlite.TypeFor[T]())
+6 -2
View File
@@ -1,7 +1,11 @@
package reflect package reflect
import "internal/reflectlite" import (
"internal/reflectlite"
"unsafe"
)
func VisibleFields(t Type) []StructField { func VisibleFields(t Type) []StructField {
return reflectlite.VisibleFields(toRawType(t)) fields := reflectlite.VisibleFields(toRawType(t))
return *(*[]StructField)(unsafe.Pointer(&fields))
} }