{"record":{"id":"6ceb0d8dbf9ccd03","repo":"wavetermdev/waveterm","slug":"cannot-set-value-of-type-v-to-field-of-type-v-6ceb0d","errorCode":null,"errorMessage":"cannot set value of type %v to field of type %v","messagePattern":"cannot set value of type (.+?) to field of type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tsunami/util/marshal.go","lineNumber":120,"sourceCode":"\n\t// Check if types are assignable\n\tif valueRef.Type().AssignableTo(field.Type()) {\n\t\tfield.Set(valueRef)\n\t\treturn nil\n\t}\n\n\t// If field is pointer and value isn't already a pointer, try address\n\tif field.Kind() == reflect.Ptr && valueRef.Kind() != reflect.Ptr {\n\t\treturn setValue(field, valueRef.Addr().Interface())\n\t}\n\n\t// Try conversion if types are convertible\n\tif valueRef.Type().ConvertibleTo(field.Type()) {\n\t\tfield.Set(valueRef.Convert(field.Type()))\n\t\treturn nil\n\t}\n\n\treturn fmt.Errorf(\"cannot set value of type %v to field of type %v\", valueRef.Type(), field.Type())\n}","sourceCodeStart":102,"sourceCodeEnd":121,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/tsunami/util/marshal.go#L102-L121","documentation":"setValue is the reflection helper behind MapToStruct. After direct assignment, assignability, pointer-addressing, and convertibility checks all fail, it reports that the value's dynamic type cannot be stored in the destination field type. Structs with same-layout mismatched types (e.g. named string types in different packages, map into slice) land here.","triggerScenarios":"MapToStruct encounters a value whose type is neither equal, assignable, addressable-for-pointer, nor convertible to the field type: e.g. map[string]any into a []string field, json.Number into time.Time, two distinct named types with different underlying kinds.","commonSituations":"Round-tripping data through JSON (everything becomes float64/string/bool/[]any/map[string]any) into structs with typed slices, nested structs, or time.Time fields; refactoring a field type without updating producers.","solutions":["Align the map value's type with the field type before calling MapToStruct (convert slices/maps explicitly).","Change the struct field to a type compatible with JSON-decoded shapes (e.g. []any then convert, or a custom UnmarshalJSON type).","If the types should be convertible but aren't, check for named-type/kind mismatches and add an explicit conversion step."],"exampleFix":"// before\nm := map[string]any{\"tags\": []any{\"a\", \"b\"}}\nutil.MapToStruct(m, &cfg) // Tags []string fails\n// after\nm[\"tags\"] = []string{\"a\", \"b\"}\nutil.MapToStruct(m, &cfg)","handlingStrategy":"validation","validationCode":"func setValueSafe(field reflect.Value, value any) (err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"cannot set %T into %s\", value, field.Type())\n        }\n    }()\n    if value != nil && reflect.TypeOf(value).ConvertibleTo(field.Type()) {\n        field.Set(reflect.ValueOf(value).Convert(field.Type()))\n    }\n    return nil\n}","typeGuard":"func fieldCompatible(value any, field reflect.Value) bool {\n    if value == nil { return true }\n    vt := reflect.TypeOf(value)\n    return vt == field.Type() || vt.AssignableTo(field.Type()) || vt.ConvertibleTo(field.Type())\n}","tryCatchPattern":"if err := util.MapToStruct(in, &out); err != nil {\n    if strings.Contains(err.Error(), \"cannot set value of type\") {\n        return normalizeAndRetry(in, &out) // coerce types then retry once\n    }\n    return err\n}","preventionTips":["Normalize JSON-decoded data (float64 numbers, []any, map[string]any) to concrete types before mapping","Change struct field types in lockstep with data producers","Add custom UnmarshalJSON for time.Time or named types fed from generic JSON"],"tags":["reflection","type-mismatch","go"],"backgroundTag":"struct-field-type-mismatch","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}