{"record":{"id":"212d572a541de599","repo":"OpenNHP/opennhp","slug":"unsupported-element-type-v-in-slice","errorCode":null,"errorMessage":"unsupported element type: %v in slice","messagePattern":"unsupported element type: (.+?) in slice","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/ztdo/ztdo.go","lineNumber":605,"sourceCode":"\t\t\t\t\t\tvalue.Set(newValue)\n\t\t\t\t\t\terr := unmarshal(f, value.Index(value.Len()-1).Addr().Interface())\n\t\t\t\t\t\tif err != nil {\n\t\t\t\t\t\t\treturn err\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif !lengthContinueIndicator { // more data to be read to construct element in current slice\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if field.Type.Elem().Kind() == reflect.Uint8 { // if the elment in slice is a byte, there MUST be lengthFor tag to be parsed before.\n\t\t\t\t\tlength := lengthMap[field.Name]\n\t\t\t\t\tbytes := make([]byte, length)\n\t\t\t\t\t_, err := f.Read(bytes)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t\tsetBytes(value, bytes)\n\t\t\t\t} else {\n\t\t\t\t\treturn fmt.Errorf(\"unsupported element type: %v in slice\", field.Type.Elem().Kind())\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn fmt.Errorf(\"unsupported field type: %v\", field.Type.Kind())\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// toBuffer provides unified way to serialize a Go struct into bytes buffer\nfunc toBuffer(data any) *bytes.Buffer {\n\tbuf := bytes.NewBuffer(nil)\n\t_ = marshal(buf, data)\n\treturn buf\n}\n\n// toStructure provides unified way to deserialize bytes from a file into a Go struct","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/ztdo/ztdo.go#L587-L623","documentation":"When unmarshal encounters a slice-typed field, it only knows how to read elements for supported element kinds (uint8 byte-slices via the f.Read branch, or fixed-size element types handled above). Any other slice element kind cannot be decoded from the stream, so it returns this error naming the element kind.","triggerScenarios":"Deserializing into a struct with a slice field whose element type is not uint8 and not one of the fixed-size supported element kinds — e.g. []string, []float32, []int64 read back from a ztdo file.","commonSituations":"Round-tripping a struct whose write-side error was 'fixed' with json-in-bytes but keeping the original typed field; schema drift where a newer writer emits a slice type this reader does not support; structs shared between ztdo and JSON APIs.","solutions":["Change the destination field to []byte and decode manually after unmarshal.","Use a fixed-size array (e.g. [N]byte) if the length is known.","Bump/align the struct definition with what the writer actually serialized.","Decode the byte blob into typed values (encoding/binary, json.Unmarshal) post-read."],"exampleFix":"// before\nvar h struct{ Tags []string }\nerr := toStructure(f, &h) // unsupported element type\n// after\nvar h struct{ TagsBytes []byte }\nerr := toStructure(f, &h)\nvar tags []string\njson.Unmarshal(h.TagsBytes, &tags)","handlingStrategy":"type-guard","validationCode":"t := reflect.TypeOf(dst).Elem()\nfor i := 0; i < t.NumField(); i++ {\n    ft := t.Field(i).Type\n    if ft.Kind() == reflect.Slice && ft.Elem().Kind() != reflect.Uint8 {\n        panic(\"change \" + t.Field(i).Name + \" to []byte and decode after unmarshal\")\n    }\n}","typeGuard":"ft := reflect.TypeOf(dst).Elem().Field(i).Type\nif ft.Kind() == reflect.Slice && ft.Elem().Kind() != reflect.Uint8 { /* unsupported for ztdo */ }","tryCatchPattern":"if err := toStructure(f, &h); err != nil {\n    if strings.Contains(err.Error(), \"unsupported element type\") {\n        return fmt.Errorf(\"slice field in %T needs a []byte representation\", h)\n    }\n    return err\n}","preventionTips":["Round-trip test every struct through marshal/unmarshal in CI","Keep reader and writer struct definitions in one shared package","Convert typed slices to []byte before writing and back after reading"],"tags":["reflection","deserialization","go","unsupported-type"],"backgroundTag":"unsupported-operation","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}