2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-04 11:11:41 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
// Package structs provides functions for struct conversion.
|
2020-11-08 14:25:17 +08:00
|
|
|
//
|
|
|
|
|
// Inspired and improved from: https://github.com/fatih/structs
|
2019-07-04 11:11:41 +08:00
|
|
|
package structs
|
|
|
|
|
|
2020-11-08 14:25:17 +08:00
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
)
|
2019-07-04 11:11:41 +08:00
|
|
|
|
2020-11-08 14:25:17 +08:00
|
|
|
// Field contains information of a struct field .
|
2019-12-12 23:38:46 +08:00
|
|
|
type Field struct {
|
2020-11-08 14:25:17 +08:00
|
|
|
value reflect.Value
|
|
|
|
|
field reflect.StructField
|
2020-11-08 15:44:04 +08:00
|
|
|
// Retrieved tag value. There might be more than one tags in the field,
|
2019-12-12 23:38:46 +08:00
|
|
|
// but only one can be retrieved according to calling function rules.
|
2020-11-08 15:44:04 +08:00
|
|
|
TagValue string
|
2020-11-08 14:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tag returns the value associated with key in the tag string. If there is no
|
|
|
|
|
// such key in the tag, Tag returns the empty string.
|
|
|
|
|
func (f *Field) Tag(key string) string {
|
|
|
|
|
return f.field.Tag.Get(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Value returns the underlying value of the field. It panics if the field
|
|
|
|
|
// is not exported.
|
|
|
|
|
func (f *Field) Value() interface{} {
|
|
|
|
|
return f.value.Interface()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEmbedded returns true if the given field is an anonymous field (embedded)
|
|
|
|
|
func (f *Field) IsEmbedded() bool {
|
|
|
|
|
return f.field.Anonymous
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsExported returns true if the given field is exported.
|
|
|
|
|
func (f *Field) IsExported() bool {
|
|
|
|
|
return f.field.PkgPath == ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the name of the given field
|
|
|
|
|
func (f *Field) Name() string {
|
|
|
|
|
return f.field.Name
|
2019-12-12 23:38:46 +08:00
|
|
|
}
|