gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/ValidValues.go (about)

     1  package corestr
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/conditional"
     5  	"gitlab.com/evatix-go/core/constants"
     6  	"gitlab.com/evatix-go/core/defaultcapacity"
     7  	"gitlab.com/evatix-go/core/internal/strutilinternal"
     8  )
     9  
    10  type ValidValues struct {
    11  	ValidValues []*ValidValue `json:"ValidValues,omitempty"`
    12  }
    13  
    14  func NewValidValuesUsingValues(values ...ValidValue) *ValidValues {
    15  	if len(values) == 0 {
    16  		return EmptyValidValues()
    17  	}
    18  
    19  	slice := make(
    20  		[]*ValidValue,
    21  		len(values))
    22  
    23  	for i, value := range values {
    24  		slice[i] = &value
    25  	}
    26  
    27  	return &ValidValues{
    28  		ValidValues: slice,
    29  	}
    30  }
    31  
    32  func NewValidValues(capacity int) *ValidValues {
    33  	slice := make([]*ValidValue, 0, capacity)
    34  
    35  	return &ValidValues{ValidValues: slice}
    36  }
    37  
    38  func EmptyValidValues() *ValidValues {
    39  	return NewValidValues(
    40  		constants.Zero)
    41  }
    42  
    43  func (it *ValidValues) Count() int {
    44  	return it.Length()
    45  }
    46  
    47  func (it *ValidValues) HasAnyItem() bool {
    48  	return it.Length() > 0
    49  }
    50  
    51  func (it *ValidValues) LastIndex() int {
    52  	return it.Length() - 1
    53  }
    54  
    55  func (it *ValidValues) HasIndex(
    56  	index int,
    57  ) bool {
    58  	return index != constants.InvalidNotFoundCase && it.LastIndex() >= index
    59  }
    60  
    61  func (it *ValidValues) Find(
    62  	finder func(index int, valueValid *ValidValue) (foundItem *ValidValue, isFound, isBreak bool),
    63  ) []*ValidValue {
    64  	length := it.Length()
    65  
    66  	if length == 0 {
    67  		return []*ValidValue{}
    68  	}
    69  
    70  	slice := make(
    71  		[]*ValidValue,
    72  		0,
    73  		defaultcapacity.OfSearch(length))
    74  
    75  	for i, item := range it.ValidValues {
    76  		foundItem, isFound, isBreak := finder(i, item)
    77  
    78  		if isFound && foundItem != nil {
    79  			slice = append(slice, foundItem)
    80  		}
    81  
    82  		if isBreak {
    83  			return slice
    84  		}
    85  	}
    86  
    87  	return slice
    88  }
    89  
    90  func (it *ValidValues) SafeValueAt(index int) string {
    91  	if it.IsEmpty() {
    92  		return constants.EmptyString
    93  	}
    94  
    95  	if it.HasIndex(index) {
    96  		return it.ValidValues[index].Value
    97  	}
    98  
    99  	return constants.EmptyString
   100  }
   101  
   102  func (it *ValidValues) SafeValidValueAt(index int) string {
   103  	if it.IsEmpty() {
   104  		return constants.EmptyString
   105  	}
   106  
   107  	if it.HasIndex(index) {
   108  		validVal := it.ValidValues[index]
   109  
   110  		return conditional.String(
   111  			validVal.IsValid,
   112  			validVal.Value,
   113  			constants.EmptyString)
   114  	}
   115  
   116  	return constants.EmptyString
   117  }
   118  
   119  func (it *ValidValues) SafeValuesAtIndexes(indexes ...int) []string {
   120  	requestLength := len(indexes)
   121  	slice := make([]string, requestLength)
   122  
   123  	if requestLength == 0 {
   124  		return slice
   125  	}
   126  
   127  	for i, index := range indexes {
   128  		slice[i] = it.SafeValueAt(index)
   129  	}
   130  
   131  	return slice
   132  }
   133  
   134  func (it *ValidValues) SafeValidValuesAtIndexes(indexes ...int) []string {
   135  	requestLength := len(indexes)
   136  	slice := make([]string, requestLength)
   137  
   138  	if requestLength == 0 {
   139  		return slice
   140  	}
   141  
   142  	for i, index := range indexes {
   143  		slice[i] = it.SafeValidValueAt(index)
   144  	}
   145  
   146  	return slice
   147  }
   148  
   149  func (it *ValidValues) Strings() []string {
   150  	if it.IsEmpty() {
   151  		return []string{}
   152  	}
   153  
   154  	slice := make([]string, it.Length())
   155  
   156  	for i, val := range it.ValidValues {
   157  		slice[i] = val.String()
   158  	}
   159  
   160  	return slice
   161  }
   162  
   163  func (it *ValidValues) FullStrings() []string {
   164  	if it.IsEmpty() {
   165  		return []string{}
   166  	}
   167  
   168  	slice := make([]string, it.Length())
   169  
   170  	for i, val := range it.ValidValues {
   171  		slice[i] = val.FullString()
   172  	}
   173  
   174  	return slice
   175  }
   176  
   177  func (it *ValidValues) String() string {
   178  	return strutilinternal.AnyToString(it.Strings())
   179  }
   180  
   181  func (it *ValidValues) Length() int {
   182  	if it == nil {
   183  		return 0
   184  	}
   185  
   186  	return len(it.ValidValues)
   187  }
   188  
   189  func (it *ValidValues) IsEmpty() bool {
   190  	return it.Length() == 0
   191  }
   192  
   193  func (it *ValidValues) Add(val string) *ValidValues {
   194  	it.ValidValues = append(it.ValidValues, &ValidValue{
   195  		Value:   val,
   196  		IsValid: true,
   197  		Message: constants.EmptyString,
   198  	})
   199  
   200  	return it
   201  }
   202  
   203  func (it *ValidValues) AddFull(isValid bool, val, message string) *ValidValues {
   204  	it.ValidValues = append(it.ValidValues, &ValidValue{
   205  		Value:   val,
   206  		IsValid: isValid,
   207  		Message: message,
   208  	})
   209  
   210  	return it
   211  }
   212  
   213  func (it *ValidValues) ConcatNew(
   214  	isCloneOnEmpty bool,
   215  	validValuesCollection ...*ValidValues,
   216  ) *ValidValues {
   217  	isEmpty := validValuesCollection == nil || len(validValuesCollection) == 0
   218  
   219  	if isEmpty && isCloneOnEmpty {
   220  		newValues := NewValidValues(it.Length())
   221  
   222  		return newValues.AddsPtr(it.ValidValues...)
   223  	}
   224  
   225  	if isEmpty && !isCloneOnEmpty {
   226  		return it
   227  	}
   228  
   229  	newValues := NewValidValues(it.Length())
   230  	newValues.AddsPtr(it.ValidValues...)
   231  
   232  	for _, validValues := range validValuesCollection {
   233  		newValues.AddValidValues(validValues)
   234  	}
   235  
   236  	return newValues
   237  }
   238  
   239  func (it *ValidValues) AddValidValues(validValues *ValidValues) *ValidValues {
   240  	if validValues == nil || validValues.IsEmpty() {
   241  		return it
   242  	}
   243  
   244  	return it.AddsPtr(validValues.ValidValues...)
   245  }
   246  
   247  func (it *ValidValues) Adds(validValues ...ValidValue) *ValidValues {
   248  	if len(validValues) == 0 {
   249  		return it
   250  	}
   251  
   252  	for _, validValue := range validValues {
   253  		it.ValidValues = append(
   254  			it.ValidValues, &validValue)
   255  	}
   256  
   257  	return it
   258  }
   259  
   260  func (it *ValidValues) AddsPtr(validValues ...*ValidValue) *ValidValues {
   261  	if len(validValues) == 0 {
   262  		return it
   263  	}
   264  
   265  	for _, validValue := range validValues {
   266  		it.ValidValues = append(
   267  			it.ValidValues, validValue)
   268  	}
   269  
   270  	return it
   271  }
   272  
   273  func (it *ValidValues) AddHashsetMap(
   274  	inputMap map[string]bool,
   275  ) *ValidValues {
   276  	if inputMap == nil || len(inputMap) == 0 {
   277  		return it
   278  	}
   279  
   280  	for val, isValid := range inputMap {
   281  		it.ValidValues = append(it.ValidValues, &ValidValue{
   282  			Value:   val,
   283  			IsValid: isValid,
   284  			Message: constants.EmptyString,
   285  		})
   286  	}
   287  
   288  	return it
   289  }
   290  
   291  func (it *ValidValues) AddHashset(
   292  	inputHashset *Hashset,
   293  ) *ValidValues {
   294  	if inputHashset == nil || inputHashset.IsEmpty() {
   295  		return it
   296  	}
   297  
   298  	return it.AddHashsetMap(inputHashset.items)
   299  }
   300  
   301  func (it *ValidValues) Hashmap() *Hashmap {
   302  	length := it.Length()
   303  	hashmap := New.Hashmap.Cap(length)
   304  
   305  	if length == 0 {
   306  		return hashmap
   307  	}
   308  
   309  	for _, keyVal := range it.ValidValues {
   310  		hashmap.AddOrUpdate(keyVal.Value, keyVal.Message)
   311  	}
   312  
   313  	return hashmap
   314  }
   315  
   316  func (it *ValidValues) Map() map[string]string {
   317  	hashmap := it.Hashmap()
   318  
   319  	return hashmap.items
   320  }