Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions _generated/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,26 @@ type ConvertErrVal string
type ConvertErr struct {
Err ConvertErrVal
}

//msgp:shim ConvertIntVal as:int64 using:fromConvertIntVal/toConvertIntVal mode:convert
//msgp:ignore ConvertIntVal

func fromConvertIntVal(v ConvertIntVal) (int64, error) {
return int64(v), nil
}

func toConvertIntVal(i int64) (ConvertIntVal, error) {
return ConvertIntVal(i), nil
}

type ConvertIntVal int64

// ConvertInt exercises a fixed-size (int64) convert shim.
type ConvertInt struct {
Int ConvertIntVal
Ptr *ConvertIntVal
Map map[string]ConvertIntVal
MapP map[string]*ConvertIntVal
Arr []ConvertIntVal
ArrP []*ConvertIntVal
}
28 changes: 28 additions & 0 deletions _generated/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package _generated

import (
"bytes"
"reflect"
"testing"

"github.com/tinylib/msgp/msgp"
Expand Down Expand Up @@ -58,3 +59,30 @@ func TestConvertToMarshalError(t *testing.T) {
t.Fatalf("expected conversion error, found %v", err.Error())
}
}

func TestConvertInt(t *testing.T) {
// A fixed-size convert shim must report an accurate constant Msgsize.
v := ConvertIntVal(7)
in := ConvertInt{
Int: 42,
Ptr: &v,
Map: map[string]ConvertIntVal{"a": 1},
MapP: map[string]*ConvertIntVal{"b": &v},
Arr: []ConvertIntVal{1, 2},
ArrP: []*ConvertIntVal{&v},
}
b, err := in.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
if in.Msgsize() < len(b) {
t.Fatalf("Msgsize %d under-reports marshaled size %d", in.Msgsize(), len(b))
}
var out ConvertInt
if _, err = out.UnmarshalMsg(b); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(out, in) {
t.Fatalf("round-trip mismatch: %v != %v", out, in)
}
}
7 changes: 6 additions & 1 deletion gen/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ func (s *sizeGen) gBase(b *BaseElem) {
return
}
if b.Convert && b.ShimMode == Convert {
if fixedSize(b.Value) {
// A fixed-size base has a constant wire size, so there is no need
// for a temporary holding the converted value.
s.addConstant(basesizeExpr(b.Value, "", b.BaseName()))
return
}
s.state = add
vname := randIdent()
s.p.printf("\nvar %s %s", vname, b.BaseType())
Expand All @@ -237,7 +243,6 @@ func (s *sizeGen) gBase(b *BaseElem) {

s.p.printf("\ns += %s", basesizeExpr(b.Value, vname, b.BaseName()))
s.state = expr

} else {
vname := b.Varname()
if b.Convert {
Expand Down
Loading