gitlab.com/evatix-go/core@v1.3.55/coredata/coredynamic/reflectGetter.go (about)

     1  package coredynamic
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"gitlab.com/evatix-go/core/errcore"
     7  	"gitlab.com/evatix-go/core/internal/reflectinternal"
     8  )
     9  
    10  type reflectGetter struct{}
    11  
    12  // PublicValuesMapStruct
    13  //
    14  //  returns structs fields map[string]Interface{}
    15  //  map[string:fieldName]Interface{}:PublicValue
    16  //
    17  //  Only public values will be collected into map values
    18  func (it reflectGetter) PublicValuesMapStruct(anyItem interface{}) (
    19  	map[string]interface{}, error,
    20  ) {
    21  	if reflectinternal.IsNull(anyItem) {
    22  		return map[string]interface{}{},
    23  			errcore.
    24  				NullResultType.
    25  				ErrorNoRefs(
    26  					"null given to expand map[name]value, failed")
    27  	}
    28  
    29  	return ReflectGetterUsingReflectValue.PublicValuesMapStruct(
    30  		reflect.ValueOf(anyItem))
    31  }
    32  
    33  // FieldNameWithValuesMap
    34  //
    35  //  returns structs fields map[string]Interface{}
    36  //  map[string:fieldName]reflect.Type:fieldType
    37  //
    38  //  unlike PublicValuesMapStruct to map it collects
    39  //  all fields with values including the private ones.
    40  //
    41  // However, this one will be slower in performance than PublicValuesMapStruct.
    42  func (it reflectGetter) FieldNameWithValuesMap(anyItem interface{}) (
    43  	map[string]interface{}, error,
    44  ) {
    45  	if reflectinternal.IsNull(anyItem) {
    46  		return map[string]interface{}{},
    47  			errcore.
    48  				NullResultType.
    49  				ErrorNoRefs(
    50  					"null given to expand map[name]value, failed")
    51  	}
    52  
    53  	return ReflectGetterUsingReflectValue.FieldNameWithValuesMap(
    54  		reflect.ValueOf(anyItem))
    55  }
    56  
    57  // FieldNamesMap
    58  //
    59  //  returns structs fields map[string]bool names
    60  //  map[string:fieldName]bool:exists
    61  func (it reflectGetter) FieldNamesMap(
    62  	anyItem interface{},
    63  ) (
    64  	map[string]bool, error,
    65  ) {
    66  	if reflectinternal.IsNull(anyItem) {
    67  		return map[string]bool{},
    68  			errcore.
    69  				NullResultType.
    70  				ErrorNoRefs(
    71  					"null given to expand map[name]bool, failed")
    72  	}
    73  
    74  	return ReflectGetterUsingReflectValue.FieldNamesMap(
    75  		reflect.ValueOf(anyItem))
    76  }
    77  
    78  // StructFieldsMap
    79  //
    80  //  returns structs all fields (public, private) map[string]reflect.StructField
    81  //  map[string:fieldName]reflect.StructField:StructField
    82  func (it reflectGetter) StructFieldsMap(
    83  	anyItem interface{},
    84  ) map[string]reflect.StructField {
    85  	if reflectinternal.IsNull(anyItem) {
    86  		return map[string]reflect.StructField{}
    87  	}
    88  
    89  	return ReflectGetterUsingReflectValue.StructFieldsMap(
    90  		reflect.ValueOf(anyItem))
    91  }
    92  
    93  // NullFieldsMap
    94  //
    95  //  returns structs all fields (public, private) map[string]bool
    96  //  null fields map only
    97  func (it reflectGetter) NullFieldsMap(
    98  	anyItem interface{},
    99  ) map[string]bool {
   100  	if reflectinternal.IsNull(anyItem) {
   101  		return map[string]bool{}
   102  	}
   103  
   104  	return ReflectGetterUsingReflectValue.NullFieldsMap(
   105  		defaultMaxLevelOfReflection,
   106  		reflect.ValueOf(anyItem))
   107  }
   108  
   109  // NullOrZeroFieldsMap
   110  //
   111  //  returns structs all fields (public, private) map[string]bool
   112  //  null or zero fields map only
   113  func (it reflectGetter) NullOrZeroFieldsMap(
   114  	anyItem interface{},
   115  ) map[string]bool {
   116  	if reflectinternal.IsNull(anyItem) {
   117  		return map[string]bool{}
   118  	}
   119  
   120  	return ReflectGetterUsingReflectValue.NullOrZeroFieldsMap(
   121  		defaultMaxLevelOfReflection,
   122  		reflect.ValueOf(anyItem))
   123  }