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

turn max length consts into global vars #92

Merged
merged 1 commit into from
Feb 1, 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
16 changes: 8 additions & 8 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
cid "github.com/ipfs/go-cid"
)

const MaxLength = 8192
var MaxLength = uint64(8192)

const ByteArrayMaxLen = 2 << 20
var ByteArrayMaxLen = uint64(2 << 20)

const MaxLenTag = "maxlen"
const NoUsrMaxLen = -1
Expand Down Expand Up @@ -318,7 +318,7 @@ func emitCborMarshalStringField(w io.Writer, f Field) error {
return err
}
} else {
if len(*{{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
if uint64(len(*{{ .Name }})) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
return xerrors.Errorf("Value in field {{ .Name | js }} was too long")
}

Expand All @@ -341,7 +341,7 @@ func emitCborMarshalStringField(w io.Writer, f Field) error {
}

return doTemplate(w, f, `
if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
if uint64(len({{ .Name }})) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
return xerrors.Errorf("Value in field {{ .Name | js }} was too long")
}

Expand Down Expand Up @@ -532,7 +532,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error {

if e.Kind() == reflect.Uint8 {
return doTemplate(w, f, `
if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} {
if uint64(len({{ .Name }})) > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} {
return xerrors.Errorf("Byte array in field {{ .Name }} was too long")
}

Expand Down Expand Up @@ -562,7 +562,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error {
}

err := doTemplate(w, f, `
if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
if uint64(len({{ .Name }})) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
return xerrors.Errorf("Slice value in field {{ .Name }} was too long")
}

Expand Down Expand Up @@ -623,7 +623,7 @@ func emitCborMarshalArrayField(w io.Writer, f Field) error {
// Note: this re-slices the slice to deal with arrays.
if e.Kind() == reflect.Uint8 {
return doTemplate(w, f, `
if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} {
if uint64(len({{ .Name }})) > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} {
return xerrors.Errorf("Byte array in field {{ .Name }} was too long")
}

Expand All @@ -642,7 +642,7 @@ func emitCborMarshalArrayField(w io.Writer, f Field) error {
}

err := doTemplate(w, f, `
if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
if uint64(len({{ .Name }})) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
return xerrors.Errorf("Slice value in field {{ .Name }} was too long")
}

Expand Down
36 changes: 18 additions & 18 deletions testing/cbor_gen.go

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

Loading