| 208 | } |
| 209 | |
| 210 | func InterfaceToInt(in interface{}) (int, bool) { |
| 211 | var ok bool |
| 212 | if in, ok = JSONNumberToInt(in); !ok { |
| 213 | return 0, false |
| 214 | } |
| 215 | |
| 216 | switch casted := in.(type) { |
| 217 | case int8: |
| 218 | return int(casted), true |
| 219 | case int16: |
| 220 | return int(casted), true |
| 221 | case int32: |
| 222 | return int(casted), true |
| 223 | case int: |
| 224 | return casted, true |
| 225 | case int64: |
| 226 | if val := int(casted); int64(val) == casted { |
| 227 | return val, true |
| 228 | } |
| 229 | } |
| 230 | return 0, false |
| 231 | } |
| 232 | |
| 233 | func InterfaceToIntDowncast(in interface{}) (int, bool) { |
| 234 | var ok bool |