structs.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package structs provides functions for struct conversion.
  7. //
  8. // Inspired and improved from: https://github.com/fatih/structs
  9. package structs
  10. import (
  11. "reflect"
  12. )
  13. // Field contains information of a struct field .
  14. type Field struct {
  15. value reflect.Value
  16. field reflect.StructField
  17. // Retrieved tag value. There might be more than one tags in the field,
  18. // but only one can be retrieved according to calling function rules.
  19. TagValue string
  20. }
  21. // Tag returns the value associated with key in the tag string. If there is no
  22. // such key in the tag, Tag returns the empty string.
  23. func (f *Field) Tag(key string) string {
  24. return f.field.Tag.Get(key)
  25. }
  26. // Value returns the underlying value of the field. It panics if the field
  27. // is not exported.
  28. func (f *Field) Value() interface{} {
  29. return f.value.Interface()
  30. }
  31. // IsEmbedded returns true if the given field is an anonymous field (embedded)
  32. func (f *Field) IsEmbedded() bool {
  33. return f.field.Anonymous
  34. }
  35. // IsExported returns true if the given field is exported.
  36. func (f *Field) IsExported() bool {
  37. return f.field.PkgPath == ""
  38. }
  39. // Name returns the name of the given field
  40. func (f *Field) Name() string {
  41. return f.field.Name
  42. }