diff --git a/encoding/protojson/decode.go b/encoding/protojson/decode.go index 1d22d3a40..5e32d8b48 100644 --- a/encoding/protojson/decode.go +++ b/encoding/protojson/decode.go @@ -139,7 +139,7 @@ func (o UnmarshalOptions) unmarshalMessage(m pref.Message, skipTypeURL bool) err // unmarshalFields unmarshals the fields into the given protoreflect.Message. func (o UnmarshalOptions) unmarshalFields(m pref.Message, skipTypeURL bool) error { messageDesc := m.Descriptor() - if !flags.Proto1Legacy && messageset.IsMessageSet(messageDesc) { + if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { return errors.New("no support for proto1 MessageSets") } diff --git a/encoding/protojson/decode_test.go b/encoding/protojson/decode_test.go index 9c4f93e61..c34310423 100644 --- a/encoding/protojson/decode_test.go +++ b/encoding/protojson/decode_test.go @@ -1334,7 +1334,7 @@ func TestUnmarshal(t *testing.T) { }) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 1", inputMessage: &pb2.FakeMessageSet{}, @@ -1350,7 +1350,7 @@ func TestUnmarshal(t *testing.T) { }) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 2", inputMessage: &pb2.FakeMessageSet{}, @@ -1360,7 +1360,7 @@ func TestUnmarshal(t *testing.T) { } }`, wantErr: true, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 3", inputMessage: &pb2.MessageSet{}, @@ -1376,7 +1376,7 @@ func TestUnmarshal(t *testing.T) { }) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "Empty", inputMessage: &emptypb.Empty{}, @@ -2457,13 +2457,13 @@ func TestUnmarshal(t *testing.T) { m.SetWeakMessage1(&weakpb.WeakImportMessage1{A: proto.Int32(1)}) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "weak fields; unknown field", inputMessage: &testpb.TestWeak{}, inputText: `{"weak_message1":{"a":1}, "weak_message2":{"a":1}}`, wantErr: true, // weak_message2 is unknown since the package containing it is not imported - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }} for _, tt := range tests { diff --git a/encoding/protojson/encode.go b/encoding/protojson/encode.go index 9b81eca52..839c7d1df 100644 --- a/encoding/protojson/encode.go +++ b/encoding/protojson/encode.go @@ -88,7 +88,7 @@ func (o MarshalOptions) marshalMessage(m pref.Message) error { // marshalFields marshals the fields in the given protoreflect.Message. func (o MarshalOptions) marshalFields(m pref.Message) error { messageDesc := m.Descriptor() - if !flags.Proto1Legacy && messageset.IsMessageSet(messageDesc) { + if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { return errors.New("no support for proto1 MessageSets") } diff --git a/encoding/protojson/encode_test.go b/encoding/protojson/encode_test.go index 5cacf9ecd..9d7a8ae64 100644 --- a/encoding/protojson/encode_test.go +++ b/encoding/protojson/encode_test.go @@ -1034,7 +1034,7 @@ func TestMarshal(t *testing.T) { "optString": "not a messageset extension" } }`, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 1", input: func() proto.Message { @@ -1049,7 +1049,7 @@ func TestMarshal(t *testing.T) { "optString": "not a messageset extension" } }`, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 2", input: func() proto.Message { @@ -1064,7 +1064,7 @@ func TestMarshal(t *testing.T) { "optString": "another not a messageset extension" } }`, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "BoolValue empty", input: &wrapperspb.BoolValue{}, diff --git a/encoding/prototext/decode.go b/encoding/prototext/decode.go index 06388c81e..7207dde26 100644 --- a/encoding/prototext/decode.go +++ b/encoding/prototext/decode.go @@ -76,7 +76,7 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error { // unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message. func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error { messageDesc := m.Descriptor() - if !flags.Proto1Legacy && messageset.IsMessageSet(messageDesc) { + if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { return errors.New("no support for proto1 MessageSets") } diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go index 588d9ee79..636e21b22 100644 --- a/encoding/prototext/decode_test.go +++ b/encoding/prototext/decode_test.go @@ -1310,7 +1310,7 @@ opt_int32: 42 }) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 1", inputMessage: &pb2.FakeMessageSet{}, @@ -1326,7 +1326,7 @@ opt_int32: 42 }) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 2", inputMessage: &pb2.FakeMessageSet{}, @@ -1336,7 +1336,7 @@ opt_int32: 42 } `, wantErr: true, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 3", inputMessage: &pb2.MessageSet{}, @@ -1351,7 +1351,7 @@ opt_int32: 42 }) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "Any not expanded", inputMessage: &anypb.Any{}, @@ -1501,13 +1501,13 @@ type_url: "pb2.Nested" m.SetWeakMessage1(&weakpb.WeakImportMessage1{A: proto.Int32(1)}) return m }(), - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "weak fields; unknown field", inputMessage: &testpb.TestWeak{}, inputText: `weak_message1:{a:1} weak_message2:{a:1}`, wantErr: true, // weak_message2 is unknown since the package containing it is not imported - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }} for _, tt := range tests { diff --git a/encoding/prototext/encode.go b/encoding/prototext/encode.go index 5d8cd4621..b732f029f 100644 --- a/encoding/prototext/encode.go +++ b/encoding/prototext/encode.go @@ -75,7 +75,7 @@ func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) { // marshalMessage converts a protoreflect.Message to a text.Value. func (o MarshalOptions) marshalMessage(m pref.Message) (text.Value, error) { messageDesc := m.Descriptor() - if !flags.Proto1Legacy && messageset.IsMessageSet(messageDesc) { + if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { return text.Value{}, errors.New("no support for proto1 MessageSets") } diff --git a/encoding/prototext/encode_test.go b/encoding/prototext/encode_test.go index 493a22961..0c480e601 100644 --- a/encoding/prototext/encode_test.go +++ b/encoding/prototext/encode_test.go @@ -1078,7 +1078,7 @@ opt_int32: 42 opt_string: "not a messageset extension" } `, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 1", input: func() proto.Message { @@ -1092,7 +1092,7 @@ opt_int32: 42 opt_string: "not a messageset extension" } `, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "not real MessageSet 2", input: func() proto.Message { @@ -1106,7 +1106,7 @@ opt_int32: 42 opt_string: "another not a messageset extension" } `, - skip: !flags.Proto1Legacy, + skip: !flags.ProtoLegacy, }, { desc: "Any not expanded", mo: prototext.MarshalOptions{ diff --git a/integration_test.go b/integration_test.go index 3f722132b..cc06bd89b 100644 --- a/integration_test.go +++ b/integration_test.go @@ -48,8 +48,8 @@ func Test(t *testing.T) { if *regenerate { t.Run("Generate", func(t *testing.T) { - fmt.Print(mustRunCommand(t, "go", "run", "-tags", "proto1_legacy", "./internal/cmd/generate-types", "-execute")) - fmt.Print(mustRunCommand(t, "go", "run", "-tags", "proto1_legacy", "./internal/cmd/generate-protos", "-execute")) + fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types", "-execute")) + fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos", "-execute")) files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n") mustRunCommand(t, append([]string{"gofmt", "-w"}, files...)...) }) @@ -79,7 +79,7 @@ func Test(t *testing.T) { runGo("PureGo", workDir, "go", "test", "-race", "-tags", "purego", "./...") runGo("Reflect", workDir, "go", "test", "-race", "-tags", "protoreflect", "./...") if goVersion == golangLatest { - runGo("Proto1Legacy", workDir, "go", "test", "-race", "-tags", "proto1_legacy", "./...") + runGo("ProtoLegacy", workDir, "go", "test", "-race", "-tags", "protolegacy", "./...") runGo("ProtocGenGo", "cmd/protoc-gen-go/testdata", "go", "test") runGo("ProtocGenGoGRPC", "cmd/protoc-gen-go-grpc/testdata", "go", "test") } @@ -94,11 +94,11 @@ func Test(t *testing.T) { mustRunCommand(t, runner, "--failure_list", failureList, "--enforce_recommended", driver) }) t.Run("GeneratedGoFiles", func(t *testing.T) { - diff := mustRunCommand(t, "go", "run", "-tags", "proto1_legacy", "./internal/cmd/generate-types") + diff := mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types") if strings.TrimSpace(diff) != "" { t.Fatalf("stale generated files:\n%v", diff) } - diff = mustRunCommand(t, "go", "run", "-tags", "proto1_legacy", "./internal/cmd/generate-protos") + diff = mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos") if strings.TrimSpace(diff) != "" { t.Fatalf("stale generated files:\n%v", diff) } diff --git a/internal/encoding/text/text_test.go b/internal/encoding/text/text_test.go index 0e40fa8f6..64771f3e4 100644 --- a/internal/encoding/text/text_test.go +++ b/internal/encoding/text/text_test.go @@ -615,7 +615,7 @@ func Test(t *testing.T) { }, { in: `nums: [0xbeefbeef,0xbeefbeefbeefbeef]`, wantVal: V(Msg{{ID("nums"), func() Value { - if flags.Proto1Legacy { + if flags.ProtoLegacy { return V(Lst{V(int32(-1091584273)), V(int64(-4688318750159552785))}) } else { return V(Lst{V(uint32(0xbeefbeef)), V(uint64(0xbeefbeefbeefbeef))}) diff --git a/internal/encoding/text/value.go b/internal/encoding/text/value.go index b0925986c..f98dbcb04 100644 --- a/internal/encoding/text/value.go +++ b/internal/encoding/text/value.go @@ -192,7 +192,7 @@ func (v Value) Int(b64 bool) (x int64, ok bool) { } // C++ accepts large positive hex numbers as negative values. // This feature is here for proto1 backwards compatibility purposes. - if flags.Proto1Legacy && len(v.raw) > 1 && v.raw[0] == '0' && v.raw[1] == 'x' { + if flags.ProtoLegacy && len(v.raw) > 1 && v.raw[0] == '0' && v.raw[1] == 'x' { if !b64 { return int64(int32(n)), n <= math.MaxUint32 } diff --git a/internal/encoding/wire/wire.go b/internal/encoding/wire/wire.go index 224ddf737..6c4fd5a11 100644 --- a/internal/encoding/wire/wire.go +++ b/internal/encoding/wire/wire.go @@ -494,7 +494,7 @@ func SizeGroup(num Number, n int) int { // Other than overflow, this does not check for field number validity. func DecodeTag(x uint64) (Number, Type) { // NOTE: MessageSet allows for larger field numbers than normal. - if flags.Proto1Legacy { + if flags.ProtoLegacy { if x>>3 > uint64(math.MaxInt32) { return -1, 0 } diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 8208a6d1b..89b0aff9d 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -5,12 +5,13 @@ // Package flags provides a set of flags controlled by build tags. package flags -// Proto1Legacy specifies whether to enable support for legacy proto1 -// functionality such as MessageSets, weak fields, and various other obscure -// behavior that is necessary to maintain backwards compatibility with proto1. +// ProtoLegacy specifies whether to enable support for legacy functionality +// such as MessageSets, weak fields, and various other obscure behavior +// that is necessary to maintain backwards compatibility with proto1 or +// the pre-release variants of proto2 and proto3. // -// This is disabled by default unless built with the "proto1_legacy" tag. +// This is disabled by default unless built with the "protolegacy" tag. // // WARNING: The compatibility agreement covers nothing provided by this flag. // As such, functionality may suddenly be removed or changed at our discretion. -const Proto1Legacy = proto1Legacy +const ProtoLegacy = protoLegacy diff --git a/internal/flags/proto1_legacy_enable.go b/internal/flags/proto_legacy_disable.go similarity index 78% rename from internal/flags/proto1_legacy_enable.go rename to internal/flags/proto_legacy_disable.go index 520d5374a..de110ca87 100644 --- a/internal/flags/proto1_legacy_enable.go +++ b/internal/flags/proto_legacy_disable.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE.md file. -// +build proto1_legacy +// +build !protolegacy package flags -const proto1Legacy = true +const protoLegacy = false diff --git a/internal/flags/proto1_legacy_disable.go b/internal/flags/proto_legacy_enable.go similarity index 77% rename from internal/flags/proto1_legacy_disable.go rename to internal/flags/proto_legacy_enable.go index 6bd2e8989..dbd2cd528 100644 --- a/internal/flags/proto1_legacy_disable.go +++ b/internal/flags/proto_legacy_enable.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE.md file. -// +build !proto1_legacy +// +build protolegacy package flags -const proto1Legacy = false +const protoLegacy = true diff --git a/internal/impl/codec_messageset.go b/internal/impl/codec_messageset.go index 073c4dc0d..d5fb44432 100644 --- a/internal/impl/codec_messageset.go +++ b/internal/impl/codec_messageset.go @@ -45,7 +45,7 @@ func sizeMessageSet(mi *MessageInfo, p pointer, tagsize int, opts marshalOptions } func marshalMessageSet(mi *MessageInfo, b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { - if !flags.Proto1Legacy { + if !flags.ProtoLegacy { return b, errors.New("no support for message_set_wire_format") } ext := *p.Extensions() @@ -97,7 +97,7 @@ func marshalMessageSetField(mi *MessageInfo, b []byte, x ExtensionField, opts ma } func unmarshalMessageSet(mi *MessageInfo, b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { - if !flags.Proto1Legacy { + if !flags.ProtoLegacy { return 0, errors.New("no support for message_set_wire_format") } if wtyp != wire.StartGroupType { diff --git a/internal/impl/message_field.go b/internal/impl/message_field.go index 7eb619948..10cacc7d9 100644 --- a/internal/impl/message_field.go +++ b/internal/impl/message_field.go @@ -288,7 +288,7 @@ func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField, x expor } func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldInfo { - if !flags.Proto1Legacy { + if !flags.ProtoLegacy { panic("no support for proto1 weak fields") } diff --git a/internal/strs/strings.go b/internal/strs/strings.go index af5f197cb..2208ff210 100644 --- a/internal/strs/strings.go +++ b/internal/strs/strings.go @@ -15,7 +15,7 @@ import ( // EnforceUTF8 reports whether to enforce strict UTF-8 validation. func EnforceUTF8(fd protoreflect.FieldDescriptor) bool { - if flags.Proto1Legacy { + if flags.ProtoLegacy { if fd, ok := fd.(interface{ EnforceUTF8() bool }); ok { return fd.EnforceUTF8() } diff --git a/proto/decode_test.go b/proto/decode_test.go index 89fc30320..c2b5ce966 100644 --- a/proto/decode_test.go +++ b/proto/decode_test.go @@ -98,9 +98,9 @@ func TestDecodeNoEnforceUTF8(t *testing.T) { got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) err := proto.Unmarshal(test.wire, got) switch { - case flags.Proto1Legacy && err != nil: + case flags.ProtoLegacy && err != nil: t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want)) - case !flags.Proto1Legacy && err == nil: + case !flags.ProtoLegacy && err == nil: t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want)) } }) diff --git a/proto/encode_test.go b/proto/encode_test.go index 573a197c7..466d3d14f 100644 --- a/proto/encode_test.go +++ b/proto/encode_test.go @@ -104,9 +104,9 @@ func TestEncodeNoEnforceUTF8(t *testing.T) { t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { _, err := proto.Marshal(want) switch { - case flags.Proto1Legacy && err != nil: + case flags.ProtoLegacy && err != nil: t.Errorf("Marshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want)) - case !flags.Proto1Legacy && err == nil: + case !flags.ProtoLegacy && err == nil: t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want)) } }) diff --git a/proto/messageset.go b/proto/messageset.go index e5d4bd5d1..14ed1991e 100644 --- a/proto/messageset.go +++ b/proto/messageset.go @@ -25,7 +25,7 @@ func sizeMessageSet(m protoreflect.Message) (size int) { } func marshalMessageSet(b []byte, m protoreflect.Message, o MarshalOptions) ([]byte, error) { - if !flags.Proto1Legacy { + if !flags.ProtoLegacy { return b, errors.New("no support for message_set_wire_format") } var err error @@ -53,7 +53,7 @@ func marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value pro } func unmarshalMessageSet(b []byte, m protoreflect.Message, o UnmarshalOptions) error { - if !flags.Proto1Legacy { + if !flags.ProtoLegacy { return errors.New("no support for message_set_wire_format") } md := m.Descriptor() diff --git a/proto/messageset_test.go b/proto/messageset_test.go index 07b2d70a4..c1ef6c94d 100644 --- a/proto/messageset_test.go +++ b/proto/messageset_test.go @@ -14,7 +14,7 @@ import ( ) func init() { - if flags.Proto1Legacy { + if flags.ProtoLegacy { testProtos = append(testProtos, messageSetTestProtos...) } } diff --git a/reflect/protodesc/desc_validate.go b/reflect/protodesc/desc_validate.go index 042950cc0..cf172a2f8 100644 --- a/reflect/protodesc/desc_validate.go +++ b/reflect/protodesc/desc_validate.go @@ -103,7 +103,7 @@ func validateMessageDeclarations(ms []filedesc.Message, mds []*descriptorpb.Desc return errors.New("message %q has conflicting fields: %q with %q", m.FullName(), f1.Name(), f2.Name()) } } - if isMessageSet && !flags.Proto1Legacy { + if isMessageSet && !flags.ProtoLegacy { return errors.New("message %q is a MessageSet, which is a legacy proto1 feature that is no longer supported", m.FullName()) } if isMessageSet && (m.Syntax() != protoreflect.Proto2 || m.Fields().Len() > 0 || m.ExtensionRanges().Len() == 0) { @@ -147,7 +147,7 @@ func validateMessageDeclarations(ms []filedesc.Message, mds []*descriptorpb.Desc if fd.Extendee != nil { return errors.New("message field %q may not have extendee: %q", f.FullName(), fd.GetExtendee()) } - if f.IsWeak() && !flags.Proto1Legacy { + if f.IsWeak() && !flags.ProtoLegacy { return errors.New("message field %q is a weak field, which is a legacy proto1 feature that is no longer supported", f.FullName()) } if f.IsWeak() && (f.Syntax() != protoreflect.Proto2 || !isOptionalMessage(f) || f.ContainingOneof() != nil) { diff --git a/reflect/protodesc/file_test.go b/reflect/protodesc/file_test.go index b20be3f7f..5319b5264 100644 --- a/reflect/protodesc/file_test.go +++ b/reflect/protodesc/file_test.go @@ -769,7 +769,7 @@ func TestNewFile(t *testing.T) { }]}] `), wantErr: func() string { - if flags.Proto1Legacy { + if flags.ProtoLegacy { return `message "M.M" is an invalid proto1 MessageSet` } else { return `message "M.M" is a MessageSet, which is a legacy proto1 feature that is no longer supported` @@ -787,7 +787,7 @@ func TestNewFile(t *testing.T) { }]}] `), wantErr: func() string { - if flags.Proto1Legacy { + if flags.ProtoLegacy { return "" } else { return `message "M.M" is a MessageSet, which is a legacy proto1 feature that is no longer supported` diff --git a/testing/prototest/prototest_test.go b/testing/prototest/prototest_test.go index 00df3a61c..5eec96100 100644 --- a/testing/prototest/prototest_test.go +++ b/testing/prototest/prototest_test.go @@ -32,7 +32,7 @@ func Test(t *testing.T) { (*legacypb.Legacy)(nil), protoimpl.X.MessageOf((*legacy1pb.Message)(nil)).Interface(), } - if flags.Proto1Legacy { + if flags.ProtoLegacy { ms = append(ms, (*testpb.TestWeak)(nil)) }