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

Nullable ints #93

Merged
merged 4 commits into from
Feb 7, 2024
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
69 changes: 45 additions & 24 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,20 +510,28 @@ func (g Gen) emitCborMarshalUint8Field(w io.Writer, f Field) error {
}

func (g Gen) emitCborMarshalInt64Field(w io.Writer, f Field) error {
if f.Pointer {
return fmt.Errorf("pointers to integers not supported")
}

// if negative
// val = -1 - cbor
// cbor = -val -1

return g.doTemplate(w, f, `
return g.doTemplate(w, f, `{{ if .Pointer }}
if {{ .Name }} == nil {
if _, err := cw.Write(cbg.CborNull); err != nil {
return err
}
} else {
if {{ (print "*" .Name) }} >= 0 {
{{ MajorType "cw" "cbg.MajUnsignedInt" (print "*" .Name) }}
} else {
{{ MajorType "cw" "cbg.MajNegativeInt" (print "-*" .Name "-1") }}
}
}
{{ else }}
if {{ .Name }} >= 0 {
{{ MajorType "cw" "cbg.MajUnsignedInt" .Name }}
} else {
{{ MajorType "cw" "cbg.MajNegativeInt" (print "-" .Name "-1") }}
}
{{ end }}
`)
}

Expand Down Expand Up @@ -982,29 +990,42 @@ func (g Gen) emitCborUnmarshalStructField(w io.Writer, f Field) error {

func (g Gen) emitCborUnmarshalInt64Field(w io.Writer, f Field) error {
return g.doTemplate(w, f, `{
maj, extra, err := {{ ReadHeader "cr" }}
var extraI int64
{{ if .Pointer }}
b, err := cr.ReadByte()
if err != nil {
return err
}
switch maj {
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative overflow")
}
extraI = -1 - extraI
default:
return fmt.Errorf("wrong type for int64 field: %d", maj)
}
if b != cbg.CborNull[0] {
if err := cr.UnreadByte(); err != nil {
return err
}
{{ end }}maj, extra, err := {{ ReadHeader "cr" }}
if err != nil {
return err
}
var extraI int64
switch maj {
rvagg marked this conversation as resolved.
Show resolved Hide resolved
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative overflow")
}
extraI = -1 - extraI
default:
return fmt.Errorf("wrong type for int64 field: %d", maj)
}

{{ .Name }} = {{ .TypeName }}(extraI)
{{ if .Pointer }}
{{ .Name }} = (*{{ .TypeName }})(&extraI)
}
{{ else }}
{{ .Name }} = {{ .TypeName }}(extraI)
{{ end }}}
`)
}

Expand Down
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
module github.com/whyrusleeping/cbor-gen

go 1.12
go 1.18
rvagg marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/google/go-cmp v0.4.0
github.com/ipfs/go-cid v0.0.6
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
)

require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 // indirect
github.com/mr-tron/base58 v1.1.3 // indirect
github.com/multiformats/go-base32 v0.0.3 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multibase v0.0.3 // indirect
github.com/multiformats/go-multihash v0.0.13 // indirect
github.com/multiformats/go-varint v0.0.5 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 // indirect
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
)
1 change: 1 addition & 0 deletions testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
types.IntArray{},
types.IntAliasArray{},
types.TupleIntArray{},
types.TupleIntArrayOptionals{},
types.IntArrayNewType{},
types.IntArrayAliasNewType{},
types.MapTransparentType{},
Expand Down
Loading