github.com/wangyougui/gf/v2@v2.6.5/os/gstructs/gstructs_field.go (about)

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