gitlab.com/evatix-go/core@v1.3.55/coredata/corerange/within.go (about)

     1  package corerange
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  	"gitlab.com/evatix-go/core/constants/bitsize"
     9  )
    10  
    11  type within struct{}
    12  
    13  func (it *within) StringRangeInt32(
    14  	input string,
    15  ) (val int32, isInRange bool) {
    16  	finalInt, isInRange := it.StringRangeInteger(
    17  		true,
    18  		math.MinInt32,
    19  		math.MaxInt32,
    20  		input)
    21  
    22  	return int32(finalInt), isInRange
    23  }
    24  
    25  func (it *within) StringRangeInt16(
    26  	input string,
    27  ) (val int16, isInRange bool) {
    28  	finalInt, isInRange := it.StringRangeInteger(
    29  		true,
    30  		math.MinInt16,
    31  		math.MaxInt16,
    32  		input)
    33  
    34  	return int16(finalInt), isInRange
    35  }
    36  
    37  func (it *within) StringRangeInt8(
    38  	input string,
    39  ) (val int8, isInRange bool) {
    40  	finalInt, isInRange := it.StringRangeInteger(
    41  		true,
    42  		math.MinInt8,
    43  		math.MaxInt8,
    44  		input)
    45  
    46  	return int8(finalInt), isInRange
    47  }
    48  
    49  func (it *within) StringRangeByte(
    50  	input string,
    51  ) (val byte, isInRange bool) {
    52  	finalInt, isInRange := it.StringRangeInteger(
    53  		true,
    54  		constants.Zero,
    55  		math.MaxUint8,
    56  		input)
    57  
    58  	return byte(finalInt), isInRange
    59  }
    60  
    61  func (it *within) StringRangeUint16(
    62  	input string,
    63  ) (val uint16, isInRange bool) {
    64  	finalInt, isInRange := it.StringRangeInteger(
    65  		true,
    66  		constants.Zero,
    67  		math.MaxUint16,
    68  		input)
    69  
    70  	return uint16(finalInt), isInRange
    71  }
    72  
    73  func (it *within) StringRangeUint32(
    74  	input string,
    75  ) (val uint32, isInRange bool) {
    76  	finalInt, isInRange := it.StringRangeInteger(
    77  		true,
    78  		constants.Zero,
    79  		math.MaxInt32,
    80  		input)
    81  
    82  	// fix https://t.ly/6aoW,
    83  	// https://gitlab.com/evatix-go/core/-/issues/81
    84  	// use MaxInt32 instead of uint32Max
    85  	if finalInt <= math.MaxInt32 {
    86  		return uint32(finalInt), isInRange
    87  	}
    88  
    89  	return 0, isInRange
    90  }
    91  
    92  func (it *within) StringRangeIntegerDefault(
    93  	min, max int,
    94  	input string,
    95  ) (val int, isInRange bool) {
    96  	toInt, err := strconv.ParseInt(
    97  		input,
    98  		10,
    99  		64)
   100  
   101  	if err != nil {
   102  		return constants.Zero, false
   103  	}
   104  
   105  	isInRange = toInt >= int64(min) &&
   106  		toInt <= int64(max)
   107  
   108  	if isInRange {
   109  		return int(toInt), isInRange
   110  	}
   111  
   112  	isLessMin := toInt < int64(min)
   113  	if isLessMin {
   114  		return min, false
   115  	}
   116  
   117  	// above
   118  	return max, false
   119  }
   120  
   121  func (it *within) StringRangeInteger(
   122  	isUsageMinMaxBoundary bool,
   123  	min, max int,
   124  	input string,
   125  ) (val int, isInRange bool) {
   126  	toInt, err := strconv.Atoi(input)
   127  
   128  	if err != nil {
   129  		return constants.Zero, false
   130  	}
   131  
   132  	return it.RangeInteger(
   133  		isUsageMinMaxBoundary,
   134  		min,
   135  		max,
   136  		toInt)
   137  }
   138  
   139  func (it *within) StringRangeFloat(
   140  	isUsageMinMaxBoundary bool,
   141  	min, max float32,
   142  	input string,
   143  ) (val float32, isInRange bool) {
   144  	toFloat64, err := strconv.ParseFloat(input, bitsize.Of32)
   145  
   146  	if err != nil {
   147  		return constants.Zero, false
   148  	}
   149  
   150  	rangedValue, isInRange := it.RangeFloat64(
   151  		isUsageMinMaxBoundary,
   152  		float64(min),
   153  		float64(max),
   154  		toFloat64)
   155  
   156  	if isInRange || isUsageMinMaxBoundary {
   157  		return float32(rangedValue), isInRange
   158  	}
   159  
   160  	return constants.Zero, isInRange
   161  }
   162  
   163  func (it *within) StringRangeFloatDefault(
   164  	input string,
   165  ) (val float32, isInRange bool) {
   166  	toFloat64, err := strconv.ParseFloat(input, bitsize.Of32)
   167  
   168  	if err != nil {
   169  		return constants.Zero, false
   170  	}
   171  
   172  	rangedValue, isInRange := it.RangeFloat64(
   173  		true,
   174  		math.SmallestNonzeroFloat32,
   175  		math.MaxFloat32,
   176  		toFloat64)
   177  
   178  	return float32(rangedValue), isInRange
   179  }
   180  
   181  func (it *within) StringRangeFloat64(
   182  	isUsageMinMaxBoundary bool,
   183  	min, max float64,
   184  	input string,
   185  ) (val float64, isInRange bool) {
   186  	toFloat, err := strconv.ParseFloat(input, bitsize.Of64)
   187  
   188  	if err != nil {
   189  		return constants.Zero, false
   190  	}
   191  
   192  	return it.RangeFloat64(
   193  		isUsageMinMaxBoundary,
   194  		min,
   195  		max,
   196  		toFloat)
   197  }
   198  
   199  func (it *within) StringRangeFloat64Default(
   200  	input string,
   201  ) (val float64, isInRange bool) {
   202  	toFloat, err := strconv.ParseFloat(input, bitsize.Of64)
   203  
   204  	if err != nil {
   205  		return constants.Zero, false
   206  	}
   207  
   208  	return it.RangeFloat64(
   209  		true,
   210  		math.SmallestNonzeroFloat32,
   211  		math.MaxFloat32,
   212  		toFloat)
   213  }
   214  
   215  func (it *within) RangeDefaultInteger(
   216  	min, max, input int,
   217  ) (val int, isInRange bool) {
   218  	return it.RangeInteger(
   219  		true,
   220  		min,
   221  		max,
   222  		input)
   223  }
   224  
   225  func (it *within) RangeInteger(
   226  	isUsageMinMaxBoundary bool,
   227  	min, max,
   228  	input int,
   229  ) (val int, isInRange bool) {
   230  	if input >= min && input <= max {
   231  		return input, true
   232  	}
   233  
   234  	if !isUsageMinMaxBoundary {
   235  		return input, false
   236  	}
   237  
   238  	if input < min {
   239  		return min, false
   240  	}
   241  
   242  	return max, false
   243  }
   244  
   245  func (it *within) RangeByteDefault(
   246  	input int,
   247  ) (val byte, isInRange bool) {
   248  	return it.RangeByte(
   249  		true,
   250  		input)
   251  }
   252  
   253  func (it *within) RangeByte(
   254  	isUsageMinMaxBoundary bool,
   255  	input int,
   256  ) (val byte, isInRange bool) {
   257  	if input >= constants.Zero && input <= math.MaxUint8 {
   258  		return byte(input), true
   259  	}
   260  
   261  	if !isUsageMinMaxBoundary {
   262  		return constants.Zero, false
   263  	}
   264  
   265  	if input < constants.Zero {
   266  		return constants.Zero, false
   267  	}
   268  
   269  	return math.MaxUint8, false
   270  }
   271  
   272  func (it *within) RangeUint16Default(
   273  	input int,
   274  ) (val uint16, isInRange bool) {
   275  	return it.RangeUint16(
   276  		true,
   277  		input)
   278  }
   279  
   280  func (it *within) RangeUint16(
   281  	isUsageMinMaxBoundary bool,
   282  	input int,
   283  ) (val uint16, isInRange bool) {
   284  	toInt, isInRange := it.RangeInteger(
   285  		isUsageMinMaxBoundary,
   286  		constants.Zero,
   287  		math.MaxUint16,
   288  		input)
   289  
   290  	if isInRange || isUsageMinMaxBoundary {
   291  		return uint16(toInt), isInRange
   292  	}
   293  
   294  	return constants.Zero, isInRange
   295  }
   296  
   297  func (it *within) RangeFloat(
   298  	isUsageMinMaxBoundary bool,
   299  	min, max,
   300  	input float32,
   301  ) (val float32, isInRange bool) {
   302  	if input >= min && input <= max {
   303  		return input, true
   304  	}
   305  
   306  	if !isUsageMinMaxBoundary {
   307  		return input, false
   308  	}
   309  
   310  	if input < min {
   311  		return min, false
   312  	}
   313  
   314  	return max, false
   315  }
   316  
   317  func (it *within) RangeFloat64(
   318  	isUsageMinMaxBoundary bool,
   319  	min, max,
   320  	input float64,
   321  ) (val float64, isInRange bool) {
   322  	if input >= min && input <= max {
   323  		return input, true
   324  	}
   325  
   326  	if !isUsageMinMaxBoundary {
   327  		return input, false
   328  	}
   329  
   330  	if input < min {
   331  		return min, false
   332  	}
   333  
   334  	return max, false
   335  }