gitlab.com/evatix-go/core@v1.3.55/coreimpl/enumimpl/BasicInt32.go (about)

     1  package enumimpl
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  	"gitlab.com/evatix-go/core/coreimpl/enumimpl/enumtype"
     8  	"gitlab.com/evatix-go/core/coreinterface"
     9  	"gitlab.com/evatix-go/core/defaulterr"
    10  	"gitlab.com/evatix-go/core/errcore"
    11  )
    12  
    13  type BasicInt32 struct {
    14  	numberEnumBase
    15  	jsonDoubleQuoteNameToValueHashMap        map[string]int32 // contains names double quotes to value
    16  	valueToJsonDoubleQuoteStringBytesHashmap map[int32][]byte // contains value to string bytes with double quotes
    17  	valueNameHashmap                         map[int32]string // contains name without double quotes
    18  	minVal, maxVal                           int32
    19  }
    20  
    21  func (it BasicInt32) IsAnyNamesOf(
    22  	value int32,
    23  	names ...string,
    24  ) bool {
    25  	currentName := it.ToEnumString(value)
    26  
    27  	for _, name := range names {
    28  		if name == currentName {
    29  			return true
    30  		}
    31  	}
    32  
    33  	return false
    34  }
    35  
    36  func (it BasicInt32) IsAnyOf(value int32, checkingItems ...int32) bool {
    37  	if len(checkingItems) == 0 {
    38  		return true
    39  	}
    40  
    41  	for _, givenByte := range checkingItems {
    42  		if value == givenByte {
    43  			return true
    44  		}
    45  	}
    46  
    47  	return false
    48  }
    49  
    50  func (it BasicInt32) Max() int32 {
    51  	return it.maxVal
    52  }
    53  
    54  func (it BasicInt32) Min() int32 {
    55  	return it.minVal
    56  }
    57  
    58  func (it BasicInt32) GetValueByString(valueString string) int32 {
    59  	return it.jsonDoubleQuoteNameToValueHashMap[valueString]
    60  }
    61  
    62  func (it BasicInt32) GetValueByName(name string) (int32, error) {
    63  	v, has := it.jsonDoubleQuoteNameToValueHashMap[name]
    64  
    65  	if has {
    66  		return v, nil
    67  	}
    68  
    69  	wrapped := fmt.Sprintf(
    70  		constants.SprintDoubleQuoteFormat,
    71  		name)
    72  
    73  	nextVal, isFoundByWrapped := it.jsonDoubleQuoteNameToValueHashMap[wrapped]
    74  
    75  	if isFoundByWrapped {
    76  		return nextVal, nil
    77  	}
    78  
    79  	// has error
    80  	return constants.InvalidValue, enumUnmarshallingMappingFailedError(
    81  		it.TypeName(),
    82  		name,
    83  		it.RangeNamesCsv())
    84  }
    85  
    86  func (it BasicInt32) GetStringValue(input int32) string {
    87  	return it.StringRanges()[input]
    88  }
    89  
    90  func (it BasicInt32) ExpectingEnumValueError(
    91  	rawString string,
    92  	expectedEnum interface{},
    93  ) error {
    94  	expectedEnumName := it.ToName(expectedEnum)
    95  	expectedValue := it.GetValueByString(expectedEnumName)
    96  	convValue, err := it.GetValueByName(rawString)
    97  
    98  	if err != nil {
    99  		return errcore.ExpectingErrorSimpleNoType(
   100  			"Expecting enum: "+expectedEnumName,
   101  			expectedEnumName,
   102  			rawString+err.Error())
   103  	}
   104  
   105  	if convValue == expectedValue {
   106  		return nil
   107  	}
   108  
   109  	return errcore.ExpectingErrorSimpleNoType(
   110  		"Expecting enum: "+expectedEnumName,
   111  		expectedEnumName,
   112  		rawString+it.RangesInvalidMessage())
   113  }
   114  
   115  func (it BasicInt32) Ranges() []int32 {
   116  	return it.actualValueRanges.([]int32)
   117  }
   118  
   119  func (it BasicInt32) Hashmap() map[string]int32 {
   120  	return it.jsonDoubleQuoteNameToValueHashMap
   121  }
   122  
   123  func (it BasicInt32) HashmapPtr() *map[string]int32 {
   124  	return &it.jsonDoubleQuoteNameToValueHashMap
   125  }
   126  
   127  func (it BasicInt32) IsValidRange(value int32) bool {
   128  	return value >= it.minVal && value <= it.maxVal
   129  }
   130  
   131  // ToEnumJsonBytes used for MarshalJSON from map
   132  func (it BasicInt32) ToEnumJsonBytes(value int32) ([]byte, error) {
   133  	jsonBytes, has := it.valueToJsonDoubleQuoteStringBytesHashmap[value]
   134  
   135  	if has {
   136  		return jsonBytes, nil
   137  	}
   138  
   139  	return []byte{}, it.notFoundJsonBytesError(value)
   140  }
   141  
   142  func (it BasicInt32) ToEnumString(value int32) string {
   143  	return it.valueNameHashmap[value]
   144  }
   145  
   146  func (it BasicInt32) AppendPrependJoinValue(
   147  	joiner string,
   148  	appendVal, prependVal int32,
   149  ) string {
   150  	return it.ToEnumString(prependVal) +
   151  		joiner +
   152  		it.ToEnumString(appendVal)
   153  }
   154  
   155  func (it BasicInt32) AppendPrependJoinNamer(
   156  	joiner string,
   157  	appendVal, prependVal coreinterface.ToNamer,
   158  ) string {
   159  	return prependVal.Name() +
   160  		joiner +
   161  		appendVal.Name()
   162  
   163  }
   164  
   165  func (it BasicInt32) ToNumberString(valueInRawFormat interface{}) string {
   166  	return fmt.Sprintf(constants.SprintValueFormat, valueInRawFormat)
   167  }
   168  
   169  // UnmarshallToValue Mostly used for UnmarshalJSON
   170  //
   171  // Given bytes string enum value and transpile to exact enum raw value using map
   172  func (it BasicInt32) UnmarshallToValue(
   173  	isMappedToFirstIfEmpty bool,
   174  	jsonUnmarshallingValue []byte,
   175  ) (int32, error) {
   176  	if !isMappedToFirstIfEmpty && jsonUnmarshallingValue == nil {
   177  		return constants.Zero,
   178  			defaulterr.UnmarshallingFailedDueToNilOrEmpty
   179  	}
   180  
   181  	if isMappedToFirstIfEmpty && jsonUnmarshallingValue == nil {
   182  		return it.minVal, nil
   183  	}
   184  
   185  	str := string(jsonUnmarshallingValue)
   186  	if isMappedToFirstIfEmpty &&
   187  		(str == constants.EmptyString || str == constants.DoubleQuotationStartEnd) {
   188  		return it.minVal, nil
   189  	}
   190  
   191  	return it.GetValueByName(str)
   192  }
   193  
   194  func (it BasicInt32) EnumType() enumtype.Variant {
   195  	return enumtype.Integer32
   196  }