diff --git a/gen.go b/gen.go index 9e1d0aa..bcf6cfe 100644 --- a/gen.go +++ b/gen.go @@ -5,6 +5,7 @@ import ( "io" "math/big" "reflect" + "sort" "strconv" "strings" "text/template" @@ -134,6 +135,10 @@ func (f Field) IsArray() bool { return f.Type.Kind() == reflect.Array } +func (f Field) EmptyVal() (string, error) { + return emptyValForField(f) +} + func (f Field) Len() int { return f.Type.Len() } @@ -196,6 +201,10 @@ func ParseTypeInfo(i interface{}) (*GenTypeInfo, error) { return nil, fmt.Errorf("invalid tag format: %w", err) } + if _, ok := tags["ignore"]; ok { + continue + } + if tags["name"] != "" { mapk = tags["name"] } @@ -250,6 +259,8 @@ func tagparse(v string) (map[string]string, error) { out[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) } else if elem == "omitempty" { out["omitempty"] = "true" + } else if elem == "ignore" || elem == "-" { + out["ignore"] = "true" } else { out["name"] = elem } @@ -1230,6 +1241,19 @@ func GenTupleEncodersForType(gti *GenTypeInfo, w io.Writer) error { return nil } +func emptyValForField(f Field) (string, error) { + if f.Pointer { + return "nil", nil + } else { + switch f.Type.Kind() { + case reflect.String: + return "\"\"", nil + default: + return "", fmt.Errorf("omit empty not supported for %s", f.Type.Kind()) + } + } +} + func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { var hasOmitEmpty bool for _, f := range gti.Fields { @@ -1254,8 +1278,8 @@ 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, ` - if t.{{ .Name }} == nil { + err = doTemplate(w, f, ` + if t.{{ .Name }} == {{ .EmptyVal }} { fieldCount-- } `) @@ -1286,11 +1310,26 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { } } + sort.Slice(gti.Fields, func(i, j int) bool { + fi := gti.Fields[i] + fj := gti.Fields[j] + + if len(fi.MapKey) < len(fj.MapKey) { + return true + } + if len(fi.MapKey) > len(fj.MapKey) { + return false + } + + // TODO: is this properly canonical? + return fi.MapKey < fj.MapKey + }) + for _, f := range gti.Fields { 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}} != nil {\n"); err != nil { + if err := doTemplate(w, f, "\nif t.{{.Name}} != {{ .EmptyVal }} {\n"); err != nil { return err } } diff --git a/testgen/main.go b/testgen/main.go index c4ba59a..e1f7bd9 100644 --- a/testgen/main.go +++ b/testgen/main.go @@ -26,6 +26,7 @@ func main() { types.RenamedFields{}, types.TestEmpty{}, types.TestConstField{}, + types.TestCanonicalFieldOrder{}, ); err != nil { panic(err) } diff --git a/testing/cbor_map_gen.go b/testing/cbor_map_gen.go index 987c358..465cafd 100644 --- a/testing/cbor_map_gen.go +++ b/testing/cbor_map_gen.go @@ -30,62 +30,28 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } - // t.Stuff (testing.SimpleTypeTree) (struct) - if len("Stuff") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Stuff\" was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Stuff"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("Stuff")); err != nil { - return err - } - - if err := t.Stuff.MarshalCBOR(cw); err != nil { - return err - } - - // t.Stufff (testing.SimpleTypeTwo) (struct) - if len("Stufff") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Stufff\" was too long") + // t.Dog (string) (string) + if len("Dog") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Dog\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Stufff"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("Stufff")); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Dog"))); err != nil { return err } - - if err := t.Stufff.MarshalCBOR(cw); err != nil { + if _, err := io.WriteString(w, string("Dog")); err != nil { return err } - // t.Others ([]uint64) (slice) - if len("Others") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Others\" was too long") + if len(t.Dog) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Dog was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Others"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("Others")); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Dog))); err != nil { return err } - - if len(t.Others) > cbg.MaxLength { - return xerrors.Errorf("Slice value in field t.Others was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Others))); err != nil { + if _, err := io.WriteString(w, string(t.Dog)); err != nil { return err } - for _, v := range t.Others { - if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { - return err - } - } // t.Test ([][]uint8) (slice) if len("Test") > cbg.MaxLength { @@ -120,49 +86,61 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } } - // t.Dog (string) (string) - if len("Dog") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Dog\" was too long") + // t.Stuff (testing.SimpleTypeTree) (struct) + if len("Stuff") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Stuff\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Dog"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Stuff"))); err != nil { return err } - if _, err := io.WriteString(w, string("Dog")); err != nil { + if _, err := io.WriteString(w, string("Stuff")); err != nil { return err } - if len(t.Dog) > cbg.MaxLength { - return xerrors.Errorf("Value in field t.Dog was too long") + if err := t.Stuff.MarshalCBOR(cw); err != nil { + return err } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Dog))); err != nil { + // t.Others ([]uint64) (slice) + if len("Others") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Others\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Others"))); err != nil { return err } - if _, err := io.WriteString(w, string(t.Dog)); err != nil { + if _, err := io.WriteString(w, string("Others")); err != nil { return err } - // t.SixtyThreeBitIntegerWithASignBit (int64) (int64) - if len("SixtyThreeBitIntegerWithASignBit") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"SixtyThreeBitIntegerWithASignBit\" was too long") + if len(t.Others) > cbg.MaxLength { + return xerrors.Errorf("Slice value in field t.Others was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SixtyThreeBitIntegerWithASignBit"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Others))); err != nil { return err } - if _, err := io.WriteString(w, string("SixtyThreeBitIntegerWithASignBit")); err != nil { + for _, v := range t.Others { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { + return err + } + } + + // t.Stufff (testing.SimpleTypeTwo) (struct) + if len("Stufff") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Stufff\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Stufff"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("Stufff")); err != nil { return err } - if t.SixtyThreeBitIntegerWithASignBit >= 0 { - if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SixtyThreeBitIntegerWithASignBit)); err != nil { - return err - } - } else { - if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.SixtyThreeBitIntegerWithASignBit-1)); err != nil { - return err - } + if err := t.Stufff.MarshalCBOR(cw); err != nil { + return err } // t.NotPizza (uint64) (uint64) @@ -215,6 +193,28 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return err } } + + // t.SixtyThreeBitIntegerWithASignBit (int64) (int64) + if len("SixtyThreeBitIntegerWithASignBit") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"SixtyThreeBitIntegerWithASignBit\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SixtyThreeBitIntegerWithASignBit"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("SixtyThreeBitIntegerWithASignBit")); err != nil { + return err + } + + if t.SixtyThreeBitIntegerWithASignBit >= 0 { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SixtyThreeBitIntegerWithASignBit)); err != nil { + return err + } + } else { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.SixtyThreeBitIntegerWithASignBit-1)); err != nil { + return err + } + } return nil } @@ -256,80 +256,17 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { } switch name { - // t.Stuff (testing.SimpleTypeTree) (struct) - case "Stuff": - - { - - b, err := cr.ReadByte() - if err != nil { - return err - } - if b != cbg.CborNull[0] { - if err := cr.UnreadByte(); err != nil { - return err - } - t.Stuff = new(SimpleTypeTree) - if err := t.Stuff.UnmarshalCBOR(cr); err != nil { - return xerrors.Errorf("unmarshaling t.Stuff pointer: %w", err) - } - } - - } - // t.Stufff (testing.SimpleTypeTwo) (struct) - case "Stufff": + // t.Dog (string) (string) + case "Dog": { - - b, err := cr.ReadByte() + sval, err := cbg.ReadString(cr) if err != nil { return err } - if b != cbg.CborNull[0] { - if err := cr.UnreadByte(); err != nil { - return err - } - t.Stufff = new(SimpleTypeTwo) - if err := t.Stufff.UnmarshalCBOR(cr); err != nil { - return xerrors.Errorf("unmarshaling t.Stufff pointer: %w", err) - } - } - - } - // t.Others ([]uint64) (slice) - case "Others": - - maj, extra, err = cr.ReadHeader() - if err != nil { - return err - } - - if extra > cbg.MaxLength { - return fmt.Errorf("t.Others: array too large (%d)", extra) - } - - if maj != cbg.MajArray { - return fmt.Errorf("expected cbor array") - } - - if extra > 0 { - t.Others = make([]uint64, extra) - } - - for i := 0; i < int(extra); i++ { - - maj, val, err := cr.ReadHeader() - if err != nil { - return xerrors.Errorf("failed to read uint64 for t.Others slice: %w", err) - } - if maj != cbg.MajUnsignedInt { - return xerrors.Errorf("value read for array t.Others was not a uint, instead got %d", maj) - } - - t.Others[i] = uint64(val) + t.Dog = string(sval) } - // t.Test ([][]uint8) (slice) case "Test": @@ -378,45 +315,8 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { } } - // t.Dog (string) (string) - case "Dog": - - { - sval, err := cbg.ReadString(cr) - if err != nil { - return err - } - - t.Dog = string(sval) - } - // t.SixtyThreeBitIntegerWithASignBit (int64) (int64) - case "SixtyThreeBitIntegerWithASignBit": - { - maj, extra, err := cr.ReadHeader() - var extraI int64 - 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 oveflow") - } - extraI = -1 - extraI - default: - return fmt.Errorf("wrong type for int64 field: %d", maj) - } - - t.SixtyThreeBitIntegerWithASignBit = int64(extraI) - } - // t.NotPizza (uint64) (uint64) - case "NotPizza": + // t.Stuff (testing.SimpleTypeTree) (struct) + case "Stuff": { @@ -428,24 +328,98 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { if err := cr.UnreadByte(); err != nil { return err } - maj, extra, err = cr.ReadHeader() - if err != nil { - return err - } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") + t.Stuff = new(SimpleTypeTree) + if err := t.Stuff.UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling t.Stuff pointer: %w", err) } - typed := uint64(extra) - t.NotPizza = &typed } } - // t.StringPtr (string) (string) - case "StringPtr": + // t.Others ([]uint64) (slice) + case "Others": - { - b, err := cr.ReadByte() - if err != nil { + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } + + if extra > cbg.MaxLength { + return fmt.Errorf("t.Others: array too large (%d)", extra) + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + + if extra > 0 { + t.Others = make([]uint64, extra) + } + + for i := 0; i < int(extra); i++ { + + maj, val, err := cr.ReadHeader() + if err != nil { + return xerrors.Errorf("failed to read uint64 for t.Others slice: %w", err) + } + + if maj != cbg.MajUnsignedInt { + return xerrors.Errorf("value read for array t.Others was not a uint, instead got %d", maj) + } + + t.Others[i] = uint64(val) + } + + // t.Stufff (testing.SimpleTypeTwo) (struct) + case "Stufff": + + { + + b, err := cr.ReadByte() + if err != nil { + return err + } + if b != cbg.CborNull[0] { + if err := cr.UnreadByte(); err != nil { + return err + } + t.Stufff = new(SimpleTypeTwo) + if err := t.Stufff.UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling t.Stufff pointer: %w", err) + } + } + + } + // t.NotPizza (uint64) (uint64) + case "NotPizza": + + { + + b, err := cr.ReadByte() + if err != nil { + return err + } + if b != cbg.CborNull[0] { + if err := cr.UnreadByte(); err != nil { + return err + } + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } + if maj != cbg.MajUnsignedInt { + return fmt.Errorf("wrong type for uint64 field") + } + typed := uint64(extra) + t.NotPizza = &typed + } + + } + // t.StringPtr (string) (string) + case "StringPtr": + + { + b, err := cr.ReadByte() + if err != nil { return err } if b != cbg.CborNull[0] { @@ -461,6 +435,32 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { t.StringPtr = (*string)(&sval) } } + // t.SixtyThreeBitIntegerWithASignBit (int64) (int64) + case "SixtyThreeBitIntegerWithASignBit": + { + maj, extra, err := cr.ReadHeader() + var extraI int64 + 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 oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.SixtyThreeBitIntegerWithASignBit = int64(extraI) + } default: // Field doesn't exist on this type, so ignore it @@ -577,51 +577,51 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return err } - // t.OldStr (string) (string) - if len("OldStr") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldStr\" was too long") + // t.OldMap (map[string]testing.SimpleTypeOne) (map) + if len("OldMap") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldMap\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldMap"))); err != nil { return err } - if _, err := io.WriteString(w, string("OldStr")); err != nil { + if _, err := io.WriteString(w, string("OldMap")); err != nil { return err } - if len(t.OldStr) > cbg.MaxLength { - return xerrors.Errorf("Value in field t.OldStr was too long") - } + { + if len(t.OldMap) > 4096 { + return xerrors.Errorf("cannot marshal t.OldMap map too large") + } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.OldStr))); err != nil { - return err - } - if _, err := io.WriteString(w, string(t.OldStr)); err != nil { - return err - } + if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.OldMap))); err != nil { + return err + } - // t.OldBytes ([]uint8) (slice) - if len("OldBytes") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldBytes\" was too long") - } + keys := make([]string, 0, len(t.OldMap)) + for k := range t.OldMap { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + v := t.OldMap[k] - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldBytes"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("OldBytes")); err != nil { - return err - } + if len(k) > cbg.MaxLength { + return xerrors.Errorf("Value in field k was too long") + } - if len(t.OldBytes) > cbg.ByteArrayMaxLen { - return xerrors.Errorf("Byte array in field t.OldBytes was too long") - } + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { + return err + } + if _, err := io.WriteString(w, string(k)); err != nil { + return err + } - if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { - return err - } + if err := v.MarshalCBOR(cw); err != nil { + return err + } - if _, err := cw.Write(t.OldBytes[:]); err != nil { - return err + } } // t.OldNum (uint64) (uint64) @@ -662,51 +662,27 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } } - // t.OldMap (map[string]testing.SimpleTypeOne) (map) - if len("OldMap") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldMap\" was too long") + // t.OldStr (string) (string) + if len("OldStr") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldStr\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldMap"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStr"))); err != nil { return err } - if _, err := io.WriteString(w, string("OldMap")); err != nil { + if _, err := io.WriteString(w, string("OldStr")); err != nil { return err } - { - if len(t.OldMap) > 4096 { - return xerrors.Errorf("cannot marshal t.OldMap map too large") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.OldMap))); err != nil { - return err - } - - keys := make([]string, 0, len(t.OldMap)) - for k := range t.OldMap { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - v := t.OldMap[k] - - if len(k) > cbg.MaxLength { - return xerrors.Errorf("Value in field k was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { - return err - } - if _, err := io.WriteString(w, string(k)); err != nil { - return err - } - - if err := v.MarshalCBOR(cw); err != nil { - return err - } + if len(t.OldStr) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.OldStr was too long") + } - } + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.OldStr))); err != nil { + return err + } + if _, err := io.WriteString(w, string(t.OldStr)); err != nil { + return err } // t.OldArray ([]testing.SimpleTypeOne) (slice) @@ -734,6 +710,30 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } } + // t.OldBytes ([]uint8) (slice) + if len("OldBytes") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldBytes\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldBytes"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("OldBytes")); err != nil { + return err + } + + if len(t.OldBytes) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("Byte array in field t.OldBytes was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { + return err + } + + if _, err := cw.Write(t.OldBytes[:]); err != nil { + return err + } + // t.OldStruct (testing.SimpleTypeOne) (struct) if len("OldStruct") > cbg.MaxLength { return xerrors.Errorf("Value in field \"OldStruct\" was too long") @@ -790,38 +790,47 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { } switch name { - // t.OldStr (string) (string) - case "OldStr": - - { - sval, err := cbg.ReadString(cr) - if err != nil { - return err - } - - t.OldStr = string(sval) - } - // t.OldBytes ([]uint8) (slice) - case "OldBytes": + // t.OldMap (map[string]testing.SimpleTypeOne) (map) + case "OldMap": maj, extra, err = cr.ReadHeader() if err != nil { return err } - - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.OldBytes: byte array too large (%d)", extra) + if maj != cbg.MajMap { + return fmt.Errorf("expected a map (major type 5)") } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") + if extra > 4096 { + return fmt.Errorf("t.OldMap: map too large") } - if extra > 0 { - t.OldBytes = make([]uint8, extra) - } + t.OldMap = make(map[string]SimpleTypeOne, extra) + + for i, l := 0, int(extra); i < l; i++ { + + var k string + + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } + + k = string(sval) + } + + var v SimpleTypeOne + + { + + if err := v.UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling v: %w", err) + } + + } + + t.OldMap[k] = v - if _, err := io.ReadFull(cr, t.OldBytes[:]); err != nil { - return err } // t.OldNum (uint64) (uint64) case "OldNum": @@ -861,47 +870,16 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { } } - // t.OldMap (map[string]testing.SimpleTypeOne) (map) - case "OldMap": - - maj, extra, err = cr.ReadHeader() - if err != nil { - return err - } - if maj != cbg.MajMap { - return fmt.Errorf("expected a map (major type 5)") - } - if extra > 4096 { - return fmt.Errorf("t.OldMap: map too large") - } - - t.OldMap = make(map[string]SimpleTypeOne, extra) - - for i, l := 0, int(extra); i < l; i++ { - - var k string - - { - sval, err := cbg.ReadString(cr) - if err != nil { - return err - } - - k = string(sval) - } - - var v SimpleTypeOne - - { - - if err := v.UnmarshalCBOR(cr); err != nil { - return xerrors.Errorf("unmarshaling v: %w", err) - } + // t.OldStr (string) (string) + case "OldStr": + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err } - t.OldMap[k] = v - + t.OldStr = string(sval) } // t.OldArray ([]testing.SimpleTypeOne) (slice) case "OldArray": @@ -933,6 +911,28 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { t.OldArray[i] = v } + // t.OldBytes ([]uint8) (slice) + case "OldBytes": + + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } + + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("t.OldBytes: byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + + if extra > 0 { + t.OldBytes = make([]uint8, extra) + } + + if _, err := io.ReadFull(cr, t.OldBytes[:]); err != nil { + return err + } // t.OldStruct (testing.SimpleTypeOne) (struct) case "OldStruct": @@ -964,28 +964,90 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - // t.OldStr (string) (string) - if len("OldStr") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldStr\" was too long") + // t.NewMap (map[string]testing.SimpleTypeOne) (map) + if len("NewMap") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"NewMap\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewMap"))); err != nil { return err } - if _, err := io.WriteString(w, string("OldStr")); err != nil { + if _, err := io.WriteString(w, string("NewMap")); err != nil { return err } - if len(t.OldStr) > cbg.MaxLength { - return xerrors.Errorf("Value in field t.OldStr was too long") + { + if len(t.NewMap) > 4096 { + return xerrors.Errorf("cannot marshal t.NewMap map too large") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.NewMap))); err != nil { + return err + } + + keys := make([]string, 0, len(t.NewMap)) + for k := range t.NewMap { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + v := t.NewMap[k] + + if len(k) > cbg.MaxLength { + return xerrors.Errorf("Value in field k was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { + return err + } + if _, err := io.WriteString(w, string(k)); err != nil { + return err + } + + if err := v.MarshalCBOR(cw); err != nil { + return err + } + + } } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.OldStr))); err != nil { + // t.NewNum (uint64) (uint64) + if len("NewNum") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"NewNum\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewNum"))); err != nil { return err } - if _, err := io.WriteString(w, string(t.OldStr)); err != nil { + if _, err := io.WriteString(w, string("NewNum")); err != nil { + return err + } + + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.NewNum)); err != nil { + return err + } + + // t.NewPtr (cid.Cid) (struct) + if len("NewPtr") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"NewPtr\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewPtr"))); err != nil { return err } + if _, err := io.WriteString(w, string("NewPtr")); err != nil { + return err + } + + if t.NewPtr == nil { + if _, err := cw.Write(cbg.CborNull); err != nil { + return err + } + } else { + if err := cbg.WriteCid(cw, *t.NewPtr); err != nil { + return xerrors.Errorf("failed to write cid field t.NewPtr: %w", err) + } + } // t.NewStr (string) (string) if len("NewStr") > cbg.MaxLength { @@ -1010,52 +1072,51 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - // t.OldBytes ([]uint8) (slice) - if len("OldBytes") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldBytes\" was too long") + // t.OldMap (map[string]testing.SimpleTypeOne) (map) + if len("OldMap") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldMap\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldBytes"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldMap"))); err != nil { return err } - if _, err := io.WriteString(w, string("OldBytes")); err != nil { + if _, err := io.WriteString(w, string("OldMap")); err != nil { return err } - if len(t.OldBytes) > cbg.ByteArrayMaxLen { - return xerrors.Errorf("Byte array in field t.OldBytes was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { - return err - } + { + if len(t.OldMap) > 4096 { + return xerrors.Errorf("cannot marshal t.OldMap map too large") + } - if _, err := cw.Write(t.OldBytes[:]); err != nil { - return err - } + if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.OldMap))); err != nil { + return err + } - // t.NewBytes ([]uint8) (slice) - if len("NewBytes") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"NewBytes\" was too long") - } + keys := make([]string, 0, len(t.OldMap)) + for k := range t.OldMap { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + v := t.OldMap[k] - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewBytes"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("NewBytes")); err != nil { - return err - } + if len(k) > cbg.MaxLength { + return xerrors.Errorf("Value in field k was too long") + } - if len(t.NewBytes) > cbg.ByteArrayMaxLen { - return xerrors.Errorf("Byte array in field t.NewBytes was too long") - } + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { + return err + } + if _, err := io.WriteString(w, string(k)); err != nil { + return err + } - if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.NewBytes))); err != nil { - return err - } + if err := v.MarshalCBOR(cw); err != nil { + return err + } - if _, err := cw.Write(t.NewBytes[:]); err != nil { - return err + } } // t.OldNum (uint64) (uint64) @@ -1074,22 +1135,6 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return err } - // t.NewNum (uint64) (uint64) - if len("NewNum") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"NewNum\" was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewNum"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("NewNum")); err != nil { - return err - } - - if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.NewNum)); err != nil { - return err - } - // t.OldPtr (cid.Cid) (struct) if len("OldPtr") > cbg.MaxLength { return xerrors.Errorf("Value in field \"OldPtr\" was too long") @@ -1112,120 +1157,76 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } } - // t.NewPtr (cid.Cid) (struct) - if len("NewPtr") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"NewPtr\" was too long") + // t.OldStr (string) (string) + if len("OldStr") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldStr\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewPtr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStr"))); err != nil { return err } - if _, err := io.WriteString(w, string("NewPtr")); err != nil { + if _, err := io.WriteString(w, string("OldStr")); err != nil { return err } - if t.NewPtr == nil { - if _, err := cw.Write(cbg.CborNull); err != nil { - return err - } - } else { - if err := cbg.WriteCid(cw, *t.NewPtr); err != nil { - return xerrors.Errorf("failed to write cid field t.NewPtr: %w", err) - } + if len(t.OldStr) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.OldStr was too long") } - // t.OldMap (map[string]testing.SimpleTypeOne) (map) - if len("OldMap") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldMap\" was too long") + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.OldStr))); err != nil { + return err + } + if _, err := io.WriteString(w, string(t.OldStr)); err != nil { + return err } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldMap"))); err != nil { + // t.NewArray ([]testing.SimpleTypeOne) (slice) + if len("NewArray") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"NewArray\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewArray"))); err != nil { return err } - if _, err := io.WriteString(w, string("OldMap")); err != nil { + if _, err := io.WriteString(w, string("NewArray")); err != nil { return err } - { - if len(t.OldMap) > 4096 { - return xerrors.Errorf("cannot marshal t.OldMap map too large") - } + if len(t.NewArray) > cbg.MaxLength { + return xerrors.Errorf("Slice value in field t.NewArray was too long") + } - if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.OldMap))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.NewArray))); err != nil { + return err + } + for _, v := range t.NewArray { + if err := v.MarshalCBOR(cw); err != nil { return err } - - keys := make([]string, 0, len(t.OldMap)) - for k := range t.OldMap { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - v := t.OldMap[k] - - if len(k) > cbg.MaxLength { - return xerrors.Errorf("Value in field k was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { - return err - } - if _, err := io.WriteString(w, string(k)); err != nil { - return err - } - - if err := v.MarshalCBOR(cw); err != nil { - return err - } - - } } - // t.NewMap (map[string]testing.SimpleTypeOne) (map) - if len("NewMap") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"NewMap\" was too long") + // t.NewBytes ([]uint8) (slice) + if len("NewBytes") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"NewBytes\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewMap"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewBytes"))); err != nil { return err } - if _, err := io.WriteString(w, string("NewMap")); err != nil { + if _, err := io.WriteString(w, string("NewBytes")); err != nil { return err } - { - if len(t.NewMap) > 4096 { - return xerrors.Errorf("cannot marshal t.NewMap map too large") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.NewMap))); err != nil { - return err - } - - keys := make([]string, 0, len(t.NewMap)) - for k := range t.NewMap { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - v := t.NewMap[k] - - if len(k) > cbg.MaxLength { - return xerrors.Errorf("Value in field k was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { - return err - } - if _, err := io.WriteString(w, string(k)); err != nil { - return err - } + if len(t.NewBytes) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("Byte array in field t.NewBytes was too long") + } - if err := v.MarshalCBOR(cw); err != nil { - return err - } + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.NewBytes))); err != nil { + return err + } - } + if _, err := cw.Write(t.NewBytes[:]); err != nil { + return err } // t.OldArray ([]testing.SimpleTypeOne) (slice) @@ -1253,60 +1254,59 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } } - // t.NewArray ([]testing.SimpleTypeOne) (slice) - if len("NewArray") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"NewArray\" was too long") + // t.OldBytes ([]uint8) (slice) + if len("OldBytes") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldBytes\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewArray"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldBytes"))); err != nil { return err } - if _, err := io.WriteString(w, string("NewArray")); err != nil { + if _, err := io.WriteString(w, string("OldBytes")); err != nil { return err } - if len(t.NewArray) > cbg.MaxLength { - return xerrors.Errorf("Slice value in field t.NewArray was too long") + if len(t.OldBytes) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("Byte array in field t.OldBytes was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.NewArray))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { return err } - for _, v := range t.NewArray { - if err := v.MarshalCBOR(cw); err != nil { - return err - } + + if _, err := cw.Write(t.OldBytes[:]); err != nil { + return err } - // t.OldStruct (testing.SimpleTypeOne) (struct) - if len("OldStruct") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"OldStruct\" was too long") + // t.NewStruct (testing.SimpleTypeOne) (struct) + if len("NewStruct") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"NewStruct\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStruct"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewStruct"))); err != nil { return err } - if _, err := io.WriteString(w, string("OldStruct")); err != nil { + if _, err := io.WriteString(w, string("NewStruct")); err != nil { return err } - if err := t.OldStruct.MarshalCBOR(cw); err != nil { + if err := t.NewStruct.MarshalCBOR(cw); err != nil { return err } - // t.NewStruct (testing.SimpleTypeOne) (struct) - if len("NewStruct") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"NewStruct\" was too long") + // t.OldStruct (testing.SimpleTypeOne) (struct) + if len("OldStruct") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"OldStruct\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewStruct"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStruct"))); err != nil { return err } - if _, err := io.WriteString(w, string("NewStruct")); err != nil { + if _, err := io.WriteString(w, string("OldStruct")); err != nil { return err } - if err := t.NewStruct.MarshalCBOR(cw); err != nil { + if err := t.OldStruct.MarshalCBOR(cw); err != nil { return err } return nil @@ -1350,85 +1350,46 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { } switch name { - // t.OldStr (string) (string) - case "OldStr": - - { - sval, err := cbg.ReadString(cr) - if err != nil { - return err - } - - t.OldStr = string(sval) - } - // t.NewStr (string) (string) - case "NewStr": - - { - sval, err := cbg.ReadString(cr) - if err != nil { - return err - } - - t.NewStr = string(sval) - } - // t.OldBytes ([]uint8) (slice) - case "OldBytes": + // t.NewMap (map[string]testing.SimpleTypeOne) (map) + case "NewMap": maj, extra, err = cr.ReadHeader() if err != nil { return err } - - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.OldBytes: byte array too large (%d)", extra) + if maj != cbg.MajMap { + return fmt.Errorf("expected a map (major type 5)") } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") + if extra > 4096 { + return fmt.Errorf("t.NewMap: map too large") } - if extra > 0 { - t.OldBytes = make([]uint8, extra) - } + t.NewMap = make(map[string]SimpleTypeOne, extra) - if _, err := io.ReadFull(cr, t.OldBytes[:]); err != nil { - return err - } - // t.NewBytes ([]uint8) (slice) - case "NewBytes": + for i, l := 0, int(extra); i < l; i++ { - maj, extra, err = cr.ReadHeader() - if err != nil { - return err - } + var k string - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.NewBytes: byte array too large (%d)", extra) - } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") - } + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } - if extra > 0 { - t.NewBytes = make([]uint8, extra) - } + k = string(sval) + } - if _, err := io.ReadFull(cr, t.NewBytes[:]); err != nil { - return err - } - // t.OldNum (uint64) (uint64) - case "OldNum": + var v SimpleTypeOne - { + { + + if err := v.UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling v: %w", err) + } - maj, extra, err = cr.ReadHeader() - if err != nil { - return err - } - if maj != cbg.MajUnsignedInt { - return fmt.Errorf("wrong type for uint64 field") } - t.OldNum = uint64(extra) + + t.NewMap[k] = v } // t.NewNum (uint64) (uint64) @@ -1446,8 +1407,8 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { t.NewNum = uint64(extra) } - // t.OldPtr (cid.Cid) (struct) - case "OldPtr": + // t.NewPtr (cid.Cid) (struct) + case "NewPtr": { @@ -1462,35 +1423,23 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { c, err := cbg.ReadCid(cr) if err != nil { - return xerrors.Errorf("failed to read cid field t.OldPtr: %w", err) + return xerrors.Errorf("failed to read cid field t.NewPtr: %w", err) } - t.OldPtr = &c + t.NewPtr = &c } } - // t.NewPtr (cid.Cid) (struct) - case "NewPtr": + // t.NewStr (string) (string) + case "NewStr": { - - b, err := cr.ReadByte() + sval, err := cbg.ReadString(cr) if err != nil { return err } - if b != cbg.CborNull[0] { - if err := cr.UnreadByte(); err != nil { - return err - } - - c, err := cbg.ReadCid(cr) - if err != nil { - return xerrors.Errorf("failed to read cid field t.NewPtr: %w", err) - } - - t.NewPtr = &c - } + t.NewStr = string(sval) } // t.OldMap (map[string]testing.SimpleTypeOne) (map) case "OldMap": @@ -1534,50 +1483,57 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { t.OldMap[k] = v } - // t.NewMap (map[string]testing.SimpleTypeOne) (map) - case "NewMap": + // t.OldNum (uint64) (uint64) + case "OldNum": - maj, extra, err = cr.ReadHeader() - if err != nil { - return err - } - if maj != cbg.MajMap { - return fmt.Errorf("expected a map (major type 5)") - } - if extra > 4096 { - return fmt.Errorf("t.NewMap: map too large") - } + { - t.NewMap = make(map[string]SimpleTypeOne, extra) + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } + if maj != cbg.MajUnsignedInt { + return fmt.Errorf("wrong type for uint64 field") + } + t.OldNum = uint64(extra) - for i, l := 0, int(extra); i < l; i++ { + } + // t.OldPtr (cid.Cid) (struct) + case "OldPtr": - var k string + { - { - sval, err := cbg.ReadString(cr) - if err != nil { + b, err := cr.ReadByte() + if err != nil { + return err + } + if b != cbg.CborNull[0] { + if err := cr.UnreadByte(); err != nil { return err } - k = string(sval) - } - - var v SimpleTypeOne - - { - - if err := v.UnmarshalCBOR(cr); err != nil { - return xerrors.Errorf("unmarshaling v: %w", err) + c, err := cbg.ReadCid(cr) + if err != nil { + return xerrors.Errorf("failed to read cid field t.OldPtr: %w", err) } + t.OldPtr = &c } - t.NewMap[k] = v + } + // t.OldStr (string) (string) + case "OldStr": + + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } + t.OldStr = string(sval) } - // t.OldArray ([]testing.SimpleTypeOne) (slice) - case "OldArray": + // t.NewArray ([]testing.SimpleTypeOne) (slice) + case "NewArray": maj, extra, err = cr.ReadHeader() if err != nil { @@ -1585,7 +1541,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { } if extra > cbg.MaxLength { - return fmt.Errorf("t.OldArray: array too large (%d)", extra) + return fmt.Errorf("t.NewArray: array too large (%d)", extra) } if maj != cbg.MajArray { @@ -1593,7 +1549,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { } if extra > 0 { - t.OldArray = make([]SimpleTypeOne, extra) + t.NewArray = make([]SimpleTypeOne, extra) } for i := 0; i < int(extra); i++ { @@ -1603,11 +1559,33 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { return err } - t.OldArray[i] = v + t.NewArray[i] = v } - // t.NewArray ([]testing.SimpleTypeOne) (slice) - case "NewArray": + // t.NewBytes ([]uint8) (slice) + case "NewBytes": + + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } + + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("t.NewBytes: byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + + if extra > 0 { + t.NewBytes = make([]uint8, extra) + } + + if _, err := io.ReadFull(cr, t.NewBytes[:]); err != nil { + return err + } + // t.OldArray ([]testing.SimpleTypeOne) (slice) + case "OldArray": maj, extra, err = cr.ReadHeader() if err != nil { @@ -1615,7 +1593,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { } if extra > cbg.MaxLength { - return fmt.Errorf("t.NewArray: array too large (%d)", extra) + return fmt.Errorf("t.OldArray: array too large (%d)", extra) } if maj != cbg.MajArray { @@ -1623,7 +1601,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { } if extra > 0 { - t.NewArray = make([]SimpleTypeOne, extra) + t.OldArray = make([]SimpleTypeOne, extra) } for i := 0; i < int(extra); i++ { @@ -1633,18 +1611,30 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { return err } - t.NewArray[i] = v + t.OldArray[i] = v } - // t.OldStruct (testing.SimpleTypeOne) (struct) - case "OldStruct": + // t.OldBytes ([]uint8) (slice) + case "OldBytes": - { + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } - if err := t.OldStruct.UnmarshalCBOR(cr); err != nil { - return xerrors.Errorf("unmarshaling t.OldStruct: %w", err) - } + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("t.OldBytes: byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + + if extra > 0 { + t.OldBytes = make([]uint8, extra) + } + if _, err := io.ReadFull(cr, t.OldBytes[:]); err != nil { + return err } // t.NewStruct (testing.SimpleTypeOne) (struct) case "NewStruct": @@ -1656,6 +1646,16 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { } } + // t.OldStruct (testing.SimpleTypeOne) (struct) + case "OldStruct": + + { + + if err := t.OldStruct.UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling t.OldStruct: %w", err) + } + + } default: // Field doesn't exist on this type, so ignore it @@ -1711,21 +1711,208 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { return err } - if len(t.Bar) > cbg.MaxLength { - return xerrors.Errorf("Value in field t.Bar was too long") - } + if len(t.Bar) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Bar was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Bar))); err != nil { + return err + } + if _, err := io.WriteString(w, string(t.Bar)); err != nil { + return err + } + return nil +} + +func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { + *t = RenamedFields{} + + cr := cbg.NewCborReader(r) + + maj, extra, err := cr.ReadHeader() + if err != nil { + return err + } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + + if maj != cbg.MajMap { + return fmt.Errorf("cbor input should be of type map") + } + + if extra > cbg.MaxLength { + return fmt.Errorf("RenamedFields: map struct too large (%d)", extra) + } + + var name string + n := extra + + for i := uint64(0); i < n; i++ { + + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } + + name = string(sval) + } + + switch name { + // t.Foo (int64) (int64) + case "foo": + { + maj, extra, err := cr.ReadHeader() + var extraI int64 + 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 oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.Foo = int64(extraI) + } + // t.Bar (string) (string) + case "beep": + + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } + + t.Bar = string(sval) + } + + default: + // Field doesn't exist on this type, so ignore it + cbg.ScanForLinks(r, func(cid.Cid) {}) + } + } + + return nil +} +func (t *TestEmpty) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + + cw := cbg.NewCborWriter(w) + fieldCount := 3 + + if t.Foo == nil { + fieldCount-- + } + + if t.Beep == "" { + fieldCount-- + } + + if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(fieldCount))); err != nil { + return err + } + + // t.Cat (int64) (int64) + if len("Cat") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Cat\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Cat"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("Cat")); err != nil { + return err + } + + if t.Cat >= 0 { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Cat)); err != nil { + return err + } + } else { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Cat-1)); err != nil { + return err + } + } + + // t.Foo (string) (string) + if t.Foo != nil { + + if len("Foo") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Foo\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Foo"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("Foo")); err != nil { + return err + } + + if t.Foo == nil { + if _, err := cw.Write(cbg.CborNull); err != nil { + return err + } + } else { + if len(*t.Foo) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Foo was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(*t.Foo))); err != nil { + return err + } + if _, err := io.WriteString(w, string(*t.Foo)); err != nil { + return err + } + } + } + + // t.Beep (string) (string) + if t.Beep != "" { + + if len("Beep") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Beep\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Beep"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("Beep")); err != nil { + return err + } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Bar))); err != nil { - return err - } - if _, err := io.WriteString(w, string(t.Bar)); err != nil { - return err + if len(t.Beep) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Beep was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Beep))); err != nil { + return err + } + if _, err := io.WriteString(w, string(t.Beep)); err != nil { + return err + } } return nil } -func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { - *t = RenamedFields{} +func (t *TestEmpty) UnmarshalCBOR(r io.Reader) (err error) { + *t = TestEmpty{} cr := cbg.NewCborReader(r) @@ -1744,7 +1931,7 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { } if extra > cbg.MaxLength { - return fmt.Errorf("RenamedFields: map struct too large (%d)", extra) + return fmt.Errorf("TestEmpty: map struct too large (%d)", extra) } var name string @@ -1762,8 +1949,8 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { } switch name { - // t.Foo (int64) (int64) - case "foo": + // t.Cat (int64) (int64) + case "Cat": { maj, extra, err := cr.ReadHeader() var extraI int64 @@ -1786,10 +1973,31 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { return fmt.Errorf("wrong type for int64 field: %d", maj) } - t.Foo = int64(extraI) + t.Cat = int64(extraI) } - // t.Bar (string) (string) - case "beep": + // t.Foo (string) (string) + case "Foo": + + { + b, err := cr.ReadByte() + if err != nil { + return err + } + if b != cbg.CborNull[0] { + if err := cr.UnreadByte(); err != nil { + return err + } + + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } + + t.Foo = (*string)(&sval) + } + } + // t.Beep (string) (string) + case "Beep": { sval, err := cbg.ReadString(cr) @@ -1797,7 +2005,7 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { return err } - t.Bar = string(sval) + t.Beep = string(sval) } default: @@ -1808,81 +2016,63 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { return nil } -func (t *TestEmpty) MarshalCBOR(w io.Writer) error { +func (t *TestConstField) MarshalCBOR(w io.Writer) error { if t == nil { _, err := w.Write(cbg.CborNull) return err } cw := cbg.NewCborWriter(w) - fieldCount := 2 - - if t.Foo == nil { - fieldCount-- - } - if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(fieldCount))); err != nil { + if _, err := cw.Write([]byte{162}); err != nil { return err } - // t.Foo (string) (string) - if t.Foo != nil { - - if len("Foo") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Foo\" was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Foo"))); err != nil { - return err - } - if _, err := io.WriteString(w, string("Foo")); err != nil { - return err - } + // t.Cats (string) (string) + if len("Cats") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Cats\" was too long") + } - if t.Foo == nil { - if _, err := cw.Write(cbg.CborNull); err != nil { - return err - } - } else { - if len(*t.Foo) > cbg.MaxLength { - return xerrors.Errorf("Value in field t.Foo was too long") - } + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Cats"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("Cats")); err != nil { + return err + } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(*t.Foo))); err != nil { - return err - } - if _, err := io.WriteString(w, string(*t.Foo)); err != nil { - return err - } - } + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("dogsdrool"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("dogsdrool")); err != nil { + return err } - // t.Cat (int64) (int64) - if len("Cat") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Cat\" was too long") + // t.Thing (int64) (int64) + if len("Thing") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Thing\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Cat"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Thing"))); err != nil { return err } - if _, err := io.WriteString(w, string("Cat")); err != nil { + if _, err := io.WriteString(w, string("Thing")); err != nil { return err } - if t.Cat >= 0 { - if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Cat)); err != nil { + if t.Thing >= 0 { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Thing)); err != nil { return err } } else { - if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Cat-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Thing-1)); err != nil { return err } } return nil } -func (t *TestEmpty) UnmarshalCBOR(r io.Reader) (err error) { - *t = TestEmpty{} +func (t *TestConstField) UnmarshalCBOR(r io.Reader) (err error) { + *t = TestConstField{} cr := cbg.NewCborReader(r) @@ -1901,7 +2091,7 @@ func (t *TestEmpty) UnmarshalCBOR(r io.Reader) (err error) { } if extra > cbg.MaxLength { - return fmt.Errorf("TestEmpty: map struct too large (%d)", extra) + return fmt.Errorf("TestConstField: map struct too large (%d)", extra) } var name string @@ -1919,29 +2109,19 @@ func (t *TestEmpty) UnmarshalCBOR(r io.Reader) (err error) { } switch name { - // t.Foo (string) (string) - case "Foo": + // t.Cats (string) (string) + case "Cats": { - b, err := cr.ReadByte() + sval, err := cbg.ReadString(cr) if err != nil { return err } - if b != cbg.CborNull[0] { - if err := cr.UnreadByte(); err != nil { - return err - } - - sval, err := cbg.ReadString(cr) - if err != nil { - return err - } - t.Foo = (*string)(&sval) - } + t.Cats = string(sval) } - // t.Cat (int64) (int64) - case "Cat": + // t.Thing (int64) (int64) + case "Thing": { maj, extra, err := cr.ReadHeader() var extraI int64 @@ -1964,7 +2144,7 @@ func (t *TestEmpty) UnmarshalCBOR(r io.Reader) (err error) { return fmt.Errorf("wrong type for int64 field: %d", maj) } - t.Cat = int64(extraI) + t.Thing = int64(extraI) } default: @@ -1975,7 +2155,7 @@ func (t *TestEmpty) UnmarshalCBOR(r io.Reader) (err error) { return nil } -func (t *TestConstField) MarshalCBOR(w io.Writer) error { +func (t *TestCanonicalFieldOrder) MarshalCBOR(w io.Writer) error { if t == nil { _, err := w.Write(cbg.CborNull) return err @@ -1983,55 +2163,104 @@ func (t *TestConstField) MarshalCBOR(w io.Writer) error { cw := cbg.NewCborWriter(w) - if _, err := cw.Write([]byte{162}); err != nil { + if _, err := cw.Write([]byte{164}); err != nil { return err } - // t.Cats (string) (string) - if len("Cats") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Cats\" was too long") + // t.Zp (string) (string) + if len("ap") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"ap\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Cats"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ap"))); err != nil { return err } - if _, err := io.WriteString(w, string("Cats")); err != nil { + if _, err := io.WriteString(w, string("ap")); err != nil { return err } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("dogsdrool"))); err != nil { + if len(t.Zp) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Zp was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Zp))); err != nil { return err } - if _, err := io.WriteString(w, string("dogsdrool")); err != nil { + if _, err := io.WriteString(w, string(t.Zp)); err != nil { return err } - // t.Thing (int64) (int64) - if len("Thing") > cbg.MaxLength { - return xerrors.Errorf("Value in field \"Thing\" was too long") + // t.Foo (int64) (int64) + if len("foo") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"foo\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Thing"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("foo"))); err != nil { return err } - if _, err := io.WriteString(w, string("Thing")); err != nil { + if _, err := io.WriteString(w, string("foo")); err != nil { return err } - if t.Thing >= 0 { - if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Thing)); err != nil { + if t.Foo >= 0 { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Foo)); err != nil { return err } } else { - if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Thing-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Foo-1)); err != nil { + return err + } + } + + // t.Bar (string) (string) + if len("beep") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"beep\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("beep"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("beep")); err != nil { + return err + } + + if len(t.Bar) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Bar was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Bar))); err != nil { + return err + } + if _, err := io.WriteString(w, string(t.Bar)); err != nil { + return err + } + + // t.Drond (int64) (int64) + if len("Drond") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Drond\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Drond"))); err != nil { + return err + } + if _, err := io.WriteString(w, string("Drond")); err != nil { + return err + } + + if t.Drond >= 0 { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Drond)); err != nil { + return err + } + } else { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Drond-1)); err != nil { return err } } return nil } -func (t *TestConstField) UnmarshalCBOR(r io.Reader) (err error) { - *t = TestConstField{} +func (t *TestCanonicalFieldOrder) UnmarshalCBOR(r io.Reader) (err error) { + *t = TestCanonicalFieldOrder{} cr := cbg.NewCborReader(r) @@ -2050,7 +2279,7 @@ func (t *TestConstField) UnmarshalCBOR(r io.Reader) (err error) { } if extra > cbg.MaxLength { - return fmt.Errorf("TestConstField: map struct too large (%d)", extra) + return fmt.Errorf("TestCanonicalFieldOrder: map struct too large (%d)", extra) } var name string @@ -2068,8 +2297,8 @@ func (t *TestConstField) UnmarshalCBOR(r io.Reader) (err error) { } switch name { - // t.Cats (string) (string) - case "Cats": + // t.Zp (string) (string) + case "ap": { sval, err := cbg.ReadString(cr) @@ -2077,10 +2306,10 @@ func (t *TestConstField) UnmarshalCBOR(r io.Reader) (err error) { return err } - t.Cats = string(sval) + t.Zp = string(sval) } - // t.Thing (int64) (int64) - case "Thing": + // t.Foo (int64) (int64) + case "foo": { maj, extra, err := cr.ReadHeader() var extraI int64 @@ -2103,7 +2332,44 @@ func (t *TestConstField) UnmarshalCBOR(r io.Reader) (err error) { return fmt.Errorf("wrong type for int64 field: %d", maj) } - t.Thing = int64(extraI) + t.Foo = int64(extraI) + } + // t.Bar (string) (string) + case "beep": + + { + sval, err := cbg.ReadString(cr) + if err != nil { + return err + } + + t.Bar = string(sval) + } + // t.Drond (int64) (int64) + case "Drond": + { + maj, extra, err := cr.ReadHeader() + var extraI int64 + 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 oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.Drond = int64(extraI) } default: diff --git a/testing/types.go b/testing/types.go index 51c6b7b..c16b83b 100644 --- a/testing/types.go +++ b/testing/types.go @@ -112,11 +112,19 @@ type BigField struct { } type TestEmpty struct { - Foo *string `cborgen:"omitempty"` - Cat int64 + Foo *string `cborgen:"omitempty"` + Beep string `cborgen:"omitempty"` + Cat int64 } type TestConstField struct { Cats string `cborgen:"const=dogsdrool"` Thing int64 } + +type TestCanonicalFieldOrder struct { + Foo int64 `cborgen:"foo"` + Bar string `cborgen:"beep"` + Drond int64 + Zp string `cborgen:"ap"` +}