gstructs.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright GoFrame Author(https://goframe.org). 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 gstructs provides functions for struct information retrieving.
  7. package gstructs
  8. import (
  9. "reflect"
  10. )
  11. // Type wraps reflect.Type for additional features.
  12. type Type struct {
  13. reflect.Type
  14. }
  15. // Field contains information of a struct field .
  16. type Field struct {
  17. Value reflect.Value // The underlying value of the field.
  18. Field reflect.StructField // The underlying field of the field.
  19. // Retrieved tag name. It depends TagValue.
  20. TagName string
  21. // Retrieved tag value.
  22. // There might be more than one tags in the field, but only one can be retrieved according to calling function rules.
  23. TagValue string
  24. }
  25. // FieldsInput is the input parameter struct type for function Fields.
  26. type FieldsInput struct {
  27. // Pointer should be type of struct/*struct.
  28. Pointer interface{}
  29. // RecursiveOption specifies the way retrieving the fields recursively if the attribute
  30. // is an embedded struct. It is RecursiveOptionNone in default.
  31. RecursiveOption RecursiveOption
  32. }
  33. // FieldMapInput is the input parameter struct type for function FieldMap.
  34. type FieldMapInput struct {
  35. // Pointer should be type of struct/*struct.
  36. Pointer interface{}
  37. // PriorityTagArray specifies the priority tag array for retrieving from high to low.
  38. // If it's given `nil`, it returns map[name]Field, of which the `name` is attribute name.
  39. PriorityTagArray []string
  40. // RecursiveOption specifies the way retrieving the fields recursively if the attribute
  41. // is an embedded struct. It is RecursiveOptionNone in default.
  42. RecursiveOption RecursiveOption
  43. }
  44. type RecursiveOption int
  45. const (
  46. RecursiveOptionNone RecursiveOption = 0 // No recursively retrieving fields as map if the field is an embedded struct.
  47. RecursiveOptionEmbedded RecursiveOption = 1 // Recursively retrieving fields as map if the field is an embedded struct.
  48. RecursiveOptionEmbeddedNoTag RecursiveOption = 2 // Recursively retrieving fields as map if the field is an embedded struct and the field has no tag.
  49. )