github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/enumeration/helpers.go (about)

     1  package enumeration
     2  
     3  import "strconv"
     4  
     5  func ScanEnum(src interface{}, offset int) (int, error) {
     6  	return ScanIntEnumStringer(src, offset)
     7  }
     8  
     9  func ScanIntEnumStringer(src interface{}, offset int) (int, error) {
    10  	n, err := toInteger(src, offset)
    11  	if err != nil {
    12  		return offset, err
    13  	}
    14  	return n - offset, nil
    15  }
    16  
    17  func toInteger(src interface{}, defaultInteger int) (int, error) {
    18  	switch v := src.(type) {
    19  	case []byte:
    20  		if len(v) > 0 {
    21  			i, err := strconv.ParseInt(string(v), 10, 64)
    22  			if err != nil {
    23  				return defaultInteger, err
    24  			}
    25  			return int(i), err
    26  		}
    27  		return defaultInteger, nil
    28  	case string:
    29  		if v != "" {
    30  			i, err := strconv.ParseInt(v, 10, 64)
    31  			if err != nil {
    32  				return defaultInteger, err
    33  			}
    34  			return int(i), err
    35  		}
    36  		return defaultInteger, nil
    37  	case int:
    38  		return v, nil
    39  	case int8:
    40  		return int(v), nil
    41  	case int16:
    42  		return int(v), nil
    43  	case int32:
    44  		return int(v), nil
    45  	case int64:
    46  		return int(v), nil
    47  	case uint:
    48  		return int(v), nil
    49  	case uint8:
    50  		return int(v), nil
    51  	case uint16:
    52  		return int(v), nil
    53  	case uint32:
    54  		return int(v), nil
    55  	case uint64:
    56  		return int(v), nil
    57  	case nil:
    58  		return defaultInteger, nil
    59  	default:
    60  		return defaultInteger, nil
    61  	}
    62  }