{"record":{"id":"83f2d38def6b3616","repo":"gohugoio/hugo","slug":"unable-to-convert-value-of-type-q-to-q","errorCode":null,"errorMessage":"unable to convert value of type %q to %q","messagePattern":"unable to convert value of type %q to %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/hreflect/convert.go","lineNumber":88,"sourceCode":"\nfunc ToStringValueE(v reflect.Value) (reflect.Value, error) {\n\tif v, ok := ConvertIfPossible(v, typeString); ok {\n\t\treturn v, nil\n\t}\n\treturn reflect.Value{}, errConvert(v, \"string\")\n}\n\n// ToString converts v to string if possible, panicking if not.\nfunc ToString(v reflect.Value) string {\n\tvv, err := ToStringE(v)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn vv\n}\n\nfunc errConvert(v reflect.Value, s string) error {\n\treturn fmt.Errorf(\"unable to convert value of type %q to %q\", v.Type().String(), s)\n}\n\n// ConvertIfPossible tries to convert val to typ if possible.\n// This is currently only implemented for int kinds,\n// added to handle the move to a new YAML library which produces uint64 for unsigned integers.\n// We can expand on this later if needed.\n// This conversion is lossless.\n// See Issue 14079.\nfunc ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {\n\tswitch val.Kind() {\n\tcase reflect.Pointer, reflect.Interface:\n\t\tif val.IsNil() {\n\t\t\t// Return typ's zero value.\n\t\t\treturn reflect.Zero(typ), true\n\t\t}\n\t\tval = val.Elem()\n\t}\n","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/common/hreflect/convert.go#L70-L106","documentation":"Returned by hreflect's errConvert helper when ToInt64E/ToFloat64E/ToStringE cannot convert a reflect.Value to the requested target type. ConvertIfPossible only handles int/uint/float/string cross-conversions losslessly; any other kind (struct, slice, map, bool, complex, channel, func) yields this error naming both the source type and the target kind.","triggerScenarios":"Calling hreflect.ToInt64E/ToFloat64E/ToStringE with a reflect.Value whose Kind is bool, struct, slice, map, or a numeric that would overflow the target (e.g. uint64 > MaxInt64).","commonSituations":"Reflection-based template helpers trying to stringify or numerify a structured value from frontmatter, or converting a large unsigned integer produced by the YAML decoder (see Issue 14079) into an int64 that overflows.","solutions":["Pre-check the reflect.Kind before calling; handle non-convertible kinds explicitly.","For uint64 sources, guard against overflow (> MaxInt64) before converting to int64.","If you need a string of a struct, marshal to JSON instead of relying on ToStringE."],"exampleFix":"// before\ni, err := hreflect.ToInt64E(reflect.ValueOf(myStruct))\n// after\nif v.Kind() == reflect.Struct {\n  b,_ := json.Marshal(v.Interface())\n  // use string(b)\n} else {\n  i, err := hreflect.ToInt64E(v)\n}","handlingStrategy":"type-guard","validationCode":"func convertibleTo(rv reflect.Value, kind reflect.Kind) bool {\n  switch rv.Kind() {\n  case reflect.Bool, reflect.String, reflect.Struct, reflect.Map, reflect.Slice, reflect.Array, reflect.Chan, reflect.Func: return false\n  }\n  return true\n}","typeGuard":"func isNumericOrString(rv reflect.Value) bool {\n  k := rv.Kind()\n  return hreflect.IsInt(k) || hreflect.IsUint(k) || hreflect.IsFloat(k) || hreflect.IsString(k)\n}","tryCatchPattern":null,"preventionTips":["Marshal structs to JSON for string conversion instead of reflection.","Guard uint64 → int64 against overflow (> math.MaxInt64).","Check Kind before invoking ToInt64E/ToFloat64E/ToStringE."],"tags":["hreflect","reflection","convert","int64","type-mismatch"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}