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