Skip to content

Commit

Permalink
fix bug in consts code
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Dec 20, 2022
1 parent 76063ba commit 0333c14
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion testing/cbor_map_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions testing/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"reflect"
Expand Down Expand Up @@ -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

0 comments on commit 0333c14

Please sign in to comment.