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

     1  package coredynamic
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  	"gitlab.com/evatix-go/core/coredata/corejson"
     9  	"gitlab.com/evatix-go/core/errcore"
    10  	"gitlab.com/evatix-go/core/internal/reflectinternal"
    11  )
    12  
    13  type KeyVal struct {
    14  	Key   interface{}
    15  	Value interface{}
    16  }
    17  
    18  func (it *KeyVal) KeyDynamic() Dynamic {
    19  	return NewDynamic(it.Key, true)
    20  }
    21  
    22  func (it *KeyVal) ValueDynamic() Dynamic {
    23  	return NewDynamic(it.Value, true)
    24  }
    25  
    26  func (it *KeyVal) KeyDynamicPtr() *Dynamic {
    27  	return NewDynamicPtr(it.Key, true)
    28  }
    29  
    30  func (it *KeyVal) ValueDynamicPtr() *Dynamic {
    31  	return NewDynamicPtr(it.Value, true)
    32  }
    33  
    34  func (it *KeyVal) IsKeyNull() bool {
    35  	return reflectinternal.IsNull(it.Key)
    36  }
    37  
    38  func (it *KeyVal) IsKeyNullOrEmptyString() bool {
    39  	return reflectinternal.IsNull(it.Key) || it.Key.(string) == ""
    40  }
    41  
    42  func (it *KeyVal) IsValueNull() bool {
    43  	return reflectinternal.IsNull(it.Value)
    44  }
    45  
    46  func (it *KeyVal) String() string {
    47  	return fmt.Sprintf(
    48  		constants.KeyValuePariSimpleFormat,
    49  		it.Key,
    50  		it.Key,
    51  		it.Value,
    52  		it.Value)
    53  }
    54  
    55  func (it *KeyVal) ValueReflectValue() reflect.Value {
    56  	return reflect.ValueOf(it.Value)
    57  }
    58  
    59  func (it *KeyVal) ValueInt() int {
    60  	casted, isSuccess := it.Value.(int)
    61  
    62  	if isSuccess {
    63  		return casted
    64  	}
    65  
    66  	return constants.InvalidValue
    67  }
    68  
    69  func (it *KeyVal) ValueUInt() uint {
    70  	casted, isSuccess := it.Value.(uint)
    71  
    72  	if isSuccess {
    73  		return casted
    74  	}
    75  
    76  	return constants.Zero
    77  }
    78  
    79  func (it *KeyVal) ValueStrings() []string {
    80  	casted, isSuccess := it.Value.([]string)
    81  
    82  	if isSuccess {
    83  		return casted
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func (it *KeyVal) ValueBool() bool {
    90  	casted, isSuccess := it.Value.(bool)
    91  
    92  	if isSuccess {
    93  		return casted
    94  	}
    95  
    96  	return false
    97  }
    98  
    99  func (it *KeyVal) ValueInt64() int64 {
   100  	casted, isSuccess := it.Value.(int64)
   101  
   102  	if isSuccess {
   103  		return casted
   104  	}
   105  
   106  	return constants.InvalidValue
   107  }
   108  
   109  func (it *KeyVal) CastKeyVal(
   110  	keyToPointer,
   111  	valueToPointer interface{},
   112  ) error {
   113  	if it == nil {
   114  		return errcore.
   115  			CannotBeNilOrEmptyType.
   116  			ErrorNoRefs("KeyVal is nil or null")
   117  	}
   118  
   119  	err := ReflectSetFromTo(it.Key, keyToPointer)
   120  
   121  	if err != nil {
   122  		return nil
   123  	}
   124  
   125  	return ReflectSetFromTo(it.Value, valueToPointer)
   126  }
   127  
   128  func (it *KeyVal) ReflectSetKey(
   129  	keyToPointer interface{},
   130  ) error {
   131  	if it == nil {
   132  		return errcore.
   133  			CannotBeNilOrEmptyType.
   134  			ErrorNoRefs("KeyVal is nil or null")
   135  	}
   136  
   137  	return ReflectSetFromTo(it.Key, keyToPointer)
   138  }
   139  
   140  func (it *KeyVal) ValueNullErr() error {
   141  	if it == nil {
   142  		return errcore.
   143  			CannotBeNilOrEmptyType.
   144  			ErrorNoRefs("KeyVal is nil or null")
   145  	}
   146  
   147  	if reflectinternal.IsNull(it.Value) {
   148  		return errcore.
   149  			CannotBeNilOrEmptyType.
   150  			Error("KeyVal.Value is nil or null, doesn't expect to be null.", "Key : "+it.KeyString())
   151  	}
   152  
   153  	return nil
   154  }
   155  
   156  func (it *KeyVal) KeyNullErr() error {
   157  	if it == nil {
   158  		return errcore.
   159  			CannotBeNilOrEmptyType.
   160  			ErrorNoRefs("KeyVal is nil or null")
   161  	}
   162  
   163  	if reflectinternal.IsNull(it.Key) {
   164  		return errcore.
   165  			CannotBeNilOrEmptyType.
   166  			Error("KeyVal.Key is nil or null, doesn't expect to be null.", "Value : "+it.ValueString())
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func (it *KeyVal) KeyString() string {
   173  	if it == nil || it.Key == nil {
   174  		return constants.EmptyString
   175  	}
   176  
   177  	return fmt.Sprintf(
   178  		constants.SprintValueFormat,
   179  		it.Key,
   180  	)
   181  }
   182  
   183  func (it *KeyVal) ValueString() string {
   184  	if it == nil || it.Value == nil {
   185  		return constants.EmptyString
   186  	}
   187  
   188  	return fmt.Sprintf(
   189  		constants.SprintValueFormat,
   190  		it.Value,
   191  	)
   192  }
   193  
   194  func (it *KeyVal) KeyReflectSet(toPointer interface{}) error {
   195  	if it == nil {
   196  		return errcore.
   197  			CannotBeNilOrEmptyType.
   198  			ErrorNoRefs("KeyVal is nil or null")
   199  	}
   200  
   201  	return ReflectSetFromTo(it.Key, toPointer)
   202  }
   203  
   204  func (it *KeyVal) ValueReflectSet(toPointer interface{}) error {
   205  	if it == nil {
   206  		return errcore.
   207  			CannotBeNilOrEmptyType.
   208  			ErrorNoRefs("KeyVal is nil or null")
   209  	}
   210  
   211  	return ReflectSetFromTo(it.Value, toPointer)
   212  }
   213  
   214  func (it *KeyVal) ReflectSetTo(toPointer interface{}) error {
   215  	if it == nil {
   216  		return errcore.
   217  			CannotBeNilOrEmptyType.
   218  			ErrorNoRefs("KeyVal is nil or null")
   219  	}
   220  
   221  	return ReflectSetFromTo(it.Value, toPointer)
   222  }
   223  
   224  func (it *KeyVal) ReflectSetToMust(toPointer interface{}) {
   225  	err := it.ReflectSetTo(toPointer)
   226  	errcore.MustBeEmpty(err)
   227  }
   228  
   229  func (it KeyVal) JsonModel() interface{} {
   230  	return it
   231  }
   232  
   233  func (it KeyVal) JsonModelAny() interface{} {
   234  	return it.JsonModel()
   235  }
   236  
   237  func (it KeyVal) Json() corejson.Result {
   238  	return corejson.New(it)
   239  }
   240  
   241  func (it KeyVal) JsonPtr() *corejson.Result {
   242  	return corejson.NewPtr(it)
   243  }
   244  
   245  //goland:noinspection GoLinterLocal
   246  func (it *KeyVal) ParseInjectUsingJson(
   247  	jsonResult *corejson.Result,
   248  ) (*KeyVal, error) {
   249  	err := jsonResult.Unmarshal(it)
   250  
   251  	if err != nil {
   252  		return nil, err
   253  	}
   254  
   255  	return it, nil
   256  }
   257  
   258  // ParseInjectUsingJsonMust Panic if error
   259  //goland:noinspection GoLinterLocal
   260  func (it *KeyVal) ParseInjectUsingJsonMust(
   261  	jsonResult *corejson.Result,
   262  ) *KeyVal {
   263  	newUsingJson, err :=
   264  		it.ParseInjectUsingJson(jsonResult)
   265  
   266  	if err != nil {
   267  		panic(err)
   268  	}
   269  
   270  	return newUsingJson
   271  }
   272  
   273  func (it *KeyVal) JsonParseSelfInject(
   274  	jsonResult *corejson.Result,
   275  ) error {
   276  	_, err := it.ParseInjectUsingJson(
   277  		jsonResult,
   278  	)
   279  
   280  	return err
   281  }
   282  
   283  func (it *KeyVal) Serialize() (jsonBytesPtr []byte, err error) {
   284  	jsonResult := it.Json()
   285  
   286  	if jsonResult.HasError() {
   287  		return []byte{}, jsonResult.MeaningfulError()
   288  	}
   289  
   290  	return jsonResult.SafeBytes(), nil
   291  }