Commit e7a58934 authored by Kirill Smelkov's avatar Kirill Smelkov

go/neo/proto: protogen: Reuse sizer for typeSizeFixed

Generating code through sizer can already give answer if a type is fixed
size or not. And in practice we don't care at all if it will be a bit
slower compared to previous explicit type switch.

For current code it is just a small cleanup, and it will be handy to
support messagepack encoding as well.

No change in generated code.
parent 5049f747
......@@ -424,34 +424,13 @@ var basicTypes = map[types.BasicKind]basicCodec{
// does a type have fixed wire size and, if yes, what it is?
func typeSizeFixed(typ types.Type) (wireSize int, ok bool) {
switch u := typ.Underlying().(type) {
case *types.Basic:
basic, ok := basicTypes[u.Kind()]
if ok {
return basic.wireSize, ok
}
case *types.Struct:
for i := 0; i < u.NumFields(); i++ {
size, ok := typeSizeFixed(u.Field(i).Type())
if !ok {
goto notfixed
}
wireSize += size
}
return wireSize, true
case *types.Array:
elemSize, ok := typeSizeFixed(u.Elem())
if ok {
return int(u.Len()) * elemSize, ok
}
// pass typ through sizer and see if encoded size is fixed or not
s := &sizer{}
codegenType("x", typ, nil, s)
if !s.size.IsNumeric() { // no symbolic part
return 0, false
}
notfixed:
// everything else is of not fixed wire size
return 0, false
return s.size.num, true
}
// interface of a codegenerator (for sizer/coder/decoder)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment