diff --git a/gen.go b/gen.go index d97f091..07667ad 100644 --- a/gen.go +++ b/gen.go @@ -26,7 +26,28 @@ var ( deferredType = reflect.TypeOf(Deferred{}) ) -func doTemplate(w io.Writer, info interface{}, templ string) error { +// Gen is a configurable code generator for CBOR types. Use this instead of the convenience +// functions to have more control over the generated code. +type Gen struct { + MaxArrayLength int // Default: 8192 (MaxLength) + MaxByteLength int // Default: 2<<20 (ByteArrayMaxLen) +} + +func (g Gen) maxByteLength() int { + if g.MaxByteLength == 0 { + return MaxLength + } + return g.MaxByteLength +} + +func (g Gen) maxArrayLength() int { + if g.MaxArrayLength == 0 { + return MaxLength + } + return g.MaxArrayLength +} + +func (g Gen) doTemplate(w io.Writer, info interface{}, templ string) error { t := template.Must(template.New(""). Funcs(template.FuncMap{ "MajorType": func(wname string, tname string, val string) string { @@ -46,12 +67,28 @@ func doTemplate(w io.Writer, info interface{}, templ string) error { "Deref": func(sp *string) string { return *sp }, + "Cfg": func(prop string) string { + switch prop { + case "MaxByteLength": + return fmt.Sprintf("%d", g.maxByteLength()) + case "MaxArrayLength": + return fmt.Sprintf("%d", g.maxArrayLength()) + default: + panic(fmt.Sprintf("error: unknown property [%s]", prop)) + } + }, }).Parse(templ)) return t.Execute(w, info) } +// PrintHeaderAndUtilityMethods is a convenience wrapper around Gen.PrintHeaderAndUtilityMethods +// using default options. func PrintHeaderAndUtilityMethods(w io.Writer, pkg string, typeInfos []*GenTypeInfo) error { + return Gen{}.PrintHeaderAndUtilityMethods(w, pkg, typeInfos) +} + +func (g Gen) PrintHeaderAndUtilityMethods(w io.Writer, pkg string, typeInfos []*GenTypeInfo) error { var imports []Import for _, gti := range typeInfos { imports = append(imports, gti.Imports()...) @@ -64,7 +101,7 @@ func PrintHeaderAndUtilityMethods(w io.Writer, pkg string, typeInfos []*GenTypeI Package string Imports []Import }{pkg, imports} - return doTemplate(w, data, `// Code generated by github.com/whyrusleeping/cbor-gen. DO NOT EDIT. + return g.doTemplate(w, data, `// Code generated by github.com/whyrusleeping/cbor-gen. DO NOT EDIT. package {{ .Package }} @@ -349,15 +386,15 @@ func (gti GenTypeInfo) MapHeaderAsByteString() string { return s } -func emitCborMarshalStringField(w io.Writer, f Field) error { +func (g Gen) emitCborMarshalStringField(w io.Writer, f Field) error { if f.Pointer { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` if {{ .Name }} == nil { if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if len(*{{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} { + if len(*{{ .Name }}) > {{ Cfg "MaxArrayLength" | MaxLen .MaxLen }} { return xerrors.Errorf("Value in field {{ .Name | js }} was too long") } @@ -370,7 +407,7 @@ func emitCborMarshalStringField(w io.Writer, f Field) error { } if f.Const != nil { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` {{ MajorType "cw" "cbg.MajTextString" (print "len(\"" (Deref .Const) "\")") }} if _, err := cw.WriteString(string("{{ .Const }}")); err != nil { return err @@ -379,8 +416,8 @@ func emitCborMarshalStringField(w io.Writer, f Field) error { } - return doTemplate(w, f, ` - if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} { + return g.doTemplate(w, f, ` + if len({{ .Name }}) > {{ Cfg "MaxArrayLength" | MaxLen .MaxLen }} { return xerrors.Errorf("Value in field {{ .Name | js }} was too long") } @@ -391,10 +428,10 @@ func emitCborMarshalStringField(w io.Writer, f Field) error { `) } -func emitCborMarshalStructField(w io.Writer, f Field) error { +func (g Gen) emitCborMarshalStructField(w io.Writer, f Field) error { switch f.Type { case bigIntType: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { if err := cw.CborWriteHeader(cbg.MajTag, 2); err != nil { return err @@ -414,7 +451,7 @@ func emitCborMarshalStructField(w io.Writer, f Field) error { `) case cidType: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` {{ if .Pointer }} if {{ .Name }} == nil { if _, err := cw.Write(cbg.CborNull); err != nil { @@ -432,7 +469,7 @@ func emitCborMarshalStructField(w io.Writer, f Field) error { {{ end }} `) default: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` if err := {{ .Name }}.MarshalCBOR(cw); err != nil { return err } @@ -440,8 +477,8 @@ func emitCborMarshalStructField(w io.Writer, f Field) error { } } -func emitCborMarshalUint64Field(w io.Writer, f Field) error { - return doTemplate(w, f, ` +func (g Gen) emitCborMarshalUint64Field(w io.Writer, f Field) error { + return g.doTemplate(w, f, ` {{ if .Pointer }} if {{ .Name }} == nil { if _, err := cw.Write(cbg.CborNull); err != nil { @@ -456,16 +493,16 @@ func emitCborMarshalUint64Field(w io.Writer, f Field) error { `) } -func emitCborMarshalUint8Field(w io.Writer, f Field) error { +func (g Gen) emitCborMarshalUint8Field(w io.Writer, f Field) error { if f.Pointer { return fmt.Errorf("pointers to integers not supported") } - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` {{ MajorType "cw" "cbg.MajUnsignedInt" .Name }} `) } -func emitCborMarshalInt64Field(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") } @@ -474,7 +511,7 @@ func emitCborMarshalInt64Field(w io.Writer, f Field) error { // val = -1 - cbor // cbor = -val -1 - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` if {{ .Name }} >= 0 { {{ MajorType "cw" "cbg.MajUnsignedInt" .Name }} } else { @@ -483,9 +520,9 @@ func emitCborMarshalInt64Field(w io.Writer, f Field) error { `) } -func emitCborMarshalBoolField(w io.Writer, f Field) error { +func (g Gen) emitCborMarshalBoolField(w io.Writer, f Field) error { if f.Pointer { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` if {{ .Name }} == nil { if _, err := cw.Write(cbg.CborNull); err != nil { return err @@ -497,7 +534,7 @@ func emitCborMarshalBoolField(w io.Writer, f Field) error { } `) } else { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` if err := cbg.WriteBool(w, {{ .Name }}); err != nil { return err } @@ -505,8 +542,8 @@ func emitCborMarshalBoolField(w io.Writer, f Field) error { } } -func emitCborMarshalMapField(w io.Writer, f Field) error { - err := doTemplate(w, f, ` +func (g Gen) emitCborMarshalMapField(w io.Writer, f Field) error { + err := g.doTemplate(w, f, ` { if len({{ .Name }}) > 4096 { return xerrors.Errorf("cannot marshal {{ .Name }} map too large") @@ -530,7 +567,7 @@ func emitCborMarshalMapField(w io.Writer, f Field) error { // Map key switch f.Type.Key().Kind() { case reflect.String: - if err := emitCborMarshalStringField(w, Field{Name: "k"}); err != nil { + if err := g.emitCborMarshalStringField(w, Field{Name: "k"}); err != nil { return err } default: @@ -540,7 +577,7 @@ func emitCborMarshalMapField(w io.Writer, f Field) error { // Map value switch f.Type.Elem().Kind() { case reflect.String: - if err := emitCborMarshalStringField(w, Field{Name: "v"}); err != nil { + if err := g.emitCborMarshalStringField(w, Field{Name: "v"}); err != nil { return err } case reflect.Ptr: @@ -550,28 +587,28 @@ func emitCborMarshalMapField(w io.Writer, f Field) error { fallthrough case reflect.Struct: - if err := emitCborMarshalStructField(w, Field{Name: "v", Type: f.Type.Elem(), Pkg: f.Pkg}); err != nil { + if err := g.emitCborMarshalStructField(w, Field{Name: "v", Type: f.Type.Elem(), Pkg: f.Pkg}); err != nil { return err } default: return fmt.Errorf("currently unsupported map elem type: %s", f.Type.Elem()) } - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` } } `) } -func emitCborMarshalSliceField(w io.Writer, f Field) error { +func (g Gen) emitCborMarshalSliceField(w io.Writer, f Field) error { if f.Pointer { return fmt.Errorf("pointers to slices not supported") } e := f.Type.Elem() if e.Kind() == reflect.Uint8 { - return doTemplate(w, f, ` - if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} { + return g.doTemplate(w, f, ` + if len({{ .Name }}) > {{ Cfg "MaxByteLength" | MaxLen .MaxLen }} { return xerrors.Errorf("Byte array in field {{ .Name }} was too long") } @@ -600,8 +637,8 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { pointer = true } - err := doTemplate(w, f, ` - if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} { + err := g.doTemplate(w, f, ` + if len({{ .Name }}) > {{ Cfg "MaxArrayLength" | MaxLen .MaxLen }} { return xerrors.Errorf("Slice value in field {{ .Name }} was too long") } @@ -624,24 +661,24 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { default: err = fmt.Errorf("do not yet support slices of %s yet", e.Kind()) case reflect.Struct: - err = emitCborMarshalStructField(w, subf) + err = g.emitCborMarshalStructField(w, subf) case reflect.Uint64: - err = emitCborMarshalUint64Field(w, subf) + err = g.emitCborMarshalUint64Field(w, subf) case reflect.Uint8: - err = emitCborMarshalUint8Field(w, subf) + err = g.emitCborMarshalUint8Field(w, subf) case reflect.Int64: - err = emitCborMarshalInt64Field(w, subf) + err = g.emitCborMarshalInt64Field(w, subf) case reflect.Slice: - err = emitCborMarshalSliceField(w, subf) + err = g.emitCborMarshalSliceField(w, subf) case reflect.String: - err = emitCborMarshalStringField(w, subf) + err = g.emitCborMarshalStringField(w, subf) } if err != nil { return err } // array end - if err := doTemplate(w, f, ` + if err := g.doTemplate(w, f, ` {{ if .PreserveNil }} } {{ end }} @@ -653,7 +690,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { return nil } -func emitCborMarshalArrayField(w io.Writer, f Field) error { +func (g Gen) emitCborMarshalArrayField(w io.Writer, f Field) error { if f.Pointer { return fmt.Errorf("pointers to arrays not supported") } @@ -661,8 +698,8 @@ 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" }} { + return g.doTemplate(w, f, ` + if len({{ .Name }}) > {{ Cfg "MaxByteLength" | MaxLen .MaxLen }} { return xerrors.Errorf("Byte array in field {{ .Name }} was too long") } @@ -680,8 +717,8 @@ func emitCborMarshalArrayField(w io.Writer, f Field) error { pointer = true } - err := doTemplate(w, f, ` - if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} { + err := g.doTemplate(w, f, ` + if len({{ .Name }}) > {{ Cfg "MaxArrayLength" | MaxLen .MaxLen }} { return xerrors.Errorf("Slice value in field {{ .Name }} was too long") } @@ -696,17 +733,17 @@ func emitCborMarshalArrayField(w io.Writer, f Field) error { default: err = fmt.Errorf("do not yet support arrays of %s yet", e.Kind()) case reflect.Struct: - err = emitCborMarshalStructField(w, subf) + err = g.emitCborMarshalStructField(w, subf) case reflect.Uint64: - err = emitCborMarshalUint64Field(w, subf) + err = g.emitCborMarshalUint64Field(w, subf) case reflect.Uint8: - err = emitCborMarshalUint8Field(w, subf) + err = g.emitCborMarshalUint8Field(w, subf) case reflect.Int64: - err = emitCborMarshalInt64Field(w, subf) + err = g.emitCborMarshalInt64Field(w, subf) case reflect.Slice: - err = emitCborMarshalSliceField(w, subf) + err = g.emitCborMarshalSliceField(w, subf) case reflect.String: - err = emitCborMarshalStringField(w, subf) + err = g.emitCborMarshalStringField(w, subf) } if err != nil { return err @@ -717,15 +754,15 @@ func emitCborMarshalArrayField(w io.Writer, f Field) error { return nil } -func emitCborMarshalStructTuple(w io.Writer, gti *GenTypeInfo) (err error) { +func (g Gen) emitCborMarshalStructTuple(w io.Writer, gti *GenTypeInfo) (err error) { // 9 byte buffer to accommodate for the maximum header length (cbor varints are maximum 9 bytes_ if gti.Transparent { - err = doTemplate(w, gti, ` + err = g.doTemplate(w, gti, ` func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { cw := cbg.NewCborWriter(w) `) } else { - err = doTemplate(w, gti, `var lengthBuf{{ .Name }} = {{ .TupleHeaderAsByteString }} + err = g.doTemplate(w, gti, `var lengthBuf{{ .Name }} = {{ .TupleHeaderAsByteString }} func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { if t == nil { _, err := w.Write(cbg.CborNull) @@ -754,39 +791,39 @@ func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { switch f.Type.Kind() { case reflect.String: - if err := emitCborMarshalStringField(w, f); err != nil { + if err := g.emitCborMarshalStringField(w, f); err != nil { return err } case reflect.Struct: - if err := emitCborMarshalStructField(w, f); err != nil { + if err := g.emitCborMarshalStructField(w, f); err != nil { return err } case reflect.Uint64: - if err := emitCborMarshalUint64Field(w, f); err != nil { + if err := g.emitCborMarshalUint64Field(w, f); err != nil { return err } case reflect.Uint8: - if err := emitCborMarshalUint8Field(w, f); err != nil { + if err := g.emitCborMarshalUint8Field(w, f); err != nil { return err } case reflect.Int64: - if err := emitCborMarshalInt64Field(w, f); err != nil { + if err := g.emitCborMarshalInt64Field(w, f); err != nil { return err } case reflect.Array: - if err := emitCborMarshalArrayField(w, f); err != nil { + if err := g.emitCborMarshalArrayField(w, f); err != nil { return err } case reflect.Slice: - if err := emitCborMarshalSliceField(w, f); err != nil { + if err := g.emitCborMarshalSliceField(w, f); err != nil { return err } case reflect.Bool: - if err := emitCborMarshalBoolField(w, f); err != nil { + if err := g.emitCborMarshalBoolField(w, f); err != nil { return err } case reflect.Map: - if err := emitCborMarshalMapField(w, f); err != nil { + if err := g.emitCborMarshalMapField(w, f); err != nil { return err } default: @@ -798,9 +835,9 @@ func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { return nil } -func emitCborUnmarshalStringField(w io.Writer, f Field) error { +func (g Gen) emitCborUnmarshalStringField(w io.Writer, f Field) error { if f.Pointer { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { b, err := cr.ReadByte() if err != nil { @@ -824,7 +861,7 @@ func emitCborUnmarshalStringField(w io.Writer, f Field) error { if f.Type == nil { f.Type = reflect.TypeOf("") } - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { sval, err := cbg.ReadString(cr) if err != nil { @@ -836,10 +873,10 @@ func emitCborUnmarshalStringField(w io.Writer, f Field) error { `) } -func emitCborUnmarshalStructField(w io.Writer, f Field) error { +func (g Gen) emitCborUnmarshalStructField(w io.Writer, f Field) error { switch f.Type { case bigIntType: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err @@ -873,7 +910,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { } `) case cidType: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { {{ if .Pointer }} b, err := cr.ReadByte() @@ -898,7 +935,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { } `) case deferredType: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { {{ if .Pointer }} {{ .Name }} = new(cbg.Deferred) @@ -910,7 +947,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { `) default: - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { {{ if .Pointer }} b, err := cr.ReadByte() @@ -936,8 +973,8 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { } } -func emitCborUnmarshalInt64Field(w io.Writer, f Field) error { - return doTemplate(w, f, `{ +func (g Gen) emitCborUnmarshalInt64Field(w io.Writer, f Field) error { + return g.doTemplate(w, f, `{ maj, extra, err := {{ ReadHeader "cr" }} var extraI int64 if err != nil { @@ -964,8 +1001,8 @@ func emitCborUnmarshalInt64Field(w io.Writer, f Field) error { `) } -func emitCborUnmarshalUint64Field(w io.Writer, f Field) error { - return doTemplate(w, f, ` +func (g Gen) emitCborUnmarshalUint64Field(w io.Writer, f Field) error { + return g.doTemplate(w, f, ` { {{ if .Pointer }} b, err := cr.ReadByte() @@ -1000,8 +1037,8 @@ func emitCborUnmarshalUint64Field(w io.Writer, f Field) error { `) } -func emitCborUnmarshalUint8Field(w io.Writer, f Field) error { - return doTemplate(w, f, ` +func (g Gen) emitCborUnmarshalUint8Field(w io.Writer, f Field) error { + return g.doTemplate(w, f, ` maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err @@ -1016,9 +1053,9 @@ func emitCborUnmarshalUint8Field(w io.Writer, f Field) error { `) } -func emitCborUnmarshalBoolField(w io.Writer, f Field) error { +func (g Gen) emitCborUnmarshalBoolField(w io.Writer, f Field) error { if f.Pointer { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` { b, err := cr.ReadByte() if err != nil { @@ -1051,7 +1088,7 @@ func emitCborUnmarshalBoolField(w io.Writer, f Field) error { } `) } else { - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err @@ -1071,8 +1108,8 @@ func emitCborUnmarshalBoolField(w io.Writer, f Field) error { } } -func emitCborUnmarshalMapField(w io.Writer, f Field) error { - err := doTemplate(w, f, ` +func (g Gen) emitCborUnmarshalMapField(w io.Writer, f Field) error { + err := g.doTemplate(w, f, ` maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err @@ -1095,12 +1132,12 @@ func emitCborUnmarshalMapField(w io.Writer, f Field) error { switch f.Type.Key().Kind() { case reflect.String: - if err := doTemplate(w, f, ` + if err := g.doTemplate(w, f, ` var k string `); err != nil { return err } - if err := emitCborUnmarshalStringField(w, Field{Name: "k"}); err != nil { + if err := g.emitCborUnmarshalStringField(w, Field{Name: "k"}); err != nil { return err } default: @@ -1111,15 +1148,15 @@ func emitCborUnmarshalMapField(w io.Writer, f Field) error { t := f.Type.Elem() switch t.Kind() { case reflect.String: - if err := doTemplate(w, f, ` + if err := g.doTemplate(w, f, ` var v string `); err != nil { return err } - if err := emitCborUnmarshalStringField(w, Field{Name: "v"}); err != nil { + if err := g.emitCborUnmarshalStringField(w, Field{Name: "v"}); err != nil { return err } - if err := doTemplate(w, f, ` + if err := g.doTemplate(w, f, ` {{ .Name }}[k] = v `); err != nil { return err @@ -1133,7 +1170,7 @@ func emitCborUnmarshalMapField(w io.Writer, f Field) error { fallthrough case reflect.Struct: subf := Field{Name: "v", Pointer: pointer, Type: t, Pkg: f.Pkg} - if err := doTemplate(w, subf, ` + if err := g.doTemplate(w, subf, ` var v {{ .TypeName }} `); err != nil { return err @@ -1142,10 +1179,10 @@ func emitCborUnmarshalMapField(w io.Writer, f Field) error { if pointer { subf.Type = subf.Type.Elem() } - if err := emitCborUnmarshalStructField(w, subf); err != nil { + if err := g.emitCborUnmarshalStructField(w, subf); err != nil { return err } - if err := doTemplate(w, f, ` + if err := g.doTemplate(w, f, ` {{ .Name }}[k] = v `); err != nil { return err @@ -1154,12 +1191,12 @@ func emitCborUnmarshalMapField(w io.Writer, f Field) error { return fmt.Errorf("currently only support maps of structs") } - return doTemplate(w, f, ` + return g.doTemplate(w, f, ` } `) } -func emitCborUnmarshalSliceField(w io.Writer, f Field) error { +func (g Gen) emitCborUnmarshalSliceField(w io.Writer, f Field) error { if f.IterLabel == "" { f.IterLabel = "i" } @@ -1171,7 +1208,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { e = e.Elem() } - err := doTemplate(w, f, ` + err := g.doTemplate(w, f, ` {{ if .PreserveNil }} { b, err := cr.ReadByte() @@ -1194,8 +1231,8 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { } if e.Kind() == reflect.Uint8 { - return doTemplate(w, f, ` - if extra > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} { + return g.doTemplate(w, f, ` + if extra > {{ Cfg "MaxByteLength" | MaxLen .MaxLen }} { return fmt.Errorf("{{ .Name }}: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1219,15 +1256,15 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { `) } - if err := doTemplate(w, f, ` - if extra > {{ MaxLen .MaxLen "cbg.MaxLength" }} { + if err := g.doTemplate(w, f, ` + if extra > {{ Cfg "MaxArrayLength" | MaxLen .MaxLen }} { return fmt.Errorf("{{ .Name }}: array too large (%d)", extra) } `); err != nil { return err } - err = doTemplate(w, f, ` + err = g.doTemplate(w, f, ` if maj != cbg.MajArray { return fmt.Errorf("expected cbor array") } @@ -1255,7 +1292,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { Pointer: pointer, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalStructField(w, subf) + err := g.emitCborUnmarshalStructField(w, subf) if err != nil { return err } @@ -1265,7 +1302,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { Pkg: f.Pkg, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalUint64Field(w, subf) + err := g.emitCborUnmarshalUint64Field(w, subf) if err != nil { return err } @@ -1275,7 +1312,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { Pkg: f.Pkg, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalInt64Field(w, subf) + err := g.emitCborUnmarshalInt64Field(w, subf) if err != nil { return err } @@ -1287,7 +1324,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { IterLabel: nextIter, Pkg: f.Pkg, } - if err := emitCborUnmarshalArrayField(w, subf); err != nil { + if err := g.emitCborUnmarshalArrayField(w, subf); err != nil { return err } case reflect.Slice: @@ -1298,7 +1335,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { IterLabel: nextIter, Pkg: f.Pkg, } - if err := emitCborUnmarshalSliceField(w, subf); err != nil { + if err := g.emitCborUnmarshalSliceField(w, subf); err != nil { return err } @@ -1308,7 +1345,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { Pkg: f.Pkg, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalStringField(w, subf) + err := g.emitCborUnmarshalStringField(w, subf) if err != nil { return err } @@ -1317,7 +1354,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { return fmt.Errorf("do not yet support slices of %s yet", e.Elem()) } - if err := doTemplate(w, f, ` + if err := g.doTemplate(w, f, ` {{ if .PreserveNil }} } } @@ -1331,7 +1368,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { return nil } -func emitCborUnmarshalArrayField(w io.Writer, f Field) error { +func (g Gen) emitCborUnmarshalArrayField(w io.Writer, f Field) error { if f.IterLabel == "" { f.IterLabel = "i" } @@ -1343,7 +1380,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { e = e.Elem() } - err := doTemplate(w, f, ` + err := g.doTemplate(w, f, ` maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err @@ -1354,8 +1391,8 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { } if e.Kind() == reflect.Uint8 { - return doTemplate(w, f, ` - if extra > {{ MaxLen .MaxLen "cbg.ByteArrayMaxLen" }} { + return g.doTemplate(w, f, ` + if extra > {{ Cfg "MaxByteLength" | MaxLen .MaxLen }} { return fmt.Errorf("{{ .Name }}: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1372,15 +1409,15 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { `) } - if err := doTemplate(w, f, ` - if extra > {{ MaxLen .MaxLen "cbg.MaxLength" }} { + if err := g.doTemplate(w, f, ` + if extra > {{ Cfg "MaxArrayLength" | MaxLen .MaxLen }} { return fmt.Errorf("{{ .Name }}: array too large (%d)", extra) } `); err != nil { return err } - err = doTemplate(w, f, ` + err = g.doTemplate(w, f, ` if maj != cbg.MajArray { return fmt.Errorf("expected cbor array") } @@ -1406,7 +1443,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { Pointer: pointer, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalStructField(w, subf) + err := g.emitCborUnmarshalStructField(w, subf) if err != nil { return err } @@ -1416,7 +1453,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { Pkg: f.Pkg, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalUint64Field(w, subf) + err := g.emitCborUnmarshalUint64Field(w, subf) if err != nil { return err } @@ -1426,7 +1463,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { Pkg: f.Pkg, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalInt64Field(w, subf) + err := g.emitCborUnmarshalInt64Field(w, subf) if err != nil { return err } @@ -1438,7 +1475,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { IterLabel: nextIter, Pkg: f.Pkg, } - if err := emitCborUnmarshalArrayField(w, subf); err != nil { + if err := g.emitCborUnmarshalArrayField(w, subf); err != nil { return err } case reflect.Slice: @@ -1449,7 +1486,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { IterLabel: nextIter, Pkg: f.Pkg, } - if err := emitCborUnmarshalSliceField(w, subf); err != nil { + if err := g.emitCborUnmarshalSliceField(w, subf); err != nil { return err } @@ -1459,7 +1496,7 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { Pkg: f.Pkg, Name: f.Name + "[" + f.IterLabel + "]", } - err := emitCborUnmarshalStringField(w, subf) + err := g.emitCborUnmarshalStringField(w, subf) if err != nil { return err } @@ -1473,9 +1510,9 @@ func emitCborUnmarshalArrayField(w io.Writer, f Field) error { return nil } -func emitCborUnmarshalStructTuple(w io.Writer, gti *GenTypeInfo) (err error) { +func (g Gen) emitCborUnmarshalStructTuple(w io.Writer, gti *GenTypeInfo) (err error) { if gti.Transparent { - err = doTemplate(w, gti, ` + err = g.doTemplate(w, gti, ` func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { *t = {{.Name}}{} @@ -1486,7 +1523,7 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { _ = extra `) } else { - err = doTemplate(w, gti, ` + err = g.doTemplate(w, gti, ` func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { *t = {{.Name}}{} @@ -1526,39 +1563,39 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { switch f.Type.Kind() { case reflect.String: - if err := emitCborUnmarshalStringField(w, f); err != nil { + if err := g.emitCborUnmarshalStringField(w, f); err != nil { return err } case reflect.Struct: - if err := emitCborUnmarshalStructField(w, f); err != nil { + if err := g.emitCborUnmarshalStructField(w, f); err != nil { return err } case reflect.Uint64: - if err := emitCborUnmarshalUint64Field(w, f); err != nil { + if err := g.emitCborUnmarshalUint64Field(w, f); err != nil { return err } case reflect.Uint8: - if err := emitCborUnmarshalUint8Field(w, f); err != nil { + if err := g.emitCborUnmarshalUint8Field(w, f); err != nil { return err } case reflect.Int64: - if err := emitCborUnmarshalInt64Field(w, f); err != nil { + if err := g.emitCborUnmarshalInt64Field(w, f); err != nil { return err } case reflect.Array: - if err := emitCborUnmarshalArrayField(w, f); err != nil { + if err := g.emitCborUnmarshalArrayField(w, f); err != nil { return err } case reflect.Slice: - if err := emitCborUnmarshalSliceField(w, f); err != nil { + if err := g.emitCborUnmarshalSliceField(w, f); err != nil { return err } case reflect.Bool: - if err := emitCborUnmarshalBoolField(w, f); err != nil { + if err := g.emitCborUnmarshalBoolField(w, f); err != nil { return err } case reflect.Map: - if err := emitCborUnmarshalMapField(w, f); err != nil { + if err := g.emitCborUnmarshalMapField(w, f); err != nil { return err } default: @@ -1571,13 +1608,19 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { return nil } -// Generates 'tuple representation' cbor encoders for the given type +// GenTupleEncodersForType is a convenience wrapper around Gen.GenTupleEncodersForType using +// default options. func GenTupleEncodersForType(gti *GenTypeInfo, w io.Writer) error { - if err := emitCborMarshalStructTuple(w, gti); err != nil { + return Gen{}.GenTupleEncodersForType(gti, w) +} + +// Generates 'tuple representation' cbor encoders for the given type +func (g Gen) GenTupleEncodersForType(gti *GenTypeInfo, w io.Writer) error { + if err := g.emitCborMarshalStructTuple(w, gti); err != nil { return err } - if err := emitCborUnmarshalStructTuple(w, gti); err != nil { + if err := g.emitCborUnmarshalStructTuple(w, gti); err != nil { return err } @@ -1599,7 +1642,7 @@ func emptyValForField(f Field) (string, error) { } } -func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { +func (g Gen) emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { var hasOmitEmpty bool for _, f := range gti.Fields { if f.OmitEmpty { @@ -1611,7 +1654,7 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { return fmt.Errorf("transparent fields not supported in map mode, use tuple encoding (outcome should be the same)") } - err := doTemplate(w, gti, `func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { + err := g.doTemplate(w, gti, `func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { if t == nil { _, err := w.Write(cbg.CborNull) return err @@ -1627,7 +1670,7 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { fmt.Fprintf(w, "fieldCount := %d\n", len(gti.Fields)) for _, f := range gti.Fields { if f.OmitEmpty { - err = doTemplate(w, f, ` + err = g.doTemplate(w, f, ` if t.{{ .Name }} == {{ .EmptyVal }} { fieldCount-- } @@ -1649,7 +1692,7 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { } else { - err = doTemplate(w, gti, ` + err = g.doTemplate(w, gti, ` if _, err := cw.Write({{ .MapHeaderAsByteString }}); err != nil { return err } @@ -1678,12 +1721,12 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { fmt.Fprintf(w, "\n\t// t.%s (%s) (%s)", f.Name, f.Type, f.Type.Kind()) if f.OmitEmpty { - if err := doTemplate(w, f, "\nif t.{{.Name}} != {{ .EmptyVal }} {\n"); err != nil { + if err := g.doTemplate(w, f, "\nif t.{{.Name}} != {{ .EmptyVal }} {\n"); err != nil { return err } } - if err := emitCborMarshalStringField(w, Field{ + if err := g.emitCborMarshalStringField(w, Field{ Name: `"` + f.MapKey + `"`, }); err != nil { return err @@ -1692,39 +1735,39 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { f.Name = "t." + f.Name switch f.Type.Kind() { case reflect.String: - if err := emitCborMarshalStringField(w, f); err != nil { + if err := g.emitCborMarshalStringField(w, f); err != nil { return err } case reflect.Struct: - if err := emitCborMarshalStructField(w, f); err != nil { + if err := g.emitCborMarshalStructField(w, f); err != nil { return err } case reflect.Uint64: - if err := emitCborMarshalUint64Field(w, f); err != nil { + if err := g.emitCborMarshalUint64Field(w, f); err != nil { return err } case reflect.Int64: - if err := emitCborMarshalInt64Field(w, f); err != nil { + if err := g.emitCborMarshalInt64Field(w, f); err != nil { return err } case reflect.Uint8: - if err := emitCborMarshalUint8Field(w, f); err != nil { + if err := g.emitCborMarshalUint8Field(w, f); err != nil { return err } case reflect.Array: - if err := emitCborMarshalArrayField(w, f); err != nil { + if err := g.emitCborMarshalArrayField(w, f); err != nil { return err } case reflect.Slice: - if err := emitCborMarshalSliceField(w, f); err != nil { + if err := g.emitCborMarshalSliceField(w, f); err != nil { return err } case reflect.Bool: - if err := emitCborMarshalBoolField(w, f); err != nil { + if err := g.emitCborMarshalBoolField(w, f); err != nil { return err } case reflect.Map: - if err := emitCborMarshalMapField(w, f); err != nil { + if err := g.emitCborMarshalMapField(w, f); err != nil { return err } default: @@ -1732,7 +1775,7 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { } if f.OmitEmpty { - if err := doTemplate(w, f, "}\n"); err != nil { + if err := g.doTemplate(w, f, "}\n"); err != nil { return err } } @@ -1742,8 +1785,8 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { return nil } -func emitCborUnmarshalStructMap(w io.Writer, gti *GenTypeInfo) error { - err := doTemplate(w, gti, ` +func (g Gen) emitCborUnmarshalStructMap(w io.Writer, gti *GenTypeInfo) error { + err := g.doTemplate(w, gti, ` func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { *t = {{.Name}}{} @@ -1776,11 +1819,11 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { return err } - if err := emitCborUnmarshalStringField(w, Field{Name: "name"}); err != nil { + if err := g.emitCborUnmarshalStringField(w, Field{Name: "name"}); err != nil { return err } - err = doTemplate(w, gti, ` + err = g.doTemplate(w, gti, ` switch name { `) if err != nil { @@ -1790,7 +1833,7 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { for _, f := range gti.Fields { fmt.Fprintf(w, "// t.%s (%s) (%s)", f.Name, f.Type, f.Type.Kind()) - err := doTemplate(w, f, ` + err := g.doTemplate(w, f, ` case "{{ .MapKey }}": `) if err != nil { @@ -1801,39 +1844,39 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { switch f.Type.Kind() { case reflect.String: - if err := emitCborUnmarshalStringField(w, f); err != nil { + if err := g.emitCborUnmarshalStringField(w, f); err != nil { return err } case reflect.Struct: - if err := emitCborUnmarshalStructField(w, f); err != nil { + if err := g.emitCborUnmarshalStructField(w, f); err != nil { return err } case reflect.Uint64: - if err := emitCborUnmarshalUint64Field(w, f); err != nil { + if err := g.emitCborUnmarshalUint64Field(w, f); err != nil { return err } case reflect.Int64: - if err := emitCborUnmarshalInt64Field(w, f); err != nil { + if err := g.emitCborUnmarshalInt64Field(w, f); err != nil { return err } case reflect.Uint8: - if err := emitCborUnmarshalUint8Field(w, f); err != nil { + if err := g.emitCborUnmarshalUint8Field(w, f); err != nil { return err } case reflect.Array: - if err := emitCborUnmarshalArrayField(w, f); err != nil { + if err := g.emitCborUnmarshalArrayField(w, f); err != nil { return err } case reflect.Slice: - if err := emitCborUnmarshalSliceField(w, f); err != nil { + if err := g.emitCborUnmarshalSliceField(w, f); err != nil { return err } case reflect.Bool: - if err := emitCborUnmarshalBoolField(w, f); err != nil { + if err := g.emitCborUnmarshalBoolField(w, f); err != nil { return err } case reflect.Map: - if err := emitCborUnmarshalMapField(w, f); err != nil { + if err := g.emitCborUnmarshalMapField(w, f); err != nil { return err } default: @@ -1841,7 +1884,7 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { } } - return doTemplate(w, gti, ` + return g.doTemplate(w, gti, ` default: // Field doesn't exist on this type, so ignore it cbg.ScanForLinks(r, func(cid.Cid){}) @@ -1853,13 +1896,19 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { `) } -// Generates 'tuple representation' cbor encoders for the given type +// GenMapEncodersForType is a convenience wrapper around Gen.GenMapEncodersForType using default +// options. func GenMapEncodersForType(gti *GenTypeInfo, w io.Writer) error { - if err := emitCborMarshalStructMap(w, gti); err != nil { + return Gen{}.GenMapEncodersForType(gti, w) +} + +// Generates 'tuple representation' cbor encoders for the given type +func (g Gen) GenMapEncodersForType(gti *GenTypeInfo, w io.Writer) error { + if err := g.emitCborMarshalStructMap(w, gti); err != nil { return err } - if err := emitCborUnmarshalStructMap(w, gti); err != nil { + if err := g.emitCborUnmarshalStructMap(w, gti); err != nil { return err } diff --git a/testing/cbor_gen.go b/testing/cbor_gen.go index 7a254d6..041724e 100644 --- a/testing/cbor_gen.go +++ b/testing/cbor_gen.go @@ -33,7 +33,7 @@ func (t *SignedArray) MarshalCBOR(w io.Writer) error { } // t.Signed ([]uint64) (slice) - if len(t.Signed) > cbg.MaxLength { + if len(t.Signed) > 8192 { return xerrors.Errorf("Slice value in field t.Signed was too long") } @@ -80,7 +80,7 @@ func (t *SignedArray) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Signed: array too large (%d)", extra) } @@ -134,7 +134,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { } // t.Foo (string) (string) - if len(t.Foo) > cbg.MaxLength { + if len(t.Foo) > 8192 { return xerrors.Errorf("Value in field t.Foo was too long") } @@ -152,7 +152,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { } // t.Binary ([]uint8) (slice) - if len(t.Binary) > cbg.ByteArrayMaxLen { + if len(t.Binary) > 8192 { return xerrors.Errorf("Byte array in field t.Binary was too long") } @@ -176,7 +176,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { } // t.NString (testing.NamedString) (string) - if len(t.NString) > cbg.MaxLength { + if len(t.NString) > 8192 { return xerrors.Errorf("Value in field t.NString was too long") } @@ -188,7 +188,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { } // t.Strings ([]string) (slice) - if len(t.Strings) > cbg.MaxLength { + if len(t.Strings) > 8192 { return xerrors.Errorf("Slice value in field t.Strings was too long") } @@ -196,7 +196,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { return err } for _, v := range t.Strings { - if len(v) > cbg.MaxLength { + if len(v) > 8192 { return xerrors.Errorf("Value in field v was too long") } @@ -265,7 +265,7 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.Binary: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -322,7 +322,7 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Strings: array too large (%d)", extra) } @@ -377,7 +377,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { } // t.Others ([]uint64) (slice) - if len(t.Others) > cbg.MaxLength { + if len(t.Others) > 8192 { return xerrors.Errorf("Slice value in field t.Others was too long") } @@ -393,7 +393,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { } // t.SignedOthers ([]int64) (slice) - if len(t.SignedOthers) > cbg.MaxLength { + if len(t.SignedOthers) > 8192 { return xerrors.Errorf("Slice value in field t.SignedOthers was too long") } @@ -414,7 +414,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { } // t.Test ([][]uint8) (slice) - if len(t.Test) > cbg.MaxLength { + if len(t.Test) > 8192 { return xerrors.Errorf("Slice value in field t.Test was too long") } @@ -422,7 +422,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return err } for _, v := range t.Test { - if len(v) > cbg.ByteArrayMaxLen { + if len(v) > 8192 { return xerrors.Errorf("Byte array in field v was too long") } @@ -437,7 +437,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { } // t.Dog (string) (string) - if len(t.Dog) > cbg.MaxLength { + if len(t.Dog) > 8192 { return xerrors.Errorf("Value in field t.Dog was too long") } @@ -449,7 +449,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { } // t.Numbers ([]testing.NamedNumber) (slice) - if len(t.Numbers) > cbg.MaxLength { + if len(t.Numbers) > 8192 { return xerrors.Errorf("Slice value in field t.Numbers was too long") } @@ -489,7 +489,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { } // t.Arrrrrghay ([3]testing.SimpleTypeOne) (array) - if len(t.Arrrrrghay) > cbg.MaxLength { + if len(t.Arrrrrghay) > 8192 { return xerrors.Errorf("Slice value in field t.Arrrrrghay was too long") } @@ -553,7 +553,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Others: array too large (%d)", extra) } @@ -596,7 +596,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.SignedOthers: array too large (%d)", extra) } @@ -650,7 +650,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Test: array too large (%d)", extra) } @@ -676,7 +676,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.Test[i]: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -710,7 +710,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Numbers: array too large (%d)", extra) } @@ -801,7 +801,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Arrrrrghay: array too large (%d)", extra) } @@ -952,7 +952,7 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { } // t.Bytes ([20]uint8) (array) - if len(t.Bytes) > cbg.ByteArrayMaxLen { + if len(t.Bytes) > 8192 { return xerrors.Errorf("Byte array in field t.Bytes was too long") } @@ -965,7 +965,7 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { } // t.Uint8 ([20]uint8) (array) - if len(t.Uint8) > cbg.ByteArrayMaxLen { + if len(t.Uint8) > 8192 { return xerrors.Errorf("Byte array in field t.Uint8 was too long") } @@ -978,7 +978,7 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { } // t.Uint64 ([20]uint64) (array) - if len(t.Uint64) > cbg.MaxLength { + if len(t.Uint64) > 8192 { return xerrors.Errorf("Slice value in field t.Uint64 was too long") } @@ -1025,7 +1025,7 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.Bytes: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1046,7 +1046,7 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.Uint8: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1067,7 +1067,7 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Uint64: array too large (%d)", extra) } @@ -1137,7 +1137,7 @@ func (t *ThingWithSomeTime) MarshalCBOR(w io.Writer) error { } // t.CatName (string) (string) - if len(t.CatName) > cbg.MaxLength { + if len(t.CatName) > 8192 { return xerrors.Errorf("Value in field t.CatName was too long") } @@ -1302,7 +1302,7 @@ func (t *IntArray) MarshalCBOR(w io.Writer) error { cw := cbg.NewCborWriter(w) // t.Ints ([]int64) (slice) - if len(t.Ints) > cbg.MaxLength { + if len(t.Ints) > 8192 { return xerrors.Errorf("Slice value in field t.Ints was too long") } @@ -1339,7 +1339,7 @@ func (t *IntArray) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Ints: array too large (%d)", extra) } @@ -1393,7 +1393,7 @@ func (t *IntAliasArray) MarshalCBOR(w io.Writer) error { cw := cbg.NewCborWriter(w) // t.Ints ([]testing.IntAlias) (slice) - if len(t.Ints) > cbg.MaxLength { + if len(t.Ints) > 8192 { return xerrors.Errorf("Slice value in field t.Ints was too long") } @@ -1430,7 +1430,7 @@ func (t *IntAliasArray) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Ints: array too large (%d)", extra) } @@ -1634,7 +1634,7 @@ func (t *IntArrayNewType) MarshalCBOR(w io.Writer) error { cw := cbg.NewCborWriter(w) // (*t) (testing.IntArrayNewType) (slice) - if len((*t)) > cbg.MaxLength { + if len((*t)) > 8192 { return xerrors.Errorf("Slice value in field (*t) was too long") } @@ -1671,7 +1671,7 @@ func (t *IntArrayNewType) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("(*t): array too large (%d)", extra) } @@ -1725,7 +1725,7 @@ func (t *IntArrayAliasNewType) MarshalCBOR(w io.Writer) error { cw := cbg.NewCborWriter(w) // (*t) (testing.IntArrayAliasNewType) (slice) - if len((*t)) > cbg.MaxLength { + if len((*t)) > 8192 { return xerrors.Errorf("Slice value in field (*t) was too long") } @@ -1762,7 +1762,7 @@ func (t *IntArrayAliasNewType) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("(*t): array too large (%d)", extra) } @@ -1833,7 +1833,7 @@ func (t *MapTransparentType) MarshalCBOR(w io.Writer) error { for _, k := range keys { v := (*t)[k] - if len(k) > cbg.MaxLength { + if len(k) > 8192 { return xerrors.Errorf("Value in field k was too long") } @@ -1844,7 +1844,7 @@ func (t *MapTransparentType) MarshalCBOR(w io.Writer) error { return err } - if len(v) > cbg.MaxLength { + if len(v) > 8192 { return xerrors.Errorf("Value in field v was too long") } diff --git a/testing/cbor_map_gen.go b/testing/cbor_map_gen.go index 10fc666..c088aed 100644 --- a/testing/cbor_map_gen.go +++ b/testing/cbor_map_gen.go @@ -31,7 +31,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.Dog (string) (string) - if len("Dog") > cbg.MaxLength { + if len("Dog") > 8192 { return xerrors.Errorf("Value in field \"Dog\" was too long") } @@ -42,7 +42,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } - if len(t.Dog) > cbg.MaxLength { + if len(t.Dog) > 8192 { return xerrors.Errorf("Value in field t.Dog was too long") } @@ -54,7 +54,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.Test ([][]uint8) (slice) - if len("Test") > cbg.MaxLength { + if len("Test") > 8192 { return xerrors.Errorf("Value in field \"Test\" was too long") } @@ -65,7 +65,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } - if len(t.Test) > cbg.MaxLength { + if len(t.Test) > 8192 { return xerrors.Errorf("Slice value in field t.Test was too long") } @@ -73,7 +73,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } for _, v := range t.Test { - if len(v) > cbg.ByteArrayMaxLen { + if len(v) > 8192 { return xerrors.Errorf("Byte array in field v was too long") } @@ -88,7 +88,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.Stuff (testing.SimpleTypeTree) (struct) - if len("Stuff") > cbg.MaxLength { + if len("Stuff") > 8192 { return xerrors.Errorf("Value in field \"Stuff\" was too long") } @@ -104,7 +104,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.Others ([]uint64) (slice) - if len("Others") > cbg.MaxLength { + if len("Others") > 8192 { return xerrors.Errorf("Value in field \"Others\" was too long") } @@ -115,7 +115,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } - if len(t.Others) > cbg.MaxLength { + if len(t.Others) > 8192 { return xerrors.Errorf("Slice value in field t.Others was too long") } @@ -131,7 +131,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.Stufff (testing.SimpleTypeTwo) (struct) - if len("Stufff") > cbg.MaxLength { + if len("Stufff") > 8192 { return xerrors.Errorf("Value in field \"Stufff\" was too long") } @@ -147,7 +147,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.BoolPtr (bool) (bool) - if len("BoolPtr") > cbg.MaxLength { + if len("BoolPtr") > 8192 { return xerrors.Errorf("Value in field \"BoolPtr\" was too long") } @@ -169,7 +169,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.NotPizza (uint64) (uint64) - if len("NotPizza") > cbg.MaxLength { + if len("NotPizza") > 8192 { return xerrors.Errorf("Value in field \"NotPizza\" was too long") } @@ -191,7 +191,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.StringPtr (string) (string) - if len("StringPtr") > cbg.MaxLength { + if len("StringPtr") > 8192 { return xerrors.Errorf("Value in field \"StringPtr\" was too long") } @@ -207,7 +207,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } } else { - if len(*t.StringPtr) > cbg.MaxLength { + if len(*t.StringPtr) > 8192 { return xerrors.Errorf("Value in field t.StringPtr was too long") } @@ -220,7 +220,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } // t.SixtyThreeBitIntegerWithASignBit (int64) (int64) - if len("SixtyThreeBitIntegerWithASignBit") > cbg.MaxLength { + if len("SixtyThreeBitIntegerWithASignBit") > 8192 { return xerrors.Errorf("Value in field \"SixtyThreeBitIntegerWithASignBit\" was too long") } @@ -300,7 +300,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Test: array too large (%d)", extra) } @@ -326,7 +326,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.Test[i]: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -371,7 +371,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Others: array too large (%d)", extra) } @@ -554,7 +554,7 @@ func (t *NeedScratchForMap) MarshalCBOR(w io.Writer) error { } // t.Thing (bool) (bool) - if len("Thing") > cbg.MaxLength { + if len("Thing") > 8192 { return xerrors.Errorf("Value in field \"Thing\" was too long") } @@ -649,7 +649,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldMap (map[string]testing.SimpleTypeOne) (map) - if len("OldMap") > cbg.MaxLength { + if len("OldMap") > 8192 { return xerrors.Errorf("Value in field \"OldMap\" was too long") } @@ -677,7 +677,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { for _, k := range keys { v := t.OldMap[k] - if len(k) > cbg.MaxLength { + if len(k) > 8192 { return xerrors.Errorf("Value in field k was too long") } @@ -696,7 +696,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldNum (uint64) (uint64) - if len("OldNum") > cbg.MaxLength { + if len("OldNum") > 8192 { return xerrors.Errorf("Value in field \"OldNum\" was too long") } @@ -712,7 +712,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldPtr (cid.Cid) (struct) - if len("OldPtr") > cbg.MaxLength { + if len("OldPtr") > 8192 { return xerrors.Errorf("Value in field \"OldPtr\" was too long") } @@ -734,7 +734,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldStr (string) (string) - if len("OldStr") > cbg.MaxLength { + if len("OldStr") > 8192 { return xerrors.Errorf("Value in field \"OldStr\" was too long") } @@ -745,7 +745,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldStr) > cbg.MaxLength { + if len(t.OldStr) > 8192 { return xerrors.Errorf("Value in field t.OldStr was too long") } @@ -757,7 +757,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldArray ([]testing.SimpleTypeOne) (slice) - if len("OldArray") > cbg.MaxLength { + if len("OldArray") > 8192 { return xerrors.Errorf("Value in field \"OldArray\" was too long") } @@ -768,7 +768,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldArray) > cbg.MaxLength { + if len(t.OldArray) > 8192 { return xerrors.Errorf("Slice value in field t.OldArray was too long") } @@ -783,7 +783,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldBytes ([]uint8) (slice) - if len("OldBytes") > cbg.MaxLength { + if len("OldBytes") > 8192 { return xerrors.Errorf("Value in field \"OldBytes\" was too long") } @@ -794,7 +794,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldBytes) > cbg.ByteArrayMaxLen { + if len(t.OldBytes) > 8192 { return xerrors.Errorf("Byte array in field t.OldBytes was too long") } @@ -807,7 +807,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldStruct (testing.SimpleTypeOne) (struct) - if len("OldStruct") > cbg.MaxLength { + if len("OldStruct") > 8192 { return xerrors.Errorf("Value in field \"OldStruct\" was too long") } @@ -823,7 +823,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldCidArray ([]cid.Cid) (slice) - if len("OldCidArray") > cbg.MaxLength { + if len("OldCidArray") > 8192 { return xerrors.Errorf("Value in field \"OldCidArray\" was too long") } @@ -834,7 +834,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldCidArray) > cbg.MaxLength { + if len(t.OldCidArray) > 8192 { return xerrors.Errorf("Slice value in field t.OldCidArray was too long") } @@ -850,7 +850,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } // t.OldCidPtrArray ([]*cid.Cid) (slice) - if len("OldCidPtrArray") > cbg.MaxLength { + if len("OldCidPtrArray") > 8192 { return xerrors.Errorf("Value in field \"OldCidPtrArray\" was too long") } @@ -861,7 +861,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldCidPtrArray) > cbg.MaxLength { + if len(t.OldCidPtrArray) > 8192 { return xerrors.Errorf("Slice value in field t.OldCidPtrArray was too long") } @@ -1021,7 +1021,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.OldArray: array too large (%d)", extra) } @@ -1060,7 +1060,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.OldBytes: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1093,7 +1093,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.OldCidArray: array too large (%d)", extra) } @@ -1135,7 +1135,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.OldCidPtrArray: array too large (%d)", extra) } @@ -1201,7 +1201,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewMap (map[string]testing.SimpleTypeOne) (map) - if len("NewMap") > cbg.MaxLength { + if len("NewMap") > 8192 { return xerrors.Errorf("Value in field \"NewMap\" was too long") } @@ -1229,7 +1229,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { for _, k := range keys { v := t.NewMap[k] - if len(k) > cbg.MaxLength { + if len(k) > 8192 { return xerrors.Errorf("Value in field k was too long") } @@ -1248,7 +1248,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewNum (uint64) (uint64) - if len("NewNum") > cbg.MaxLength { + if len("NewNum") > 8192 { return xerrors.Errorf("Value in field \"NewNum\" was too long") } @@ -1264,7 +1264,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewPtr (cid.Cid) (struct) - if len("NewPtr") > cbg.MaxLength { + if len("NewPtr") > 8192 { return xerrors.Errorf("Value in field \"NewPtr\" was too long") } @@ -1286,7 +1286,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewStr (string) (string) - if len("NewStr") > cbg.MaxLength { + if len("NewStr") > 8192 { return xerrors.Errorf("Value in field \"NewStr\" was too long") } @@ -1297,7 +1297,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - if len(t.NewStr) > cbg.MaxLength { + if len(t.NewStr) > 8192 { return xerrors.Errorf("Value in field t.NewStr was too long") } @@ -1309,7 +1309,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldMap (map[string]testing.SimpleTypeOne) (map) - if len("OldMap") > cbg.MaxLength { + if len("OldMap") > 8192 { return xerrors.Errorf("Value in field \"OldMap\" was too long") } @@ -1337,7 +1337,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { for _, k := range keys { v := t.OldMap[k] - if len(k) > cbg.MaxLength { + if len(k) > 8192 { return xerrors.Errorf("Value in field k was too long") } @@ -1356,7 +1356,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldNum (uint64) (uint64) - if len("OldNum") > cbg.MaxLength { + if len("OldNum") > 8192 { return xerrors.Errorf("Value in field \"OldNum\" was too long") } @@ -1372,7 +1372,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldPtr (cid.Cid) (struct) - if len("OldPtr") > cbg.MaxLength { + if len("OldPtr") > 8192 { return xerrors.Errorf("Value in field \"OldPtr\" was too long") } @@ -1394,7 +1394,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldStr (string) (string) - if len("OldStr") > cbg.MaxLength { + if len("OldStr") > 8192 { return xerrors.Errorf("Value in field \"OldStr\" was too long") } @@ -1405,7 +1405,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldStr) > cbg.MaxLength { + if len(t.OldStr) > 8192 { return xerrors.Errorf("Value in field t.OldStr was too long") } @@ -1417,7 +1417,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewArray ([]testing.SimpleTypeOne) (slice) - if len("NewArray") > cbg.MaxLength { + if len("NewArray") > 8192 { return xerrors.Errorf("Value in field \"NewArray\" was too long") } @@ -1428,7 +1428,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - if len(t.NewArray) > cbg.MaxLength { + if len(t.NewArray) > 8192 { return xerrors.Errorf("Slice value in field t.NewArray was too long") } @@ -1443,7 +1443,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewBytes ([]uint8) (slice) - if len("NewBytes") > cbg.MaxLength { + if len("NewBytes") > 8192 { return xerrors.Errorf("Value in field \"NewBytes\" was too long") } @@ -1454,7 +1454,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - if len(t.NewBytes) > cbg.ByteArrayMaxLen { + if len(t.NewBytes) > 8192 { return xerrors.Errorf("Byte array in field t.NewBytes was too long") } @@ -1467,7 +1467,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldArray ([]testing.SimpleTypeOne) (slice) - if len("OldArray") > cbg.MaxLength { + if len("OldArray") > 8192 { return xerrors.Errorf("Value in field \"OldArray\" was too long") } @@ -1478,7 +1478,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldArray) > cbg.MaxLength { + if len(t.OldArray) > 8192 { return xerrors.Errorf("Slice value in field t.OldArray was too long") } @@ -1493,7 +1493,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldBytes ([]uint8) (slice) - if len("OldBytes") > cbg.MaxLength { + if len("OldBytes") > 8192 { return xerrors.Errorf("Value in field \"OldBytes\" was too long") } @@ -1504,7 +1504,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - if len(t.OldBytes) > cbg.ByteArrayMaxLen { + if len(t.OldBytes) > 8192 { return xerrors.Errorf("Byte array in field t.OldBytes was too long") } @@ -1517,7 +1517,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.NewStruct (testing.SimpleTypeOne) (struct) - if len("NewStruct") > cbg.MaxLength { + if len("NewStruct") > 8192 { return xerrors.Errorf("Value in field \"NewStruct\" was too long") } @@ -1533,7 +1533,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } // t.OldStruct (testing.SimpleTypeOne) (struct) - if len("OldStruct") > cbg.MaxLength { + if len("OldStruct") > 8192 { return xerrors.Errorf("Value in field \"OldStruct\" was too long") } @@ -1778,7 +1778,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.NewArray: array too large (%d)", extra) } @@ -1817,7 +1817,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.NewBytes: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1840,7 +1840,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.OldArray: array too large (%d)", extra) } @@ -1879,7 +1879,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.OldBytes: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -1936,7 +1936,7 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { } // t.Foo (int64) (int64) - if len("foo") > cbg.MaxLength { + if len("foo") > 8192 { return xerrors.Errorf("Value in field \"foo\" was too long") } @@ -1958,7 +1958,7 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { } // t.Bar (string) (string) - if len("beep") > cbg.MaxLength { + if len("beep") > 8192 { return xerrors.Errorf("Value in field \"beep\" was too long") } @@ -1969,7 +1969,7 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { return err } - if len(t.Bar) > cbg.MaxLength { + if len(t.Bar) > 8192 { return xerrors.Errorf("Value in field t.Bar was too long") } @@ -2088,7 +2088,7 @@ func (t *TestEmpty) MarshalCBOR(w io.Writer) error { } // t.Cat (int64) (int64) - if len("Cat") > cbg.MaxLength { + if len("Cat") > 8192 { return xerrors.Errorf("Value in field \"Cat\" was too long") } @@ -2112,7 +2112,7 @@ func (t *TestEmpty) MarshalCBOR(w io.Writer) error { // t.Foo (string) (string) if t.Foo != nil { - if len("Foo") > cbg.MaxLength { + if len("Foo") > 8192 { return xerrors.Errorf("Value in field \"Foo\" was too long") } @@ -2128,7 +2128,7 @@ func (t *TestEmpty) MarshalCBOR(w io.Writer) error { return err } } else { - if len(*t.Foo) > cbg.MaxLength { + if len(*t.Foo) > 8192 { return xerrors.Errorf("Value in field t.Foo was too long") } @@ -2144,7 +2144,7 @@ func (t *TestEmpty) MarshalCBOR(w io.Writer) error { // t.Beep (string) (string) if t.Beep != "" { - if len("Beep") > cbg.MaxLength { + if len("Beep") > 8192 { return xerrors.Errorf("Value in field \"Beep\" was too long") } @@ -2155,7 +2155,7 @@ func (t *TestEmpty) MarshalCBOR(w io.Writer) error { return err } - if len(t.Beep) > cbg.MaxLength { + if len(t.Beep) > 8192 { return xerrors.Errorf("Value in field t.Beep was too long") } @@ -2287,7 +2287,7 @@ func (t *TestConstField) MarshalCBOR(w io.Writer) error { } // t.Cats (string) (string) - if len("Cats") > cbg.MaxLength { + if len("Cats") > 8192 { return xerrors.Errorf("Value in field \"Cats\" was too long") } @@ -2306,7 +2306,7 @@ func (t *TestConstField) MarshalCBOR(w io.Writer) error { } // t.Thing (int64) (int64) - if len("Thing") > cbg.MaxLength { + if len("Thing") > 8192 { return xerrors.Errorf("Value in field \"Thing\" was too long") } @@ -2426,7 +2426,7 @@ func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { } // t.Zp (string) (string) - if len("ap") > cbg.MaxLength { + if len("ap") > 8192 { return xerrors.Errorf("Value in field \"ap\" was too long") } @@ -2437,7 +2437,7 @@ func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { return err } - if len(t.Zp) > cbg.MaxLength { + if len(t.Zp) > 8192 { return xerrors.Errorf("Value in field t.Zp was too long") } @@ -2449,7 +2449,7 @@ func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { } // t.Foo (int64) (int64) - if len("foo") > cbg.MaxLength { + if len("foo") > 8192 { return xerrors.Errorf("Value in field \"foo\" was too long") } @@ -2471,7 +2471,7 @@ func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { } // t.Bar (string) (string) - if len("beep") > cbg.MaxLength { + if len("beep") > 8192 { return xerrors.Errorf("Value in field \"beep\" was too long") } @@ -2482,7 +2482,7 @@ func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { return err } - if len(t.Bar) > cbg.MaxLength { + if len(t.Bar) > 8192 { return xerrors.Errorf("Value in field t.Bar was too long") } @@ -2494,7 +2494,7 @@ func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { } // t.Drond (int64) (int64) - if len("Drond") > cbg.MaxLength { + if len("Drond") > 8192 { return xerrors.Errorf("Value in field \"Drond\" was too long") } @@ -2651,7 +2651,7 @@ func (t *MapStringString) MarshalCBOR(w io.Writer) error { } // t.Snorkleblump (map[string]string) (map) - if len("Snorkleblump") > cbg.MaxLength { + if len("Snorkleblump") > 8192 { return xerrors.Errorf("Value in field \"Snorkleblump\" was too long") } @@ -2679,7 +2679,7 @@ func (t *MapStringString) MarshalCBOR(w io.Writer) error { for _, k := range keys { v := t.Snorkleblump[k] - if len(k) > cbg.MaxLength { + if len(k) > 8192 { return xerrors.Errorf("Value in field k was too long") } @@ -2690,7 +2690,7 @@ func (t *MapStringString) MarshalCBOR(w io.Writer) error { return err } - if len(v) > cbg.MaxLength { + if len(v) > 8192 { return xerrors.Errorf("Value in field v was too long") } @@ -2809,7 +2809,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { } // t.Cat (string) (string) - if len("Cat") > cbg.MaxLength { + if len("Cat") > 8192 { return xerrors.Errorf("Value in field \"Cat\" was too long") } @@ -2820,7 +2820,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { return err } - if len(t.Cat) > cbg.MaxLength { + if len(t.Cat) > 8192 { return xerrors.Errorf("Value in field t.Cat was too long") } @@ -2832,7 +2832,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { } // t.Not ([]uint64) (slice) - if len("Not") > cbg.MaxLength { + if len("Not") > 8192 { return xerrors.Errorf("Value in field \"Not\" was too long") } @@ -2843,7 +2843,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { return err } - if len(t.Not) > cbg.MaxLength { + if len(t.Not) > 8192 { return xerrors.Errorf("Slice value in field t.Not was too long") } @@ -2868,7 +2868,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { } // t.Beep (int64) (int64) - if len("Beep") > cbg.MaxLength { + if len("Beep") > 8192 { return xerrors.Errorf("Value in field \"Beep\" was too long") } @@ -2890,7 +2890,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { } // t.Other ([]uint8) (slice) - if len("Other") > cbg.MaxLength { + if len("Other") > 8192 { return xerrors.Errorf("Value in field \"Other\" was too long") } @@ -2901,7 +2901,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { return err } - if len(t.Other) > cbg.ByteArrayMaxLen { + if len(t.Other) > 8192 { return xerrors.Errorf("Byte array in field t.Other was too long") } @@ -2914,7 +2914,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { } // t.Stuff ([]uint64) (slice) - if len("Stuff") > cbg.MaxLength { + if len("Stuff") > 8192 { return xerrors.Errorf("Value in field \"Stuff\" was too long") } @@ -2925,7 +2925,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { return err } - if len(t.Stuff) > cbg.MaxLength { + if len(t.Stuff) > 8192 { return xerrors.Errorf("Slice value in field t.Stuff was too long") } @@ -2941,7 +2941,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { } // t.NotOther ([]uint8) (slice) - if len("NotOther") > cbg.MaxLength { + if len("NotOther") > 8192 { return xerrors.Errorf("Value in field \"NotOther\" was too long") } @@ -2952,7 +2952,7 @@ func (t *TestSliceNilPreserve) MarshalCBOR(w io.Writer) error { return err } - if len(t.NotOther) > cbg.ByteArrayMaxLen { + if len(t.NotOther) > 8192 { return xerrors.Errorf("Byte array in field t.NotOther was too long") } @@ -3043,7 +3043,7 @@ func (t *TestSliceNilPreserve) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Not: array too large (%d)", extra) } @@ -3114,7 +3114,7 @@ func (t *TestSliceNilPreserve) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.Other: byte array too large (%d)", extra) } if maj != cbg.MajByteString { @@ -3137,7 +3137,7 @@ func (t *TestSliceNilPreserve) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.MaxLength { + if extra > 8192 { return fmt.Errorf("t.Stuff: array too large (%d)", extra) } @@ -3191,7 +3191,7 @@ func (t *TestSliceNilPreserve) UnmarshalCBOR(r io.Reader) (err error) { return err } - if extra > cbg.ByteArrayMaxLen { + if extra > 8192 { return fmt.Errorf("t.NotOther: byte array too large (%d)", extra) } if maj != cbg.MajByteString { diff --git a/writefile.go b/writefile.go index ece1eae..4804fc7 100644 --- a/writefile.go +++ b/writefile.go @@ -9,12 +9,18 @@ import ( "golang.org/x/xerrors" ) +// WriteTupleFileEncodersToFile is a convenience wrapper around Gen.WriteTupleEncodersToFile using +// default options. +func WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error { + return Gen{}.WriteTupleEncodersToFile(fname, pkg, types...) +} + // WriteTupleFileEncodersToFile generates array backed MarshalCBOR and UnmarshalCBOR implementations for the // given types in the specified file, with the specified package name. // // The MarshalCBOR and UnmarshalCBOR implementations will marshal/unmarshal each type's fields as a // fixed-length CBOR array of field values. -func WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error { +func (g Gen) WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error { buf := new(bytes.Buffer) typeInfos := make([]*GenTypeInfo, len(types)) @@ -26,12 +32,12 @@ func WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error { typeInfos[i] = gti } - if err := PrintHeaderAndUtilityMethods(buf, pkg, typeInfos); err != nil { + if err := g.PrintHeaderAndUtilityMethods(buf, pkg, typeInfos); err != nil { return xerrors.Errorf("failed to write header: %w", err) } for _, t := range typeInfos { - if err := GenTupleEncodersForType(t, buf); err != nil { + if err := g.GenTupleEncodersForType(t, buf); err != nil { return xerrors.Errorf("failed to generate encoders: %w", err) } } @@ -56,12 +62,18 @@ func WriteTupleEncodersToFile(fname, pkg string, types ...interface{}) error { return nil } +// WriteMapFileEncodersToFile is a convenience wrapper around Gen.WriteMapEncodersToFile using +// default options. +func WriteMapEncodersToFile(fname, pkg string, types ...interface{}) error { + return Gen{}.WriteMapEncodersToFile(fname, pkg, types...) +} + // WriteMapFileEncodersToFile generates map backed MarshalCBOR and UnmarshalCBOR implementations for // the given types in the specified file, with the specified package name. // // The MarshalCBOR and UnmarshalCBOR implementations will marshal/unmarshal each type's fields as a // map of field names to field values. -func WriteMapEncodersToFile(fname, pkg string, types ...interface{}) error { +func (g Gen) WriteMapEncodersToFile(fname, pkg string, types ...interface{}) error { buf := new(bytes.Buffer) typeInfos := make([]*GenTypeInfo, len(types)) @@ -73,12 +85,12 @@ func WriteMapEncodersToFile(fname, pkg string, types ...interface{}) error { typeInfos[i] = gti } - if err := PrintHeaderAndUtilityMethods(buf, pkg, typeInfos); err != nil { + if err := g.PrintHeaderAndUtilityMethods(buf, pkg, typeInfos); err != nil { return xerrors.Errorf("failed to write header: %w", err) } for _, t := range typeInfos { - if err := GenMapEncodersForType(t, buf); err != nil { + if err := g.GenMapEncodersForType(t, buf); err != nil { return xerrors.Errorf("failed to generate encoders: %w", err) } }