mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 02:27:48 +00:00
reflect, internal/reflectlite: embed reflectlite types into reflect types
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
// Deep equality test via reflection
|
||||
|
||||
package reflect
|
||||
package reflectlite
|
||||
|
||||
import "unsafe"
|
||||
|
||||
@@ -15,7 +15,7 @@ import "unsafe"
|
||||
type visit struct {
|
||||
a1 unsafe.Pointer
|
||||
a2 unsafe.Pointer
|
||||
typ *rawType
|
||||
typ *RawType
|
||||
}
|
||||
|
||||
// Tests for deep equality using reflected types. The map argument tracks
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build mips
|
||||
|
||||
package reflect
|
||||
package reflectlite
|
||||
|
||||
import "unsafe"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build !mips
|
||||
|
||||
package reflect
|
||||
package reflectlite
|
||||
|
||||
import "unsafe"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build !avr
|
||||
|
||||
package reflect
|
||||
package reflectlite
|
||||
|
||||
// intw is an integer type, used in places where an int is typically required,
|
||||
// except architectures where the size of an int != word size.
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build avr
|
||||
|
||||
package reflect
|
||||
package reflectlite
|
||||
|
||||
// intw is an integer type, used in places where an int is typically required,
|
||||
// except architectures where the size of an int != word size.
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build !avr
|
||||
|
||||
package reflect_test
|
||||
package reflectlite_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build never
|
||||
|
||||
package reflectlite
|
||||
|
||||
import "reflect"
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflectlite
|
||||
|
||||
import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// errSyntax indicates that a value does not have the right syntax for the target type.
|
||||
var errSyntax = badSyntax{}
|
||||
|
||||
type badSyntax struct{}
|
||||
|
||||
func (badSyntax) Error() string {
|
||||
return "invalid syntax"
|
||||
}
|
||||
|
||||
func unhex(b byte) (v rune, ok bool) {
|
||||
c := rune(b)
|
||||
switch {
|
||||
case '0' <= c && c <= '9':
|
||||
return c - '0', true
|
||||
case 'a' <= c && c <= 'f':
|
||||
return c - 'a' + 10, true
|
||||
case 'A' <= c && c <= 'F':
|
||||
return c - 'A' + 10, true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
lowerhex = "0123456789abcef"
|
||||
)
|
||||
|
||||
// unquoteChar decodes the first character or byte in the escaped string
|
||||
// or character literal represented by the string s.
|
||||
// It returns four values:
|
||||
//
|
||||
// 1. value, the decoded Unicode code point or byte value;
|
||||
// 2. multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation;
|
||||
// 3. tail, the remainder of the string after the character; and
|
||||
// 4. an error that will be nil if the character is syntactically valid.
|
||||
//
|
||||
// The second argument, quote, specifies the type of literal being parsed
|
||||
// and therefore which escaped quote character is permitted.
|
||||
// If set to a single quote, it permits the sequence \' and disallows unescaped '.
|
||||
// If set to a double quote, it permits \" and disallows unescaped ".
|
||||
// If set to zero, it does not permit either escape and allows both quote characters to appear unescaped.
|
||||
func unquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) {
|
||||
// easy cases
|
||||
if len(s) == 0 {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
switch c := s[0]; {
|
||||
case c == quote && (quote == '\'' || quote == '"'):
|
||||
err = errSyntax
|
||||
return
|
||||
case c >= utf8.RuneSelf:
|
||||
r, size := utf8.DecodeRuneInString(s)
|
||||
return r, true, s[size:], nil
|
||||
case c != '\\':
|
||||
return rune(s[0]), false, s[1:], nil
|
||||
}
|
||||
|
||||
// hard case: c is backslash
|
||||
if len(s) <= 1 {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
c := s[1]
|
||||
s = s[2:]
|
||||
|
||||
switch c {
|
||||
case 'a':
|
||||
value = '\a'
|
||||
case 'b':
|
||||
value = '\b'
|
||||
case 'f':
|
||||
value = '\f'
|
||||
case 'n':
|
||||
value = '\n'
|
||||
case 'r':
|
||||
value = '\r'
|
||||
case 't':
|
||||
value = '\t'
|
||||
case 'v':
|
||||
value = '\v'
|
||||
case 'x', 'u', 'U':
|
||||
n := 0
|
||||
switch c {
|
||||
case 'x':
|
||||
n = 2
|
||||
case 'u':
|
||||
n = 4
|
||||
case 'U':
|
||||
n = 8
|
||||
}
|
||||
var v rune
|
||||
if len(s) < n {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
for j := 0; j < n; j++ {
|
||||
x, ok := unhex(s[j])
|
||||
if !ok {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
v = v<<4 | x
|
||||
}
|
||||
s = s[n:]
|
||||
if c == 'x' {
|
||||
// single-byte string, possibly not UTF-8
|
||||
value = v
|
||||
break
|
||||
}
|
||||
if v > utf8.MaxRune {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
value = v
|
||||
multibyte = true
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7':
|
||||
v := rune(c) - '0'
|
||||
if len(s) < 2 {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
for j := 0; j < 2; j++ { // one digit already; two more
|
||||
x := rune(s[j]) - '0'
|
||||
if x < 0 || x > 7 {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
v = (v << 3) | x
|
||||
}
|
||||
s = s[2:]
|
||||
if v > 255 {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
value = v
|
||||
case '\\':
|
||||
value = '\\'
|
||||
case '\'', '"':
|
||||
if c != quote {
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
value = rune(c)
|
||||
default:
|
||||
err = errSyntax
|
||||
return
|
||||
}
|
||||
tail = s
|
||||
return
|
||||
}
|
||||
|
||||
// unquote interprets s as a single-quoted, double-quoted,
|
||||
// or backquoted Go string literal, returning the string value
|
||||
// that s quotes. (If s is single-quoted, it would be a Go
|
||||
// character literal; unquote returns the corresponding
|
||||
// one-character string.)
|
||||
func unquote(s string) (string, error) {
|
||||
n := len(s)
|
||||
if n < 2 {
|
||||
return "", errSyntax
|
||||
}
|
||||
quote := s[0]
|
||||
if quote != s[n-1] {
|
||||
return "", errSyntax
|
||||
}
|
||||
s = s[1 : n-1]
|
||||
|
||||
if quote == '`' {
|
||||
if contains(s, '`') {
|
||||
return "", errSyntax
|
||||
}
|
||||
if contains(s, '\r') {
|
||||
// -1 because we know there is at least one \r to remove.
|
||||
buf := make([]byte, 0, len(s)-1)
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] != '\r' {
|
||||
buf = append(buf, s[i])
|
||||
}
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
if quote != '"' && quote != '\'' {
|
||||
return "", errSyntax
|
||||
}
|
||||
if contains(s, '\n') {
|
||||
return "", errSyntax
|
||||
}
|
||||
|
||||
// Is it trivial? Avoid allocation.
|
||||
if !contains(s, '\\') && !contains(s, quote) {
|
||||
switch quote {
|
||||
case '"':
|
||||
if utf8.ValidString(s) {
|
||||
return s, nil
|
||||
}
|
||||
case '\'':
|
||||
r, size := utf8.DecodeRuneInString(s)
|
||||
if size == len(s) && (r != utf8.RuneError || size != 1) {
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var runeTmp [utf8.UTFMax]byte
|
||||
buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations.
|
||||
for len(s) > 0 {
|
||||
c, multibyte, ss, err := unquoteChar(s, quote)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
s = ss
|
||||
if c < utf8.RuneSelf || !multibyte {
|
||||
buf = append(buf, byte(c))
|
||||
} else {
|
||||
n := utf8.EncodeRune(runeTmp[:], c)
|
||||
buf = append(buf, runeTmp[:n]...)
|
||||
}
|
||||
if quote == '\'' && len(s) != 0 {
|
||||
// single-quoted must be single character
|
||||
return "", errSyntax
|
||||
}
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
|
||||
func quote(s string) string {
|
||||
buf := make([]byte, 0, 3*len(s)/2)
|
||||
const quote = '"'
|
||||
|
||||
buf = append(buf, quote)
|
||||
for width := 0; len(s) > 0; s = s[width:] {
|
||||
r := rune(s[0])
|
||||
width = 1
|
||||
if r >= utf8.RuneSelf {
|
||||
r, width = utf8.DecodeRuneInString(s)
|
||||
}
|
||||
if width == 1 && r == utf8.RuneError {
|
||||
buf = append(buf, `\x`...)
|
||||
buf = append(buf, lowerhex[s[0]>>4])
|
||||
buf = append(buf, lowerhex[s[0]&0xF])
|
||||
continue
|
||||
}
|
||||
buf = appendEscapedRune(buf, r)
|
||||
}
|
||||
buf = append(buf, quote)
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func appendEscapedRune(buf []byte, r rune) []byte {
|
||||
|
||||
const quote = '"'
|
||||
|
||||
var runeTmp [utf8.UTFMax]byte
|
||||
if r == rune(quote) || r == '\\' { // always backslashed
|
||||
buf = append(buf, '\\')
|
||||
buf = append(buf, byte(r))
|
||||
return buf
|
||||
}
|
||||
if isPrint(r) {
|
||||
n := utf8.EncodeRune(runeTmp[:], r)
|
||||
buf = append(buf, runeTmp[:n]...)
|
||||
return buf
|
||||
}
|
||||
switch r {
|
||||
case '\a':
|
||||
buf = append(buf, `\a`...)
|
||||
case '\b':
|
||||
buf = append(buf, `\b`...)
|
||||
case '\f':
|
||||
buf = append(buf, `\f`...)
|
||||
case '\n':
|
||||
buf = append(buf, `\n`...)
|
||||
case '\r':
|
||||
buf = append(buf, `\r`...)
|
||||
case '\t':
|
||||
buf = append(buf, `\t`...)
|
||||
case '\v':
|
||||
buf = append(buf, `\v`...)
|
||||
default:
|
||||
switch {
|
||||
case r < ' ' || r == 0x7f:
|
||||
buf = append(buf, `\x`...)
|
||||
buf = append(buf, lowerhex[byte(r)>>4])
|
||||
buf = append(buf, lowerhex[byte(r)&0xF])
|
||||
case !utf8.ValidRune(r):
|
||||
r = 0xFFFD
|
||||
fallthrough
|
||||
case r < 0x10000:
|
||||
buf = append(buf, `\u`...)
|
||||
for s := 12; s >= 0; s -= 4 {
|
||||
buf = append(buf, lowerhex[r>>uint(s)&0xF])
|
||||
}
|
||||
default:
|
||||
buf = append(buf, `\U`...)
|
||||
for s := 28; s >= 0; s -= 4 {
|
||||
buf = append(buf, lowerhex[r>>uint(s)&0xF])
|
||||
}
|
||||
}
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// This is only used for struct tags. Assume
|
||||
func isPrint(r rune) bool {
|
||||
if r <= 0xFF {
|
||||
if 0x20 <= r && r <= 0x7E {
|
||||
// All the ASCII is printable from space through DEL-1.
|
||||
return true
|
||||
}
|
||||
if 0xA1 <= r && r <= 0xFF {
|
||||
// Similarly for ¡ through ÿ...
|
||||
return r != 0xAD // ...except for the bizarre soft hyphen.
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TinyGo: Skip all other unicode processing
|
||||
return false
|
||||
}
|
||||
|
||||
// contains reports whether the string contains the byte c.
|
||||
func contains(s string, c byte) bool {
|
||||
return indexByteString(s, c) != -1
|
||||
}
|
||||
|
||||
// Index finds the index of the first instance of the specified byte in the string.
|
||||
// If the byte is not found, this returns -1.
|
||||
func indexByteString(s string, c byte) int {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == c {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package reflectlite
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// Some of code here has been copied from the Go sources:
|
||||
// https://github.com/golang/go/blob/go1.15.2/src/reflect/swapper.go
|
||||
// It has the following copyright note:
|
||||
//
|
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
func Swapper(slice interface{}) func(i, j int) {
|
||||
v := ValueOf(slice)
|
||||
if v.Kind() != Slice {
|
||||
panic(&ValueError{Method: "Swapper"})
|
||||
}
|
||||
|
||||
// Just return Nop func if nothing to swap.
|
||||
if v.Len() < 2 {
|
||||
return func(i, j int) {}
|
||||
}
|
||||
|
||||
typ := v.typecode.Elem()
|
||||
size := typ.Size()
|
||||
|
||||
header := (*sliceHeader)(v.value)
|
||||
tmp := unsafe.Pointer(&make([]byte, size)[0])
|
||||
|
||||
return func(i, j int) {
|
||||
if uint(i) >= uint(header.len) || uint(j) >= uint(header.len) {
|
||||
panic("reflect: slice index out of range")
|
||||
}
|
||||
val1 := unsafe.Add(header.data, uintptr(i)*size)
|
||||
val2 := unsafe.Add(header.data, uintptr(j)*size)
|
||||
memcpy(tmp, val1, size)
|
||||
memcpy(val1, val2, size)
|
||||
memcpy(val2, tmp, size)
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,105 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflectlite
|
||||
|
||||
// VisibleFields returns all the visible fields in t, which must be a
|
||||
// struct type. A field is defined as visible if it's accessible
|
||||
// directly with a FieldByName call. The returned fields include fields
|
||||
// inside anonymous struct members and unexported fields. They follow
|
||||
// the same order found in the struct, with anonymous fields followed
|
||||
// immediately by their promoted fields.
|
||||
//
|
||||
// For each element e of the returned slice, the corresponding field
|
||||
// can be retrieved from a value v of type t by calling v.FieldByIndex(e.Index).
|
||||
func VisibleFields(t Type) []StructField {
|
||||
if t == nil {
|
||||
panic("reflect: VisibleFields(nil)")
|
||||
}
|
||||
if t.Kind() != Struct {
|
||||
panic("reflect.VisibleFields of non-struct type")
|
||||
}
|
||||
w := &visibleFieldsWalker{
|
||||
byName: make(map[string]int),
|
||||
visiting: make(map[Type]bool),
|
||||
fields: make([]StructField, 0, t.NumField()),
|
||||
index: make([]int, 0, 2),
|
||||
}
|
||||
w.walk(t)
|
||||
// Remove all the fields that have been hidden.
|
||||
// Use an in-place removal that avoids copying in
|
||||
// the common case that there are no hidden fields.
|
||||
j := 0
|
||||
for i := range w.fields {
|
||||
f := &w.fields[i]
|
||||
if f.Name == "" {
|
||||
continue
|
||||
}
|
||||
if i != j {
|
||||
// A field has been removed. We need to shuffle
|
||||
// all the subsequent elements up.
|
||||
w.fields[j] = *f
|
||||
}
|
||||
j++
|
||||
}
|
||||
return w.fields[:j]
|
||||
}
|
||||
|
||||
type visibleFieldsWalker struct {
|
||||
byName map[string]int
|
||||
visiting map[Type]bool
|
||||
fields []StructField
|
||||
index []int
|
||||
}
|
||||
|
||||
// walk walks all the fields in the struct type t, visiting
|
||||
// fields in index preorder and appending them to w.fields
|
||||
// (this maintains the required ordering).
|
||||
// Fields that have been overridden have their
|
||||
// Name field cleared.
|
||||
func (w *visibleFieldsWalker) walk(t Type) {
|
||||
if w.visiting[t] {
|
||||
return
|
||||
}
|
||||
w.visiting[t] = true
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
w.index = append(w.index, i)
|
||||
add := true
|
||||
if oldIndex, ok := w.byName[f.Name]; ok {
|
||||
old := &w.fields[oldIndex]
|
||||
if len(w.index) == len(old.Index) {
|
||||
// Fields with the same name at the same depth
|
||||
// cancel one another out. Set the field name
|
||||
// to empty to signify that has happened, and
|
||||
// there's no need to add this field.
|
||||
old.Name = ""
|
||||
add = false
|
||||
} else if len(w.index) < len(old.Index) {
|
||||
// The old field loses because it's deeper than the new one.
|
||||
old.Name = ""
|
||||
} else {
|
||||
// The old field wins because it's shallower than the new one.
|
||||
add = false
|
||||
}
|
||||
}
|
||||
if add {
|
||||
// Copy the index so that it's not overwritten
|
||||
// by the other appends.
|
||||
f.Index = append([]int(nil), w.index...)
|
||||
w.byName[f.Name] = len(w.fields)
|
||||
w.fields = append(w.fields, f)
|
||||
}
|
||||
if f.Anonymous {
|
||||
if f.Type.Kind() == Pointer {
|
||||
f.Type = f.Type.Elem()
|
||||
}
|
||||
if f.Type.Kind() == Struct {
|
||||
w.walk(f.Type)
|
||||
}
|
||||
}
|
||||
w.index = w.index[:len(w.index)-1]
|
||||
}
|
||||
delete(w.visiting, t)
|
||||
}
|
||||
+2
-35
@@ -1,40 +1,7 @@
|
||||
package reflect
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// Some of code here has been copied from the Go sources:
|
||||
// https://github.com/golang/go/blob/go1.15.2/src/reflect/swapper.go
|
||||
// It has the following copyright note:
|
||||
//
|
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
import "internal/reflectlite"
|
||||
|
||||
func Swapper(slice interface{}) func(i, j int) {
|
||||
v := ValueOf(slice)
|
||||
if v.Kind() != Slice {
|
||||
panic(&ValueError{Method: "Swapper"})
|
||||
}
|
||||
|
||||
// Just return Nop func if nothing to swap.
|
||||
if v.Len() < 2 {
|
||||
return func(i, j int) {}
|
||||
}
|
||||
|
||||
typ := v.typecode.Elem()
|
||||
size := typ.Size()
|
||||
|
||||
header := (*sliceHeader)(v.value)
|
||||
tmp := unsafe.Pointer(&make([]byte, size)[0])
|
||||
|
||||
return func(i, j int) {
|
||||
if uint(i) >= uint(header.len) || uint(j) >= uint(header.len) {
|
||||
panic("reflect: slice index out of range")
|
||||
}
|
||||
val1 := unsafe.Add(header.data, uintptr(i)*size)
|
||||
val2 := unsafe.Add(header.data, uintptr(j)*size)
|
||||
memcpy(tmp, val1, size)
|
||||
memcpy(val1, val2, size)
|
||||
memcpy(val2, tmp, size)
|
||||
}
|
||||
return reflectlite.Swapper(slice)
|
||||
}
|
||||
|
||||
+71
-1086
File diff suppressed because it is too large
Load Diff
+47
-1960
File diff suppressed because it is too large
Load Diff
@@ -1,105 +1,7 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflect
|
||||
|
||||
// VisibleFields returns all the visible fields in t, which must be a
|
||||
// struct type. A field is defined as visible if it's accessible
|
||||
// directly with a FieldByName call. The returned fields include fields
|
||||
// inside anonymous struct members and unexported fields. They follow
|
||||
// the same order found in the struct, with anonymous fields followed
|
||||
// immediately by their promoted fields.
|
||||
//
|
||||
// For each element e of the returned slice, the corresponding field
|
||||
// can be retrieved from a value v of type t by calling v.FieldByIndex(e.Index).
|
||||
import "internal/reflectlite"
|
||||
|
||||
func VisibleFields(t Type) []StructField {
|
||||
if t == nil {
|
||||
panic("reflect: VisibleFields(nil)")
|
||||
}
|
||||
if t.Kind() != Struct {
|
||||
panic("reflect.VisibleFields of non-struct type")
|
||||
}
|
||||
w := &visibleFieldsWalker{
|
||||
byName: make(map[string]int),
|
||||
visiting: make(map[Type]bool),
|
||||
fields: make([]StructField, 0, t.NumField()),
|
||||
index: make([]int, 0, 2),
|
||||
}
|
||||
w.walk(t)
|
||||
// Remove all the fields that have been hidden.
|
||||
// Use an in-place removal that avoids copying in
|
||||
// the common case that there are no hidden fields.
|
||||
j := 0
|
||||
for i := range w.fields {
|
||||
f := &w.fields[i]
|
||||
if f.Name == "" {
|
||||
continue
|
||||
}
|
||||
if i != j {
|
||||
// A field has been removed. We need to shuffle
|
||||
// all the subsequent elements up.
|
||||
w.fields[j] = *f
|
||||
}
|
||||
j++
|
||||
}
|
||||
return w.fields[:j]
|
||||
}
|
||||
|
||||
type visibleFieldsWalker struct {
|
||||
byName map[string]int
|
||||
visiting map[Type]bool
|
||||
fields []StructField
|
||||
index []int
|
||||
}
|
||||
|
||||
// walk walks all the fields in the struct type t, visiting
|
||||
// fields in index preorder and appending them to w.fields
|
||||
// (this maintains the required ordering).
|
||||
// Fields that have been overridden have their
|
||||
// Name field cleared.
|
||||
func (w *visibleFieldsWalker) walk(t Type) {
|
||||
if w.visiting[t] {
|
||||
return
|
||||
}
|
||||
w.visiting[t] = true
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
w.index = append(w.index, i)
|
||||
add := true
|
||||
if oldIndex, ok := w.byName[f.Name]; ok {
|
||||
old := &w.fields[oldIndex]
|
||||
if len(w.index) == len(old.Index) {
|
||||
// Fields with the same name at the same depth
|
||||
// cancel one another out. Set the field name
|
||||
// to empty to signify that has happened, and
|
||||
// there's no need to add this field.
|
||||
old.Name = ""
|
||||
add = false
|
||||
} else if len(w.index) < len(old.Index) {
|
||||
// The old field loses because it's deeper than the new one.
|
||||
old.Name = ""
|
||||
} else {
|
||||
// The old field wins because it's shallower than the new one.
|
||||
add = false
|
||||
}
|
||||
}
|
||||
if add {
|
||||
// Copy the index so that it's not overwritten
|
||||
// by the other appends.
|
||||
f.Index = append([]int(nil), w.index...)
|
||||
w.byName[f.Name] = len(w.fields)
|
||||
w.fields = append(w.fields, f)
|
||||
}
|
||||
if f.Anonymous {
|
||||
if f.Type.Kind() == Pointer {
|
||||
f.Type = f.Type.Elem()
|
||||
}
|
||||
if f.Type.Kind() == Struct {
|
||||
w.walk(f.Type)
|
||||
}
|
||||
}
|
||||
w.index = w.index[:len(w.index)-1]
|
||||
}
|
||||
delete(w.visiting, t)
|
||||
return reflectlite.VisibleFields(toRawType(t))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user