{"record":{"id":"860d1faa788a4401","repo":"semaphoreui/semaphore","slug":"cannot-assign-value-of-type-s-to-map-element-of-t","errorCode":null,"errorMessage":"cannot assign value of type %s to map element of type %s","messagePattern":"cannot assign value of type (.+?) to map element of type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util/config.go","lineNumber":1278,"sourceCode":"\t\t\t\t\t\tsrcVal := fieldValue.MapIndex(key)\n\t\t\t\t\t\tvar mapElem reflect.Value\n\t\t\t\t\t\tif srcVal.IsValid() {\n\t\t\t\t\t\t\tmapElem = cloneStruct(srcVal)\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tmapElem = reflect.New(mapElemType).Elem()\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif mapElemType.Kind() == reflect.Struct {\n\t\t\t\t\t\t\tif err := assignMapToStructRecursive(mapElemValue.Interface().(map[string]any), mapElem); err != nil {\n\t\t\t\t\t\t\t\treturn err\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tif mapElemValue.Type().ConvertibleTo(mapElemType) {\n\t\t\t\t\t\t\t\tmapElem.Set(mapElemValue.Convert(mapElemType))\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnewVal, converted := CastValueToKind(mapElemValue.Interface(), mapElemType.Kind())\n\t\t\t\t\t\t\t\tif !converted {\n\t\t\t\t\t\t\t\t\treturn fmt.Errorf(\"cannot assign value of type %s to map element of type %s\",\n\t\t\t\t\t\t\t\t\t\tmapElemValue.Type(), mapElemType)\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tmapElem.Set(reflect.ValueOf(newVal))\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tfieldValue.SetMapIndex(key, mapElem)\n\t\t\t\t\t}\n\n\t\t\t\tdefault:\n\t\t\t\t\t// Handle simple types\n\t\t\t\t\tif val.Type().ConvertibleTo(fieldValue.Type()) {\n\t\t\t\t\t\tfieldValue.Set(val.Convert(fieldValue.Type()))\n\t\t\t\t\t} else {\n\n\t\t\t\t\t\tnewVal, converted := CastValueToKind(val.Interface(), fieldValue.Type().Kind())\n\t\t\t\t\t\tif !converted {","sourceCodeStart":1260,"sourceCodeEnd":1296,"githubUrl":"https://github.com/semaphoreui/semaphore/blob/1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa/util/config.go#L1260-L1296","documentation":"When populating a map field with non-struct element type, each value must be reflect-convertible to the map's element type or castable by CastValueToKind (which supports string, int, bool kinds). If neither works — e.g. a string value into a map[string]float64, or any value into a map with an unsupported element kind — this error is returned.","triggerScenarios":"Assigning map[string]any{\"k\": \"1.5\"} to a map[string]float64 field, or values with unparseable text (\"abc\") into map[string]int/map[string]bool fields.","commonSituations":"Numeric/float settings sourced from env vars as strings; typos in numeric config values; map fields with less-common element types (float, uint, custom kinds) that CastValueToKind does not handle.","solutions":["Provide values already typed as the map's element type (e.g. 1.5 not \"1.5\" for map[string]float64).","If values are strings, ensure they parse cleanly for the target kind and consider pre-casting yourself.","Change the map field's element type to string, int, or bool (supported by CastValueToKind).","Convert the whole source map to the typed map before calling AssignMapToStruct."],"exampleFix":"// before\nm := map[string]any{\"limits\": map[string]any{\"cpu\": \"1.5\"}} // field is map[string]float64\n// after\nm := map[string]any{\"limits\": map[string]any{\"cpu\": 1.5}}","handlingStrategy":"validation","validationCode":"for k, v := range srcMap {\n  switch v.(type) {\n  case string, int, bool, float64:\n  default:\n    return fmt.Errorf(\"map value %q has unsupported type %T\", k, v)\n  }\n}","typeGuard":"func castableToKind(v any, k reflect.Kind) bool {\n  switch k {\n  case reflect.String, reflect.Int, reflect.Bool:\n    return true\n  }\n  return reflect.TypeOf(v).ConvertibleTo(reflect.SliceOf(reflect.InterfaceOf(nil)).Type()) // else pre-check manually\n}","tryCatchPattern":"if err := util.AssignMapToStruct(m, &cfg); err != nil {\n  if strings.Contains(err.Error(), \"cannot assign value of type\") {\n    log.Fatalf(\"map element type unsupported: %v\", err)\n  }\n}","preventionTips":["Match value types to the map's declared element type in source maps.","Restrict map element types to string, int, bool where possible.","Cast numeric strings to numbers at the config ingestion point."],"tags":["reflection","config","go","map","type-cast"],"backgroundTag":"config-type-mismatch","analyzedSha":"1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa","analyzedAt":"2026-09-07T11:00:33.293Z","contentChangedAt":"2026-09-07T11:00:33.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}