diff --git a/types/tstring/builder.go b/types/tstring/builder.go index 86fa900..9cbab83 100644 --- a/types/tstring/builder.go +++ b/types/tstring/builder.go @@ -76,6 +76,7 @@ func (b Builder) MarshalCodeTemplate(_ *uint64) string { // UnmarshalCodeTemplate returns code template unmarshaling the data. func (b Builder) UnmarshalCodeTemplate(_ *uint64) string { + unsafe := b.tm.Import("unsafe") code := `{ var l uint64 ` @@ -87,9 +88,9 @@ func (b Builder) UnmarshalCodeTemplate(_ *uint64) string { code += ` if l > 0 { ` if b.fieldType.Name() == "string" { - code += " {{ . }} = string(b[o:o+l])\n" + code += fmt.Sprintf(" {{ . }} = %[1]s.String((*byte)(%[1]s.Pointer(&b[o])), l)\n", unsafe) } else { - code += fmt.Sprintf(" {{ . }} = %[1]s(b[o:o+l])\n", + code += fmt.Sprintf(" {{ . }} = %[2]s(%[1]s.String((*byte)(%[1]s.Pointer(&b[o])), l))\n", unsafe, b.tm.TypeName(b.msgType, b.fieldType)) } code += ` o += l