MCPcopy
hub / github.com/pelletier/go-toml / makeMapKey

Function makeMapKey

unmarshaler.go:1268–1322  ·  view source on GitHub ↗

makeMapKey converts a TOML key into a value usable as the given map key type.

(kt reflect.Type, name string)

Source from the content-addressed store, hash-verified

1266// makeMapKey converts a TOML key into a value usable as the given map key
1267// type.
1268func makeMapKey(kt reflect.Type, name string) (reflect.Value, error) {
1269 switch kt.Kind() {
1270 case reflect.String:
1271 return reflect.ValueOf(name).Convert(kt), nil
1272 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1273 i, err := strconv.ParseInt(name, 10, 64)
1274 if err != nil {
1275 return reflect.Value{}, fmt.Errorf("toml: cannot parse map key %q as %s: %w", name, kt, err)
1276 }
1277 k := reflect.New(kt).Elem()
1278 if k.OverflowInt(i) {
1279 return reflect.Value{}, fmt.Errorf("toml: map key %q overflows %s", name, kt)
1280 }
1281 k.SetInt(i)
1282 return k, nil
1283 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
1284 u, err := strconv.ParseUint(name, 10, 64)
1285 if err != nil {
1286 return reflect.Value{}, fmt.Errorf("toml: cannot parse map key %q as %s: %w", name, kt, err)
1287 }
1288 k := reflect.New(kt).Elem()
1289 if k.OverflowUint(u) {
1290 return reflect.Value{}, fmt.Errorf("toml: map key %q overflows %s", name, kt)
1291 }
1292 k.SetUint(u)
1293 return k, nil
1294 case reflect.Float32, reflect.Float64:
1295 f, err := strconv.ParseFloat(name, 64)
1296 if err != nil {
1297 return reflect.Value{}, fmt.Errorf("toml: cannot parse map key %q as %s: %w", name, kt, err)
1298 }
1299 k := reflect.New(kt).Elem()
1300 k.SetFloat(f)
1301 return k, nil
1302 case reflect.Ptr:
1303 if kt.Implements(textUnmarshalerType) {
1304 k := reflect.New(kt.Elem())
1305 err := k.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(name))
1306 if err != nil {
1307 return reflect.Value{}, fmt.Errorf("toml: error unmarshaling map key %q: %w", name, err)
1308 }
1309 return k, nil
1310 }
1311 default:
1312 if reflect.PtrTo(kt).Implements(textUnmarshalerType) {
1313 k := reflect.New(kt)
1314 err := k.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(name))
1315 if err != nil {
1316 return reflect.Value{}, fmt.Errorf("toml: error unmarshaling map key %q: %w", name, err)
1317 }
1318 return k.Elem(), nil
1319 }
1320 }
1321 return reflect.Value{}, fmt.Errorf("toml: cannot decode a key into a map with key type %s", kt)
1322}
1323
1324// elemOrNewMap unwraps an interface value to descend into it. Contents that
1325// can hold a table (generic maps and slices) are kept; anything else is

Callers 3

walkTableMethod · 0.85
resolveCaptureMethod · 0.85
descendMethod · 0.85

Calls 1

UnmarshalTextMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…