kubernetes/kops · error
unhandled type %v %q
Error message
unhandled type %v %q
What it means
During auto-population of a nil pointer on a partial path match, the library only knows how to allocate a pointer to a struct (reflect.New of a struct). If the nil pointer points to any other kind (string, int, map, slice, etc.) it cannot auto-create it and returns this error naming the element kind and type.
Source
Thrown at util/pkg/reflectutils/access.go:91
}
}
// Partial match, check for nil struct and auto-populate
if v.Kind() == reflect.Ptr && v.IsNil() {
if !v.CanSet() {
return fmt.Errorf("cannot set field %q (marked immutable)", path)
}
t := v.Type().String()
var newV reflect.Value
switch v.Type().Elem().Kind() {
case reflect.Struct:
newV = reflect.New(v.Type().Elem())
default:
return fmt.Errorf("unhandled type %v %q", v.Type().Elem().Kind(), t)
}
v.Set(newV)
return nil
}
return nil
}
err = ReflectRecursive(targetValue, visitor, &ReflectOptions{JSONNames: true})
if err != nil {
return err
}
if !fieldSet {
return fmt.Errorf("field %s not found in %s", targetPath, BuildTypeName(reflect.TypeOf(target)))
}View on GitHub (pinned to 4c8573c808)
Solutions
- Initialize the pointer field before calling SetString (e.g. s := ""; obj.Field = &s).
- Restructure the target so the path traverses pointer-to-struct instead of pointer-to-scalar.
- Set the leaf value directly instead of via a path that traverses the nil scalar pointer.
Example fix
// before obj.MaxPrice = nil // *string, SetString cannot auto-create reflectutils.SetString(&obj, "spec.maxPrice.value", "100") // after obj.MaxPrice = new(string) reflectutils.SetString(&obj, "spec.maxPrice.value", "100")
Defensive patterns
Strategy: validation
Validate before calling
// Fail fast if the path traverses a nil pointer-to-scalar
if obj.MaxPrice == nil {
obj.MaxPrice = new(string)
} Type guard
func isPtrToStruct(v reflect.Value) bool {
return v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct
} Try / catch
if err := SetString(&obj, path, val); err != nil {
if strings.Contains(err.Error(), "unhandled type") {
return fmt.Errorf("path %q traverses a pointer SetString cannot allocate; initialize it first", path)
}
return err
} Prevention
- Prefer pointer-to-struct over pointer-to-scalar in mutation-path types.
- Initialize nil pointers before calling SetString.
- Prefer direct field assignment for exotic types.
When it happens
Trigger: SetString with a path traversing a nil *string/*int/*map field, e.g. targetPath "spec.someNilStringPtr.sub" where someNilStringPtr is *string and nil.
Common situations: Kops API objects gained new pointer-typed (non-struct) fields; users scripting `kops set` against paths that traverse such pointers, or custom structs fed to SetString that use *string indirection.
Related errors
- unhandled type %q
- conversion error for field %s: %s
- unable to convert from Quantity %v to float
- BuildFlagsList to reflect value: %s
- only accepts structs; got %T
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/7bcdfd176b0fa173.
Report an issue: GitHub.