AsInt is a helper function that extracts an integer from the map. This function will convert number values such as int, uint, and float to int64. By doing so, it may lose precision for large numbers. This is helpful when you are not sure about the type of the number.
(key string)
| 89 | // By doing so, it may lose precision for large numbers. |
| 90 | // This is helpful when you are not sure about the type of the number. |
| 91 | func (m StringMap) AsInt(key string) (int64, error) { |
| 92 | val, err := m.Get(key) |
| 93 | if err != nil { |
| 94 | return 0, err |
| 95 | } |
| 96 | |
| 97 | t := reflect.TypeOf(val) |
| 98 | //nolint:exhaustive |
| 99 | switch t.Kind() { |
| 100 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 101 | return reflect.ValueOf(val).Int(), nil |
| 102 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 103 | return int64(reflect.ValueOf(val).Uint()), nil //nolint:gosec |
| 104 | case reflect.Float32, reflect.Float64: |
| 105 | return int64(reflect.ValueOf(val).Float()), nil |
| 106 | default: |
| 107 | return 0, fmt.Errorf("%w: expected a number, but received %T", errNotANumber, val) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (m StringMap) GetString(key string) (string, error) { |
| 112 | val, err := m.Get(key) |