diff --git a/gen.go b/gen.go index f7bbc9b..9e1d0aa 100644 --- a/gen.go +++ b/gen.go @@ -42,6 +42,9 @@ func doTemplate(w io.Writer, info interface{}, templ string) error { } return fmt.Sprintf("%d", val) }, + "Deref": func(sp *string) string { + return *sp + }, }).Parse(templ)) return t.Execute(w, info) @@ -309,7 +312,7 @@ func emitCborMarshalStringField(w io.Writer, f Field) error { if f.Const != nil { return doTemplate(w, f, ` - {{ MajorType "cw" "cbg.MajTextString" (print "len(" .Name ")") }} + {{ MajorType "cw" "cbg.MajTextString" (print "len(\"" (Deref .Const) "\")") }} if _, err := io.WriteString(w, string("{{ .Const }}")); err != nil { return err } diff --git a/testing/cbor_map_gen.go b/testing/cbor_map_gen.go index a995808..987c358 100644 --- a/testing/cbor_map_gen.go +++ b/testing/cbor_map_gen.go @@ -1999,7 +1999,7 @@ func (t *TestConstField) MarshalCBOR(w io.Writer) error { return err } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Cats))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("dogsdrool"))); err != nil { return err } if _, err := io.WriteString(w, string("dogsdrool")); err != nil { diff --git a/testing/roundtrip_test.go b/testing/roundtrip_test.go index c268b61..c546cba 100644 --- a/testing/roundtrip_test.go +++ b/testing/roundtrip_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io" "math/rand" "reflect" @@ -388,4 +389,24 @@ func TestOmitEmpty(t *testing.T) { testValueRoundtrip(t, &et, &recepticle) } +func TestConstRoundtrip(t *testing.T) { + tcf := &TestConstField{ + Thing: 16223, + } + + buf := new(bytes.Buffer) + if err := tcf.MarshalCBOR(buf); err != nil { + t.Fatal(err) + } + + fmt.Printf("%x\n", buf.Bytes()) + + var out TestConstField + if err := out.UnmarshalCBOR(buf); err != nil { + t.Fatal(err) + } + + fmt.Println(out) +} + //TODO same for strings