Skip to content

Commit c8d0b87

Browse files
ydnardeadprogram
authored andcommitted
internal/reflectlite, reflect: handle different StructField
1 parent 99a6183 commit c8d0b87

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

src/internal/reflectlite/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ func (t *RawType) String() string {
294294
}
295295
return s
296296
}
297-
298297
switch t.Kind() {
299298
case Chan:
300299
elem := t.elem().String()
@@ -977,6 +976,7 @@ func (t *RawType) FieldByIndex(index []int) StructField {
977976
}
978977

979978
// A StructField describes a single field in a struct.
979+
// This must be kept in sync with [reflect.StructField].
980980
type StructField struct {
981981
// Name indicates the field name.
982982
Name string

src/reflect/deepequal.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package reflect
2+
3+
import "internal/reflectlite"
4+
5+
func DeepEqual(x, y interface{}) bool {
6+
return reflectlite.DeepEqual(x, y)
7+
}

src/reflect/type.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ type rawType struct {
342342
}
343343

344344
func toType(t reflectlite.Type) Type {
345+
if t == nil {
346+
return nil
347+
}
345348
return (*rawType)(unsafe.Pointer(t.(*reflectlite.RawType)))
346349
}
347350

@@ -395,6 +398,30 @@ func (t *rawType) ConvertibleTo(u Type) bool {
395398
panic("unimplemented: (reflect.Type).ConvertibleTo()")
396399
}
397400

401+
func (t *rawType) Elem() Type {
402+
return toType(t.RawType.Elem())
403+
}
404+
405+
func (t *rawType) Field(i int) StructField {
406+
f := t.RawType.Field(i)
407+
return toStructField(f)
408+
}
409+
410+
func (t *rawType) FieldByIndex(index []int) StructField {
411+
f := t.RawType.FieldByIndex(index)
412+
return toStructField(f)
413+
}
414+
415+
func (t *rawType) FieldByName(name string) (StructField, bool) {
416+
f, ok := t.RawType.FieldByName(name)
417+
return toStructField(f), ok
418+
}
419+
420+
func (t *rawType) FieldByNameFunc(match func(string) bool) (StructField, bool) {
421+
f, ok := t.RawType.FieldByNameFunc(match)
422+
return toStructField(f), ok
423+
}
424+
398425
func (t *rawType) Implements(u Type) bool {
399426
return t.RawType.Implements(&(u.(*rawType).RawType))
400427
}
@@ -431,11 +458,41 @@ func (t *rawType) Out(i int) Type {
431458
panic("unimplemented: (reflect.Type).Out()")
432459
}
433460

434-
func (t *rawType) Elem() Type {
435-
return toType(t.RawType.Elem())
461+
// A StructField describes a single field in a struct.
462+
// This must be kept in sync with [reflectlite.StructField].
463+
type StructField struct {
464+
// Name indicates the field name.
465+
Name string
466+
467+
// PkgPath is the package path where the struct containing this field is
468+
// declared for unexported fields, or the empty string for exported fields.
469+
PkgPath string
470+
471+
Type Type
472+
Tag StructTag // field tag string
473+
Offset uintptr
474+
Index []int // index sequence for Type.FieldByIndex
475+
Anonymous bool
476+
}
477+
478+
func toStructField(f reflectlite.StructField) StructField {
479+
return StructField{
480+
Name: f.Name,
481+
PkgPath: f.PkgPath,
482+
Type: toType(f.Type),
483+
Tag: f.Tag,
484+
Offset: f.Offset,
485+
Index: f.Index,
486+
Anonymous: f.Anonymous,
487+
}
488+
}
489+
490+
// IsExported reports whether the field is exported.
491+
func (f StructField) IsExported() bool {
492+
return f.PkgPath == ""
436493
}
437494

438-
type StructField = reflectlite.StructField
495+
type StructTag = reflectlite.StructTag
439496

440497
func TypeFor[T any]() Type {
441498
return toType(reflectlite.TypeFor[T]())

src/reflect/visiblefields.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package reflect
22

3-
import "internal/reflectlite"
3+
import (
4+
"internal/reflectlite"
5+
"unsafe"
6+
)
47

58
func VisibleFields(t Type) []StructField {
6-
return reflectlite.VisibleFields(toRawType(t))
9+
fields := reflectlite.VisibleFields(toRawType(t))
10+
return *(*[]StructField)(unsafe.Pointer(&fields))
711
}

0 commit comments

Comments
 (0)