gstructs_field.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  7. import (
  8. "reflect"
  9. "github.com/gogf/gf/v2/internal/empty"
  10. "github.com/gogf/gf/v2/internal/utils"
  11. "github.com/gogf/gf/v2/util/gtag"
  12. )
  13. // Tag returns the value associated with key in the tag string. If there is no
  14. // such key in the tag, Tag returns the empty string.
  15. func (f *Field) Tag(key string) string {
  16. s := f.Field.Tag.Get(key)
  17. if s != "" {
  18. s = gtag.Parse(s)
  19. }
  20. return s
  21. }
  22. // TagLookup returns the value associated with key in the tag string.
  23. // If the key is present in the tag the value (which may be empty)
  24. // is returned. Otherwise, the returned value will be the empty string.
  25. // The ok return value reports whether the value was explicitly set in
  26. // the tag string. If the tag does not have the conventional format,
  27. // the value returned by Lookup is unspecified.
  28. func (f *Field) TagLookup(key string) (value string, ok bool) {
  29. value, ok = f.Field.Tag.Lookup(key)
  30. if ok && value != "" {
  31. value = gtag.Parse(value)
  32. }
  33. return
  34. }
  35. // IsEmbedded returns true if the given field is an anonymous field (embedded)
  36. func (f *Field) IsEmbedded() bool {
  37. return f.Field.Anonymous
  38. }
  39. // TagStr returns the tag string of the field.
  40. func (f *Field) TagStr() string {
  41. return string(f.Field.Tag)
  42. }
  43. // TagMap returns all the tag of the field along with its value string as map.
  44. func (f *Field) TagMap() map[string]string {
  45. var (
  46. data = ParseTag(f.TagStr())
  47. )
  48. for k, v := range data {
  49. data[k] = utils.StripSlashes(gtag.Parse(v))
  50. }
  51. return data
  52. }
  53. // IsExported returns true if the given field is exported.
  54. func (f *Field) IsExported() bool {
  55. return f.Field.PkgPath == ""
  56. }
  57. // Name returns the name of the given field.
  58. func (f *Field) Name() string {
  59. return f.Field.Name
  60. }
  61. // Type returns the type of the given field.
  62. // Note that this Type is not reflect.Type. If you need reflect.Type, please use Field.Type().Type.
  63. func (f *Field) Type() Type {
  64. return Type{
  65. Type: f.Field.Type,
  66. }
  67. }
  68. // Kind returns the reflect.Kind for Value of Field `f`.
  69. func (f *Field) Kind() reflect.Kind {
  70. return f.Value.Kind()
  71. }
  72. // OriginalKind retrieves and returns the original reflect.Kind for Value of Field `f`.
  73. func (f *Field) OriginalKind() reflect.Kind {
  74. var (
  75. reflectType = f.Value.Type()
  76. reflectKind = reflectType.Kind()
  77. )
  78. for reflectKind == reflect.Ptr {
  79. reflectType = reflectType.Elem()
  80. reflectKind = reflectType.Kind()
  81. }
  82. return reflectKind
  83. }
  84. // IsEmpty checks and returns whether the value of this Field is empty.
  85. func (f *Field) IsEmpty() bool {
  86. return empty.IsEmpty(f.Value)
  87. }
  88. // IsNil checks and returns whether the value of this Field is nil.
  89. func (f *Field) IsNil(traceSource ...bool) bool {
  90. return empty.IsNil(f.Value, traceSource...)
  91. }