convertToInt converts an interface to an int
(Value interface{})
| 124 | |
| 125 | // convertToInt converts an interface to an int |
| 126 | func convertToInt(Value interface{}) (int, error) { |
| 127 | switch value := Value.(type) { |
| 128 | case int: |
| 129 | return value, nil |
| 130 | case int32: |
| 131 | return int(value), nil |
| 132 | case int64: |
| 133 | return int(value), nil |
| 134 | case float32: |
| 135 | return int(value), nil |
| 136 | case float64: |
| 137 | return int(value), nil |
| 138 | case uint: |
| 139 | return int(value), nil |
| 140 | case uint8: |
| 141 | return int(value), nil |
| 142 | case uint16: |
| 143 | return int(value), nil |
| 144 | case uint32: |
| 145 | return int(value), nil |
| 146 | case uint64: |
| 147 | return int(value), nil |
| 148 | case string: |
| 149 | intValue, err := strconv.Atoi(value) |
| 150 | if err != nil { |
| 151 | return 0, err |
| 152 | } |
| 153 | return intValue, nil |
| 154 | case []byte: |
| 155 | strValue := string(value) |
| 156 | intValue, err := strconv.Atoi(strValue) |
| 157 | if err != nil { |
| 158 | return 0, err |
| 159 | } |
| 160 | return intValue, nil |
| 161 | default: |
| 162 | return 0, errors.New("unsupported type") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // convertToFloat converts an interface to a float64 |
| 167 | func convertToFloat(Value interface{}) (float64, error) { |