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