{"record":{"id":"6447748b7a82d47a","repo":"OpenNHP/opennhp","slug":"unsupported-field-type-v-in-slice","errorCode":null,"errorMessage":"unsupported field type: %v in slice","messagePattern":"unsupported field type: (.+?) in slice","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/ztdo/ztdo.go","lineNumber":515,"sourceCode":"\t\t\tif err := marshal(buf, field.Interface()); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t} else {\n\t\t\tvar bytes []byte\n\t\t\tif field.Type().Kind() == reflect.Array { // here assume it's an array of byte.\n\t\t\t\tbytes = make([]byte, field.Len())\n\t\t\t\treflect.Copy(reflect.ValueOf(bytes), field)\n\t\t\t} else if field.Type().Kind() == reflect.Slice {\n\t\t\t\tif field.Type().Elem().Kind() == reflect.Struct {\n\t\t\t\t\tfor j := range field.Len() {\n\t\t\t\t\t\tif err := marshal(buf, field.Index(j).Interface()); err != nil {\n\t\t\t\t\t\t\treturn err\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 {\n\t\t\t\t\tbytes = field.Interface().([]byte)\n\t\t\t\t} else {\n\t\t\t\t\treturn fmt.Errorf(\"unsupported field 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\n\t\t\tif _, err := buf.Write(bytes); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// unmarshal recursively deserializes binary data from a file into a Go struct\nfunc unmarshal(f *os.File, data any) error {\n\trValues := reflect.ValueOf(data)\n\trTypes := reflect.TypeOf(data)","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/ztdo/ztdo.go#L497-L533","documentation":"Inside marshal, a slice-typed field is only supported if its element kind is Uint8 (encoded as raw bytes) or, via the preceding branch, an array/struct of supported element types. Any other slice element kind (e.g. []int32, []string, [][]byte) has no defined wire encoding in this format, so the serializer rejects it with this error.","triggerScenarios":"Marshaling a struct containing a slice field whose element type is not uint8 and not handled by the struct/array branch — e.g. []int, []string, []float64, or a slice of structs containing unsupported nested types.","commonSituations":"Adding a convenience field like Tags []string or Scores []float64 to a ztdo header/metadata struct; refactoring a fixed-size array field into a slice; reusing a JSON-oriented struct directly for ztdo serialization.","solutions":["Change the field to []byte and encode the data yourself (e.g. with encoding/binary or json.Marshal before storing).","Replace []T (struct T) with a struct containing fixed-size fields, or encode each element into a byte slice.","Keep slices out of ztdo structs; store variable-length data in the metadata section instead.","Pre-encode the collection into a single []byte field before calling marshal/toBuffer."],"exampleFix":"// before\ntype H struct {\n    Tags []string // unsupported\n}\n// after\ntype H struct {\n    TagsBytes []byte // json.Marshal(tags) beforehand\n}","handlingStrategy":"type-guard","validationCode":"func sliceOK(t reflect.Type) bool {\n    if t.Kind() != reflect.Slice { return true }\n    return t.Elem().Kind() == reflect.Uint8 || t.Elem().Kind() == reflect.Struct\n}","typeGuard":"// fail fast at init\nt := reflect.TypeOf(MyHeader{})\nfor i := 0; i < t.NumField(); i++ {\n    if ft := t.Field(i).Type; ft.Kind() == reflect.Slice && ft.Elem().Kind() != reflect.Uint8 {\n        panic(\"ztdo field \" + t.Field(i).Name + \" must be []byte\")\n    }\n}","tryCatchPattern":"if err := marshal(buf, s); err != nil {\n    if strings.Contains(err.Error(), \"unsupported field type\") {\n        return fmt.Errorf(\"field in %T has no ztdo encoding: pre-encode to []byte\", s)\n    }\n    return err\n}","preventionTips":["Use []byte for any variable-length data in ztdo structs","Pre-encode collections (json/binary) into a byte field","Review struct fields against marshal's supported kinds when adding fields"],"tags":["reflection","serialization","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"}