github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/enum/enum.go (about)

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