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