Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support uint64 pointers #13

Merged
merged 2 commits into from
Feb 23, 2020
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
52 changes: 46 additions & 6 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (f Field) ElemName() string {
return typeName(f.Pkg, f.Type.Elem())
}


type GenTypeInfo struct {
Name string
Fields []Field
Expand Down Expand Up @@ -212,13 +211,22 @@ func emitCborMarshalStructField(w io.Writer, f Field) error {
}

func emitCborMarshalUint64Field(w io.Writer, f Field) error {
if f.Pointer {
return fmt.Errorf("pointers to integers not supported")
}
return doTemplate(w, f, `
{{ if .Pointer }}
if {{ .Name }} == nil {
if _, err := w.Write(cbg.CborNull); err != nil {
return err
}
} else {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(*{{ .Name }}))); err != nil {
return err
}
}
{{ else }}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64({{ .Name }}))); err != nil {
return err
}
{{ end }}
`)
}

Expand Down Expand Up @@ -592,8 +600,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error {
}
}

func
emitCborUnmarshalInt64Field(w io.Writer, f Field) error {
func emitCborUnmarshalInt64Field(w io.Writer, f Field) error {
return doTemplate(w, f, `{
maj, extra, err := cbg.CborReadHeader(br)
var extraI int64
Expand Down Expand Up @@ -623,6 +630,29 @@ emitCborUnmarshalInt64Field(w io.Writer, f Field) error {

func emitCborUnmarshalUint64Field(w io.Writer, f Field) error {
return doTemplate(w, f, `
{
{{ if .Pointer }}
pb, err := br.PeekByte()
if err != nil {
return err
}
if pb == cbg.CborNull[0] {
var nbuf [1]byte
if _, err := br.Read(nbuf[:]); err != nil {
return err
}
} else {
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
typed := {{ .TypeName }}(extra)
{{ .Name }} = &typed
}
{{ else }}
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
Expand All @@ -631,6 +661,8 @@ func emitCborUnmarshalUint64Field(w io.Writer, f Field) error {
return fmt.Errorf("wrong type for uint64 field")
}
{{ .Name }} = {{ .TypeName }}(extra)
{{ end }}
}
`)
}

Expand Down Expand Up @@ -1010,6 +1042,10 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
if err := emitCborMarshalUint64Field(w, f); err != nil {
return err
}
case reflect.Int64:
if err := emitCborMarshalInt64Field(w, f); err != nil {
return err
}
case reflect.Uint8:
if err := emitCborMarshalUint8Field(w, f); err != nil {
return err
Expand Down Expand Up @@ -1097,6 +1133,10 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) error {
if err := emitCborUnmarshalUint64Field(w, f); err != nil {
return err
}
case reflect.Int64:
if err := emitCborUnmarshalInt64Field(w, f); err != nil {
return err
}
case reflect.Uint8:
if err := emitCborUnmarshalUint8Field(w, f); err != nil {
return err
Expand Down
100 changes: 91 additions & 9 deletions testing/cbor_gen.go

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

100 changes: 99 additions & 1 deletion testing/cbor_map_gen.go

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

14 changes: 9 additions & 5 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ type SimpleTypeTwo struct {
Test [][]byte
Dog string
Numbers []NaturalNumber
Pizza *uint64
PointyPizza *NaturalNumber
}

type SimpleTypeTree struct {
Stuff *SimpleTypeTree
Stufff *SimpleTypeTwo
Others []uint64
Test [][]byte
Dog string
Stuff *SimpleTypeTree
Stufff *SimpleTypeTwo
Others []uint64
Test [][]byte
Dog string
SixtyThreeBitIntegerWithASignBit int64
NotPizza *uint64
}