reflect: add StructField.IsExported method

This field was introduced in Go 1.17 and is used by the encoding/json
package (starting with Go 1.17).
Этот коммит содержится в:
Ayke van Laethem 2021-08-14 15:52:45 +02:00 коммит произвёл Ron Evans
родитель ad73986070
коммит d45497691f
3 изменённых файлов: 19 добавлений и 0 удалений

Просмотреть файл

@ -706,6 +706,11 @@ type StructField struct {
Offset uintptr Offset uintptr
} }
// IsExported reports whether the field is exported.
func (f StructField) IsExported() bool {
return f.PkgPath == ""
}
// rawStructField is the same as StructField but with the Type member replaced // rawStructField is the same as StructField but with the Type member replaced
// with rawType. For internal use only. Avoiding this conversion to the Type // with rawType. For internal use only. Avoiding this conversion to the Type
// interface improves code size in many cases. // interface improves code size in many cases.

1
testdata/reflect.go предоставленный
Просмотреть файл

@ -398,6 +398,7 @@ func showValue(rv reflect.Value, indent string) {
println(indent+" field:", i, field.Name) println(indent+" field:", i, field.Name)
println(indent+" tag:", field.Tag) println(indent+" tag:", field.Tag)
println(indent+" embedded:", field.Anonymous) println(indent+" embedded:", field.Anonymous)
println(indent+" exported:", field.IsExported())
showValue(rv.Field(i), indent+" ") showValue(rv.Field(i), indent+" ")
} }
default: default:

13
testdata/reflect.txt предоставленный
Просмотреть файл

@ -235,6 +235,7 @@ reflect type: struct
field: 0 error field: 0 error
tag: tag:
embedded: true embedded: true
exported: false
reflect type: interface reflect type: interface
interface interface
nil: true nil: true
@ -243,16 +244,19 @@ reflect type: struct
field: 0 a field: 0 a
tag: tag:
embedded: false embedded: false
exported: false
reflect type: uint8 reflect type: uint8
uint: 42 uint: 42
field: 1 b field: 1 b
tag: tag:
embedded: false embedded: false
exported: false
reflect type: int16 reflect type: int16
int: 321 int: 321
field: 2 c field: 2 c
tag: tag:
embedded: false embedded: false
exported: false
reflect type: int8 reflect type: int8
int: 123 int: 123
reflect type: struct comparable=false reflect type: struct comparable=false
@ -260,31 +264,37 @@ reflect type: struct comparable=false
field: 0 n field: 0 n
tag: foo:"bar" tag: foo:"bar"
embedded: false embedded: false
exported: false
reflect type: int reflect type: int
int: 5 int: 5
field: 1 some field: 1 some
tag: tag:
embedded: false embedded: false
exported: false
reflect type: struct reflect type: struct
struct: 2 struct: 2
field: 0 X field: 0 X
tag: tag:
embedded: false embedded: false
exported: true
reflect type: int16 reflect type: int16
int: -5 int: -5
field: 1 Y field: 1 Y
tag: tag:
embedded: false embedded: false
exported: true
reflect type: int16 reflect type: int16
int: 3 int: 3
field: 2 zero field: 2 zero
tag: tag:
embedded: false embedded: false
exported: false
reflect type: struct reflect type: struct
struct: 0 struct: 0
field: 3 buf field: 3 buf
tag: tag:
embedded: false embedded: false
exported: false
reflect type: slice comparable=false reflect type: slice comparable=false
slice: uint8 2 2 slice: uint8 2 2
pointer: true pointer: true
@ -298,6 +308,7 @@ reflect type: struct comparable=false
field: 4 Buf field: 4 Buf
tag: tag:
embedded: false embedded: false
exported: true
reflect type: slice comparable=false reflect type: slice comparable=false
slice: uint8 1 1 slice: uint8 1 1
pointer: true pointer: true
@ -313,12 +324,14 @@ reflect type: ptr
field: 0 next field: 0 next
tag: description:"chain" tag: description:"chain"
embedded: false embedded: false
exported: false
reflect type: ptr addrable=true reflect type: ptr addrable=true
pointer: false struct pointer: false struct
nil: true nil: true
field: 1 foo field: 1 foo
tag: tag:
embedded: false embedded: false
exported: false
reflect type: int addrable=true reflect type: int addrable=true
int: 42 int: 42
reflect type: ptr reflect type: ptr