{"record":{"id":"f278860ac3c5ffe7","repo":"go-kratos/kratos","slug":"type-assert-to-v-failed","errorCode":null,"errorMessage":"type assert to %v failed","messagePattern":"type assert to (.+?) failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"config/value.go","lineNumber":39,"sourceCode":"type Value interface {\n\tBool() (bool, error)\n\tInt() (int64, error)\n\tFloat() (float64, error)\n\tString() (string, error)\n\tDuration() (time.Duration, error)\n\tSlice() ([]Value, error)\n\tMap() (map[string]Value, error)\n\tScan(any) error\n\tLoad() any\n\tStore(any)\n}\n\ntype atomicValue struct {\n\tatomic.Value\n}\n\nfunc (v *atomicValue) typeAssertError() error {\n\treturn fmt.Errorf(\"type assert to %v failed\", reflect.TypeOf(v.Load()))\n}\n\nfunc (v *atomicValue) Bool() (bool, error) {\n\tswitch val := v.Load().(type) {\n\tcase bool:\n\t\treturn val, nil\n\tcase int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:\n\t\treturn strconv.ParseBool(fmt.Sprint(val))\n\tcase string:\n\t\treturn strconv.ParseBool(val)\n\t}\n\treturn false, v.typeAssertError()\n}\n\nfunc (v *atomicValue) Int() (int64, error) {\n\tswitch val := v.Load().(type) {\n\tcase int:\n\t\treturn int64(val), nil","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/go-kratos/kratos/blob/668db92c2c001e9552594ba5a8aede8456af6d7e/config/value.go#L21-L57","documentation":"config.Value is backed by an atomic.Value; typed accessors (Bool, Int, Float, Duration, ...) try a direct type switch plus string/number conversions, and when none apply they return this error naming the actually stored type. It means the config value's runtime type does not fit the requested accessor.","triggerScenarios":"Calling a typed accessor on a config Value whose stored type does not match — Bool() on a map or slice, Int() on a list, Duration() on a non-string, or after Store() of an arbitrary type.","commonSituations":"Numbers or booleans quoted as strings after env overrides; accessing a tree node (always a map) with a scalar accessor; config schema drift between environments.","solutions":["Inspect the raw value with v.Load() (the error already prints the real type via %T)","Fix the config so the value matches the accessor (unquote numbers, use true/false for booleans)","Prefer Value.Scan into typed structs over scalar accessors"],"exampleFix":"// before\nport := c.Value(\"data.database.port\").Int() // underlying is string \"3306\"\n\n// after\nport, err := strconv.Atoi(c.Value(\"data.database.port\").String())\n// or fix the config to an unquoted number: port: 3306","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func isBoolLike(v config.Value) bool {\n    switch v.Load().(type) {\n    case bool, int, int64, float64, string:\n        return true\n    }\n    return false\n}","tryCatchPattern":"b, err := v.Bool()\nif err != nil {\n    if strings.Contains(err.Error(), \"type assert\") {\n        // wrong underlying type: inspect v.Load() and fix the config\n        return handleTypeMismatch(v.Load())\n    }\n    return err\n}","preventionTips":["Keep config scalars unquoted (numbers, true/false)","Use Scan into typed structs instead of per-key scalar accessors","Validate config shape at startup by decoding into a struct"],"tags":["config","type-assertion","atomic-value","go"],"backgroundTag":null,"analyzedSha":"668db92c2c001e9552594ba5a8aede8456af6d7e","analyzedAt":"2026-08-16T02:07:20.704Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}