MCPcopy Create free account
hub / github.com/amp-labs/connectors / AsInt

Method AsInt

common/stringMap.go:91–109  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
91func (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
111func (m StringMap) GetString(key string) (string, error) {
112 val, err := m.Get(key)

Callers 5

TestStringMapFunction · 0.95
EventTimeStampNanoMethod · 0.80
WorkspaceMethod · 0.80
RecordIdMethod · 0.80
EventTimeStampNanoMethod · 0.80

Calls 1

GetMethod · 0.95

Tested by 1

TestStringMapFunction · 0.76