github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/calc_utils.go (about)

     1  // The package is migrated from beego, you can get from following link:
     2  // import(
     3  //
     4  //	"github.com/beego/beego/v2/client/cache"
     5  //
     6  // )
     7  // Copyright 2023. All Rights Reserved.
     8  //
     9  // Licensed under the Apache License, Version 2.0 (the "License");
    10  // you may not use this file except in compliance with the License.
    11  // You may obtain a copy of the License at
    12  //
    13  //	http://www.apache.org/licenses/LICENSE-2.0
    14  //
    15  // Unless required by applicable law or agreed to in writing, software
    16  // distributed under the License is distributed on an "AS IS" BASIS,
    17  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18  // See the License for the specific language governing permissions and
    19  // limitations under the License.
    20  package cache
    21  
    22  import (
    23  	"math"
    24  
    25  	"github.com/mdaxf/iac/framework/berror"
    26  )
    27  
    28  var (
    29  	ErrIncrementOverflow = berror.Error(IncrementOverflow, "this incr invocation will overflow.")
    30  	ErrDecrementOverflow = berror.Error(DecrementOverflow, "this decr invocation will overflow.")
    31  	ErrNotIntegerType    = berror.Error(NotIntegerType, "item val is not (u)int (u)int32 (u)int64")
    32  )
    33  
    34  const (
    35  	MinUint32 uint32 = 0
    36  	MinUint64 uint64 = 0
    37  )
    38  
    39  func incr(originVal interface{}) (interface{}, error) {
    40  	switch val := originVal.(type) {
    41  	case int:
    42  		tmp := val + 1
    43  		if val > 0 && tmp < 0 {
    44  			return nil, ErrIncrementOverflow
    45  		}
    46  		return tmp, nil
    47  	case int32:
    48  		if val == math.MaxInt32 {
    49  			return nil, ErrIncrementOverflow
    50  		}
    51  		return val + 1, nil
    52  	case int64:
    53  		if val == math.MaxInt64 {
    54  			return nil, ErrIncrementOverflow
    55  		}
    56  		return val + 1, nil
    57  	case uint:
    58  		tmp := val + 1
    59  		if tmp < val {
    60  			return nil, ErrIncrementOverflow
    61  		}
    62  		return tmp, nil
    63  	case uint32:
    64  		if val == math.MaxUint32 {
    65  			return nil, ErrIncrementOverflow
    66  		}
    67  		return val + 1, nil
    68  	case uint64:
    69  		if val == math.MaxUint64 {
    70  			return nil, ErrIncrementOverflow
    71  		}
    72  		return val + 1, nil
    73  	default:
    74  		return nil, ErrNotIntegerType
    75  	}
    76  }
    77  
    78  func decr(originVal interface{}) (interface{}, error) {
    79  	switch val := originVal.(type) {
    80  	case int:
    81  		tmp := val - 1
    82  		if val < 0 && tmp > 0 {
    83  			return nil, ErrDecrementOverflow
    84  		}
    85  		return tmp, nil
    86  	case int32:
    87  		if val == math.MinInt32 {
    88  			return nil, ErrDecrementOverflow
    89  		}
    90  		return val - 1, nil
    91  	case int64:
    92  		if val == math.MinInt64 {
    93  			return nil, ErrDecrementOverflow
    94  		}
    95  		return val - 1, nil
    96  	case uint:
    97  		if val == 0 {
    98  			return nil, ErrDecrementOverflow
    99  		}
   100  		return val - 1, nil
   101  	case uint32:
   102  		if val == MinUint32 {
   103  			return nil, ErrDecrementOverflow
   104  		}
   105  		return val - 1, nil
   106  	case uint64:
   107  		if val == MinUint64 {
   108  			return nil, ErrDecrementOverflow
   109  		}
   110  		return val - 1, nil
   111  	default:
   112  		return nil, ErrNotIntegerType
   113  	}
   114  }