From 66625cee3e0243da19aed4a7e2177ad4c90449c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Fri, 13 Sep 2024 17:04:29 +0300 Subject: [PATCH 1/4] features/unmarshal: implement unmarshal_unique MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement unmarshal_unique that is like unmarshal but calls unique.Make() on each string. The idea is to keep only one copy of a string around with gRPC marshaling/unmarshaling pools on. No idea how to test this in unit tests but this change works in our project. Signed-off-by: Giedrius Statkevičius --- README.md | 2 ++ features/unmarshal/unmarshal.go | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 96adb7b..3a34818 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ The following features can be generated: - `unmarshal_unsafe` generates a `func (p *YourProto) UnmarshalVTUnsafe(data []byte)` that behaves like `UnmarshalVT`, except it unsafely casts slices of data to `bytes` and `string` fields instead of copying them to newly allocated arrays, so that it performs less allocations. **Data received from the wire has to be left untouched for the lifetime of the message.** Otherwise, the message's `bytes` and `string` fields can be corrupted. +- `unmarshal_unique` is like `unmarshal` but it calls `unique.Make()` on each string that interns it i.e. if there are multiple copies of the same string coming through gRPC, only one copy if it is stored in memory. + - `pool`: generates the following helper methods - `func (p *YourProto) ResetVT()`: this function behaves similarly to `proto.Reset(p)`, except it keeps as much memory as possible available on the message, so that further calls to `UnmarshalVT` on the same message will need to allocate less memory. This an API meant to be used with memory pools and does not need to be used directly. diff --git a/features/unmarshal/unmarshal.go b/features/unmarshal/unmarshal.go index 79e929b..d77aa23 100644 --- a/features/unmarshal/unmarshal.go +++ b/features/unmarshal/unmarshal.go @@ -25,12 +25,17 @@ func init() { generator.RegisterFeature("unmarshal_unsafe", func(gen *generator.GeneratedFile) generator.FeatureGenerator { return &unmarshal{GeneratedFile: gen, unsafe: true} }) + + generator.RegisterFeature("unmarshal_unique", func(gen *generator.GeneratedFile) generator.FeatureGenerator { + return &unmarshal{GeneratedFile: gen, unique: true} + }) } type unmarshal struct { *generator.GeneratedFile unsafe bool once bool + unique bool } var _ generator.FeatureGenerator = (*unmarshal)(nil) @@ -193,13 +198,16 @@ func (p *unmarshal) mapField(varName string, field *protogen.Field) { p.P(`if postStringIndex`, varName, ` > l {`) p.P(`return `, p.Ident("io", `ErrUnexpectedEOF`)) p.P(`}`) - if p.unsafe { + switch { + case p.unsafe: p.P(`if intStringLen`, varName, ` == 0 {`) p.P(varName, ` = ""`) p.P(`} else {`) p.P(varName, ` = `, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen`, varName, `)`) p.P(`}`) - } else { + case p.unique: + p.P(varName, ` = `, "unique.Make[string](string", `(dAtA[iNdEx:postStringIndex`, varName, `])).Value()`) + default: p.P(varName, ` = `, "string", `(dAtA[iNdEx:postStringIndex`, varName, `])`) } p.P(`iNdEx = postStringIndex`, varName) @@ -423,12 +431,15 @@ func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message * p.P(`return `, p.Ident("io", `ErrUnexpectedEOF`)) p.P(`}`) str := "string(dAtA[iNdEx:postIndex])" - if p.unsafe { + switch { + case p.unsafe: str = "stringValue" p.P(`var stringValue string`) p.P(`if intStringLen > 0 {`) p.P(`stringValue = `, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen)`) p.P(`}`) + case p.unique: + str = "unique.Make[string](string(dAtA[iNdEx:postIndex])).Value()" } if oneof { p.P(`m.`, fieldname, ` = &`, field.GoIdent, `{`, field.GoName, ": ", str, `}`) From 35ae8c4cfdb524f755366ac9d206d13e9c3a3eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Mon, 16 Sep 2024 12:15:42 +0300 Subject: [PATCH 2/4] unmarshal: update after PR review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert the option into a field option. Update README.md. Signed-off-by: Giedrius Statkevičius --- README.md | 16 ++++++- features/unmarshal/unmarshal.go | 34 +++++++++++---- .../planetscale/vtprotobuf/vtproto/ext.proto | 10 +++++ vtproto/ext.pb.go | 42 ++++++++++++++----- 4 files changed, 80 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3a34818..1030c05 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,6 @@ The following features can be generated: - `unmarshal_unsafe` generates a `func (p *YourProto) UnmarshalVTUnsafe(data []byte)` that behaves like `UnmarshalVT`, except it unsafely casts slices of data to `bytes` and `string` fields instead of copying them to newly allocated arrays, so that it performs less allocations. **Data received from the wire has to be left untouched for the lifetime of the message.** Otherwise, the message's `bytes` and `string` fields can be corrupted. -- `unmarshal_unique` is like `unmarshal` but it calls `unique.Make()` on each string that interns it i.e. if there are multiple copies of the same string coming through gRPC, only one copy if it is stored in memory. - - `pool`: generates the following helper methods - `func (p *YourProto) ResetVT()`: this function behaves similarly to `proto.Reset(p)`, except it keeps as much memory as possible available on the message, so that further calls to `UnmarshalVT` on the same message will need to allocate less memory. This an API meant to be used with memory pools and does not need to be used directly. @@ -55,6 +53,20 @@ The following features can be generated: - `func (p *YourProto) CloneMessageVT() proto.Message`: this function behaves like the above `p.CloneVT()`, but provides a uniform signature in order to be accessible via type assertions even if the type is not known at compile time. This allows implementing a generic `func CloneVT(proto.Message)` without reflection. If the receiver `p` is `nil`, a typed `nil` pointer of the message type will be returned inside a `proto.Message` interface. +### Field Options + +- `unique` is a field option available on strings. If it is set to `true` then all all strings are interned using [unique.Make](https://pkg.go.dev/unique#Make). Go 1.23+ is needed. `unmarshal_unsafe` takes precendence over `unique`. Example usage: + +``` +import "github.com/planetscale/vtprotobuf/vtproto/ext.proto"; + +message Label { + string name = 1 [(vtproto.options).unique = true]; + string value = 2 [(vtproto.options).unique = true]; +} +``` + + ## Usage 1. Install `protoc-gen-go-vtproto`: diff --git a/features/unmarshal/unmarshal.go b/features/unmarshal/unmarshal.go index d77aa23..294dbe1 100644 --- a/features/unmarshal/unmarshal.go +++ b/features/unmarshal/unmarshal.go @@ -12,9 +12,11 @@ import ( "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/encoding/protowire" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" "github.com/planetscale/vtprotobuf/generator" + "github.com/planetscale/vtprotobuf/vtproto" ) func init() { @@ -25,17 +27,12 @@ func init() { generator.RegisterFeature("unmarshal_unsafe", func(gen *generator.GeneratedFile) generator.FeatureGenerator { return &unmarshal{GeneratedFile: gen, unsafe: true} }) - - generator.RegisterFeature("unmarshal_unique", func(gen *generator.GeneratedFile) generator.FeatureGenerator { - return &unmarshal{GeneratedFile: gen, unique: true} - }) } type unmarshal struct { *generator.GeneratedFile unsafe bool once bool - unique bool } var _ generator.FeatureGenerator = (*unmarshal)(nil) @@ -161,6 +158,11 @@ func (p *unmarshal) declareMapField(varName string, nullable bool, field *protog } func (p *unmarshal) mapField(varName string, field *protogen.Field) { + var unique bool + if _, ok := proto.GetExtension(field.Desc.Options(), vtproto.E_Unique).(bool); ok { + unique = true + } + switch field.Desc.Kind() { case protoreflect.DoubleKind: p.P(`var `, varName, `temp uint64`) @@ -205,8 +207,12 @@ func (p *unmarshal) mapField(varName string, field *protogen.Field) { p.P(`} else {`) p.P(varName, ` = `, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen`, varName, `)`) p.P(`}`) - case p.unique: - p.P(varName, ` = `, "unique.Make[string](string", `(dAtA[iNdEx:postStringIndex`, varName, `])).Value()`) + case unique: + p.P(`if intStringLen`, varName, ` == 0 {`) + p.P(varName, ` = ""`) + p.P(`} else {`) + p.P(varName, ` = unique.Make[string](`, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen`, varName, `)).Value()`) + p.P(`}`) default: p.P(varName, ` = `, "string", `(dAtA[iNdEx:postStringIndex`, varName, `])`) } @@ -283,11 +289,17 @@ func (p *unmarshal) noStarOrSliceType(field *protogen.Field) string { } func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message *protogen.Message, proto3 bool) { + var unique bool + repeated := field.Desc.Cardinality() == protoreflect.Repeated typ := p.noStarOrSliceType(field) oneof := field.Oneof != nil && !field.Oneof.Desc.IsSynthetic() nullable := field.Oneof != nil && field.Oneof.Desc.IsSynthetic() + if _, ok := proto.GetExtension(field.Desc.Options(), vtproto.E_Unique).(bool); ok { + unique = true + } + switch field.Desc.Kind() { case protoreflect.DoubleKind: p.P(`var v uint64`) @@ -438,8 +450,12 @@ func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message * p.P(`if intStringLen > 0 {`) p.P(`stringValue = `, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen)`) p.P(`}`) - case p.unique: - str = "unique.Make[string](string(dAtA[iNdEx:postIndex])).Value()" + case unique: + str = "stringValue" + p.P(`var stringValue string`) + p.P(`if intStringLen > 0 {`) + p.P(`stringValue = unique.Make[string](`, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen)).Value()`) + p.P(`}`) } if oneof { p.P(`m.`, fieldname, ` = &`, field.GoIdent, `{`, field.GoName, ": ", str, `}`) diff --git a/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto b/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto index 99099c5..5878452 100644 --- a/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto +++ b/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto @@ -9,4 +9,14 @@ option go_package = "github.com/planetscale/vtprotobuf/vtproto"; extend google.protobuf.MessageOptions { optional bool mempool = 64101; +} + +extend google.protobuf.FieldOptions { + optional FieldOptions options = 64102; +} + +// These options should be used during schema definition, +// applying them to some of the fields in protobuf +message FieldOptions { + optional bool unique = 1; } \ No newline at end of file diff --git a/vtproto/ext.pb.go b/vtproto/ext.pb.go index 98c14a6..dd05f4b 100644 --- a/vtproto/ext.pb.go +++ b/vtproto/ext.pb.go @@ -29,6 +29,14 @@ var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes = []protoi Tag: "varint,64101,opt,name=mempool", Filename: "github.com/planetscale/vtprotobuf/vtproto/ext.proto", }, + { + ExtendedType: (*descriptorpb.FieldOptions)(nil), + ExtensionType: (*bool)(nil), + Field: 64102, + Name: "vtproto.unique", + Tag: "varint,64102,opt,name=unique", + Filename: "github.com/planetscale/vtprotobuf/vtproto/ext.proto", + }, } // Extension fields to descriptorpb.MessageOptions. @@ -37,6 +45,12 @@ var ( E_Mempool = &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes[0] ) +// Extension fields to descriptorpb.FieldOptions. +var ( + // optional bool unique = 64102; + E_Unique = &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes[1] +) + var File_github_com_planetscale_vtprotobuf_vtproto_ext_proto protoreflect.FileDescriptor var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc = []byte{ @@ -49,23 +63,29 @@ var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc = []byte{ 0x3a, 0x3b, 0x0a, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x12, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xe5, 0xf4, 0x03, - 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x42, 0x49, 0x0a, - 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x75, 0x66, 0x42, 0x07, 0x56, 0x54, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x5a, 0x29, 0x67, - 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x6c, 0x61, 0x6e, 0x65, 0x74, - 0x73, 0x63, 0x61, 0x6c, 0x65, 0x2f, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, - 0x2f, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x3a, 0x37, 0x0a, + 0x06, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xe6, 0xf4, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, + 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x42, 0x49, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x07, 0x56, + 0x54, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x70, 0x6c, 0x61, 0x6e, 0x65, 0x74, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x2f, 0x76, + 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, + 0x6f, } var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_goTypes = []interface{}{ (*descriptorpb.MessageOptions)(nil), // 0: google.protobuf.MessageOptions + (*descriptorpb.FieldOptions)(nil), // 1: google.protobuf.FieldOptions } var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_depIdxs = []int32{ 0, // 0: vtproto.mempool:extendee -> google.protobuf.MessageOptions - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 0, // [0:1] is the sub-list for extension extendee + 1, // 1: vtproto.unique:extendee -> google.protobuf.FieldOptions + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 0, // [0:2] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } @@ -81,7 +101,7 @@ func file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_init() { RawDescriptor: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc, NumEnums: 0, NumMessages: 0, - NumExtensions: 1, + NumExtensions: 2, NumServices: 0, }, GoTypes: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_goTypes, From ce20bda5a28bf03d97f011e727308f8d06803443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Mon, 16 Sep 2024 14:14:28 +0300 Subject: [PATCH 3/4] unmarshal: add tests for unique MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Giedrius Statkevičius --- features/unmarshal/unmarshal.go | 17 +- .../planetscale/vtprotobuf/vtproto/ext.proto | 4 +- testproto/unique/unique.pb.go | 147 ++++++++ testproto/unique/unique.proto | 8 + testproto/unique/unique_test.go | 25 ++ testproto/unique/unique_vtproto.pb.go | 327 ++++++++++++++++++ vtproto/ext.pb.go | 131 +++++-- 7 files changed, 621 insertions(+), 38 deletions(-) create mode 100644 testproto/unique/unique.pb.go create mode 100644 testproto/unique/unique.proto create mode 100644 testproto/unique/unique_test.go create mode 100644 testproto/unique/unique_vtproto.pb.go diff --git a/features/unmarshal/unmarshal.go b/features/unmarshal/unmarshal.go index 294dbe1..0f7bce0 100644 --- a/features/unmarshal/unmarshal.go +++ b/features/unmarshal/unmarshal.go @@ -158,10 +158,7 @@ func (p *unmarshal) declareMapField(varName string, nullable bool, field *protog } func (p *unmarshal) mapField(varName string, field *protogen.Field) { - var unique bool - if _, ok := proto.GetExtension(field.Desc.Options(), vtproto.E_Unique).(bool); ok { - unique = true - } + unique := proto.GetExtension(field.Desc.Options(), vtproto.E_Options).(*vtproto.Opts).GetUnique() switch field.Desc.Kind() { case protoreflect.DoubleKind: @@ -211,7 +208,7 @@ func (p *unmarshal) mapField(varName string, field *protogen.Field) { p.P(`if intStringLen`, varName, ` == 0 {`) p.P(varName, ` = ""`) p.P(`} else {`) - p.P(varName, ` = unique.Make[string](`, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen`, varName, `)).Value()`) + p.P(varName, ` = `, p.Ident("unique", `Make`), `[string](`, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen`, varName, `)).Value()`) p.P(`}`) default: p.P(varName, ` = `, "string", `(dAtA[iNdEx:postStringIndex`, varName, `])`) @@ -289,17 +286,11 @@ func (p *unmarshal) noStarOrSliceType(field *protogen.Field) string { } func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message *protogen.Message, proto3 bool) { - var unique bool - repeated := field.Desc.Cardinality() == protoreflect.Repeated typ := p.noStarOrSliceType(field) oneof := field.Oneof != nil && !field.Oneof.Desc.IsSynthetic() nullable := field.Oneof != nil && field.Oneof.Desc.IsSynthetic() - if _, ok := proto.GetExtension(field.Desc.Options(), vtproto.E_Unique).(bool); ok { - unique = true - } - switch field.Desc.Kind() { case protoreflect.DoubleKind: p.P(`var v uint64`) @@ -429,6 +420,8 @@ func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message * p.P(`m.`, fieldname, ` = &b`) } case protoreflect.StringKind: + unique := proto.GetExtension(field.Desc.Options(), vtproto.E_Options).(*vtproto.Opts).GetUnique() + p.P(`var stringLen uint64`) p.decodeVarint("stringLen", "uint64") p.P(`intStringLen := int(stringLen)`) @@ -454,7 +447,7 @@ func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message * str = "stringValue" p.P(`var stringValue string`) p.P(`if intStringLen > 0 {`) - p.P(`stringValue = unique.Make[string](`, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen)).Value()`) + p.P(`stringValue = `, p.Ident("unique", `Make`), `[string](`, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen)).Value()`) p.P(`}`) } if oneof { diff --git a/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto b/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto index 5878452..00c117c 100644 --- a/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto +++ b/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto @@ -12,11 +12,11 @@ extend google.protobuf.MessageOptions { } extend google.protobuf.FieldOptions { - optional FieldOptions options = 64102; + optional Opts options = 999999; } // These options should be used during schema definition, // applying them to some of the fields in protobuf -message FieldOptions { +message Opts { optional bool unique = 1; } \ No newline at end of file diff --git a/testproto/unique/unique.pb.go b/testproto/unique/unique.pb.go new file mode 100644 index 0000000..cc1fb68 --- /dev/null +++ b/testproto/unique/unique.pb.go @@ -0,0 +1,147 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v3.21.12 +// source: unique/unique.proto + +package unique + +import ( + _ "github.com/planetscale/vtprotobuf/vtproto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type UniqueFieldExtension struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Foo string `protobuf:"bytes,1,opt,name=foo,proto3" json:"foo,omitempty"` +} + +func (x *UniqueFieldExtension) Reset() { + *x = UniqueFieldExtension{} + if protoimpl.UnsafeEnabled { + mi := &file_unique_unique_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UniqueFieldExtension) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UniqueFieldExtension) ProtoMessage() {} + +func (x *UniqueFieldExtension) ProtoReflect() protoreflect.Message { + mi := &file_unique_unique_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UniqueFieldExtension.ProtoReflect.Descriptor instead. +func (*UniqueFieldExtension) Descriptor() ([]byte, []int) { + return file_unique_unique_proto_rawDescGZIP(), []int{0} +} + +func (x *UniqueFieldExtension) GetFoo() string { + if x != nil { + return x.Foo + } + return "" +} + +var File_unique_unique_proto protoreflect.FileDescriptor + +var file_unique_unique_proto_rawDesc = []byte{ + 0x0a, 0x13, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x2f, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x33, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x70, 0x6c, 0x61, 0x6e, 0x65, 0x74, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x2f, 0x76, 0x74, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2f, 0x65, 0x78, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x31, 0x0a, 0x14, 0x55, 0x6e, + 0x69, 0x71, 0x75, 0x65, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, + 0x6f, 0x6e, 0x12, 0x19, 0x0a, 0x03, 0x66, 0x6f, 0x6f, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, + 0x07, 0xfa, 0xa3, 0xe8, 0x03, 0x02, 0x08, 0x01, 0x52, 0x03, 0x66, 0x6f, 0x6f, 0x42, 0x12, 0x5a, + 0x10, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x75, 0x6e, 0x69, 0x71, 0x75, + 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_unique_unique_proto_rawDescOnce sync.Once + file_unique_unique_proto_rawDescData = file_unique_unique_proto_rawDesc +) + +func file_unique_unique_proto_rawDescGZIP() []byte { + file_unique_unique_proto_rawDescOnce.Do(func() { + file_unique_unique_proto_rawDescData = protoimpl.X.CompressGZIP(file_unique_unique_proto_rawDescData) + }) + return file_unique_unique_proto_rawDescData +} + +var file_unique_unique_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_unique_unique_proto_goTypes = []interface{}{ + (*UniqueFieldExtension)(nil), // 0: UniqueFieldExtension +} +var file_unique_unique_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_unique_unique_proto_init() } +func file_unique_unique_proto_init() { + if File_unique_unique_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_unique_unique_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UniqueFieldExtension); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_unique_unique_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_unique_unique_proto_goTypes, + DependencyIndexes: file_unique_unique_proto_depIdxs, + MessageInfos: file_unique_unique_proto_msgTypes, + }.Build() + File_unique_unique_proto = out.File + file_unique_unique_proto_rawDesc = nil + file_unique_unique_proto_goTypes = nil + file_unique_unique_proto_depIdxs = nil +} diff --git a/testproto/unique/unique.proto b/testproto/unique/unique.proto new file mode 100644 index 0000000..767023b --- /dev/null +++ b/testproto/unique/unique.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +option go_package = "testproto/unique"; + +import "github.com/planetscale/vtprotobuf/vtproto/ext.proto"; + +message UniqueFieldExtension { + string foo = 1 [(vtproto.options).unique = true]; +} diff --git a/testproto/unique/unique_test.go b/testproto/unique/unique_test.go new file mode 100644 index 0000000..bef4055 --- /dev/null +++ b/testproto/unique/unique_test.go @@ -0,0 +1,25 @@ +package unique + +import ( + "testing" + "unsafe" + + "github.com/stretchr/testify/require" +) + +func TestUnmarshalSameMemory(t *testing.T) { + m := &UniqueFieldExtension{ + Foo: "bar", + } + + b, err := m.MarshalVTStrict() + require.NoError(t, err) + + m2 := &UniqueFieldExtension{} + require.NoError(t, m2.UnmarshalVT(b)) + + m3 := &UniqueFieldExtension{} + require.NoError(t, m3.UnmarshalVT(b)) + + require.Equal(t, unsafe.StringData(m2.Foo), unsafe.StringData(m3.Foo)) +} diff --git a/testproto/unique/unique_vtproto.pb.go b/testproto/unique/unique_vtproto.pb.go new file mode 100644 index 0000000..d232bc8 --- /dev/null +++ b/testproto/unique/unique_vtproto.pb.go @@ -0,0 +1,327 @@ +// Code generated by protoc-gen-go-vtproto. DO NOT EDIT. +// protoc-gen-go-vtproto version: (devel) +// source: unique/unique.proto + +package unique + +import ( + fmt "fmt" + protohelpers "github.com/planetscale/vtprotobuf/protohelpers" + proto "google.golang.org/protobuf/proto" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + io "io" + unique "unique" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +func (m *UniqueFieldExtension) CloneVT() *UniqueFieldExtension { + if m == nil { + return (*UniqueFieldExtension)(nil) + } + r := new(UniqueFieldExtension) + r.Foo = m.Foo + if len(m.unknownFields) > 0 { + r.unknownFields = make([]byte, len(m.unknownFields)) + copy(r.unknownFields, m.unknownFields) + } + return r +} + +func (m *UniqueFieldExtension) CloneMessageVT() proto.Message { + return m.CloneVT() +} + +func (this *UniqueFieldExtension) EqualVT(that *UniqueFieldExtension) bool { + if this == that { + return true + } else if this == nil || that == nil { + return false + } + if this.Foo != that.Foo { + return false + } + return string(this.unknownFields) == string(that.unknownFields) +} + +func (this *UniqueFieldExtension) EqualMessageVT(thatMsg proto.Message) bool { + that, ok := thatMsg.(*UniqueFieldExtension) + if !ok { + return false + } + return this.EqualVT(that) +} +func (m *UniqueFieldExtension) MarshalVT() (dAtA []byte, err error) { + if m == nil { + return nil, nil + } + size := m.SizeVT() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBufferVT(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *UniqueFieldExtension) MarshalToVT(dAtA []byte) (int, error) { + size := m.SizeVT() + return m.MarshalToSizedBufferVT(dAtA[:size]) +} + +func (m *UniqueFieldExtension) MarshalToSizedBufferVT(dAtA []byte) (int, error) { + if m == nil { + return 0, nil + } + i := len(dAtA) + _ = i + var l int + _ = l + if m.unknownFields != nil { + i -= len(m.unknownFields) + copy(dAtA[i:], m.unknownFields) + } + if len(m.Foo) > 0 { + i -= len(m.Foo) + copy(dAtA[i:], m.Foo) + i = protohelpers.EncodeVarint(dAtA, i, uint64(len(m.Foo))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *UniqueFieldExtension) MarshalVTStrict() (dAtA []byte, err error) { + if m == nil { + return nil, nil + } + size := m.SizeVT() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBufferVTStrict(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *UniqueFieldExtension) MarshalToVTStrict(dAtA []byte) (int, error) { + size := m.SizeVT() + return m.MarshalToSizedBufferVTStrict(dAtA[:size]) +} + +func (m *UniqueFieldExtension) MarshalToSizedBufferVTStrict(dAtA []byte) (int, error) { + if m == nil { + return 0, nil + } + i := len(dAtA) + _ = i + var l int + _ = l + if m.unknownFields != nil { + i -= len(m.unknownFields) + copy(dAtA[i:], m.unknownFields) + } + if len(m.Foo) > 0 { + i -= len(m.Foo) + copy(dAtA[i:], m.Foo) + i = protohelpers.EncodeVarint(dAtA, i, uint64(len(m.Foo))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *UniqueFieldExtension) SizeVT() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Foo) + if l > 0 { + n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) + } + n += len(m.unknownFields) + return n +} + +func (m *UniqueFieldExtension) UnmarshalVT(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protohelpers.ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: UniqueFieldExtension: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: UniqueFieldExtension: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Foo", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protohelpers.ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return protohelpers.ErrInvalidLength + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return protohelpers.ErrInvalidLength + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + var stringValue string + if intStringLen > 0 { + stringValue = unique.Make[string](unsafe.String(&dAtA[iNdEx], intStringLen)).Value() + } + m.Foo = stringValue + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := protohelpers.Skip(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return protohelpers.ErrInvalidLength + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.unknownFields = append(m.unknownFields, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *UniqueFieldExtension) UnmarshalVTUnsafe(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protohelpers.ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: UniqueFieldExtension: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: UniqueFieldExtension: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Foo", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protohelpers.ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return protohelpers.ErrInvalidLength + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return protohelpers.ErrInvalidLength + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + var stringValue string + if intStringLen > 0 { + stringValue = unsafe.String(&dAtA[iNdEx], intStringLen) + } + m.Foo = stringValue + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := protohelpers.Skip(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return protohelpers.ErrInvalidLength + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + m.unknownFields = append(m.unknownFields, dAtA[iNdEx:iNdEx+skippy]...) + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} diff --git a/vtproto/ext.pb.go b/vtproto/ext.pb.go index dd05f4b..1b1908d 100644 --- a/vtproto/ext.pb.go +++ b/vtproto/ext.pb.go @@ -11,6 +11,7 @@ import ( protoimpl "google.golang.org/protobuf/runtime/protoimpl" descriptorpb "google.golang.org/protobuf/types/descriptorpb" reflect "reflect" + sync "sync" ) const ( @@ -20,6 +21,55 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +// These options should be used during schema definition, +// applying them to some of the fields in protobuf +type Opts struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Unique *bool `protobuf:"varint,1,opt,name=unique" json:"unique,omitempty"` +} + +func (x *Opts) Reset() { + *x = Opts{} + if protoimpl.UnsafeEnabled { + mi := &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Opts) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Opts) ProtoMessage() {} + +func (x *Opts) ProtoReflect() protoreflect.Message { + mi := &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Opts.ProtoReflect.Descriptor instead. +func (*Opts) Descriptor() ([]byte, []int) { + return file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescGZIP(), []int{0} +} + +func (x *Opts) GetUnique() bool { + if x != nil && x.Unique != nil { + return *x.Unique + } + return false +} + var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes = []protoimpl.ExtensionInfo{ { ExtendedType: (*descriptorpb.MessageOptions)(nil), @@ -31,10 +81,10 @@ var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes = []protoi }, { ExtendedType: (*descriptorpb.FieldOptions)(nil), - ExtensionType: (*bool)(nil), - Field: 64102, - Name: "vtproto.unique", - Tag: "varint,64102,opt,name=unique", + ExtensionType: (*Opts)(nil), + Field: 999999, + Name: "vtproto.options", + Tag: "bytes,999999,opt,name=options", Filename: "github.com/planetscale/vtprotobuf/vtproto/ext.proto", }, } @@ -47,8 +97,8 @@ var ( // Extension fields to descriptorpb.FieldOptions. var ( - // optional bool unique = 64102; - E_Unique = &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes[1] + // optional vtproto.Opts options = 999999; + E_Options = &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes[1] ) var File_github_com_planetscale_vtprotobuf_vtproto_ext_proto protoreflect.FileDescriptor @@ -60,31 +110,49 @@ var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc = []byte{ 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x22, 0x1e, 0x0a, 0x04, 0x4f, 0x70, 0x74, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x6e, 0x69, 0x71, + 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x3a, 0x3b, 0x0a, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x12, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xe5, 0xf4, 0x03, - 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x3a, 0x37, 0x0a, - 0x06, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, - 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xe6, 0xf4, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, - 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x42, 0x49, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x07, 0x56, - 0x54, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x70, 0x6c, 0x61, 0x6e, 0x65, 0x74, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x2f, 0x76, - 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, - 0x6f, + 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x3a, 0x48, 0x0a, + 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, + 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xbf, 0x84, 0x3d, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x0d, 0x2e, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x73, 0x52, 0x07, + 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x42, 0x49, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x07, + 0x56, 0x54, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x6c, 0x61, 0x6e, 0x65, 0x74, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x2f, + 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x76, 0x74, 0x70, 0x72, 0x6f, + 0x74, 0x6f, } +var ( + file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescOnce sync.Once + file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescData = file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc +) + +func file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescGZIP() []byte { + file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescOnce.Do(func() { + file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescData = protoimpl.X.CompressGZIP(file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescData) + }) + return file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDescData +} + +var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_goTypes = []interface{}{ - (*descriptorpb.MessageOptions)(nil), // 0: google.protobuf.MessageOptions - (*descriptorpb.FieldOptions)(nil), // 1: google.protobuf.FieldOptions + (*Opts)(nil), // 0: vtproto.Opts + (*descriptorpb.MessageOptions)(nil), // 1: google.protobuf.MessageOptions + (*descriptorpb.FieldOptions)(nil), // 2: google.protobuf.FieldOptions } var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_depIdxs = []int32{ - 0, // 0: vtproto.mempool:extendee -> google.protobuf.MessageOptions - 1, // 1: vtproto.unique:extendee -> google.protobuf.FieldOptions - 2, // [2:2] is the sub-list for method output_type - 2, // [2:2] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name + 1, // 0: vtproto.mempool:extendee -> google.protobuf.MessageOptions + 2, // 1: vtproto.options:extendee -> google.protobuf.FieldOptions + 0, // 2: vtproto.options:type_name -> vtproto.Opts + 3, // [3:3] is the sub-list for method output_type + 3, // [3:3] is the sub-list for method input_type + 2, // [2:3] is the sub-list for extension type_name 0, // [0:2] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } @@ -94,18 +162,33 @@ func file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_init() { if File_github_com_planetscale_vtprotobuf_vtproto_ext_proto != nil { return } + if !protoimpl.UnsafeEnabled { + file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Opts); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc, NumEnums: 0, - NumMessages: 0, + NumMessages: 1, NumExtensions: 2, NumServices: 0, }, GoTypes: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_goTypes, DependencyIndexes: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_depIdxs, + MessageInfos: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_msgTypes, ExtensionInfos: file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes, }.Build() File_github_com_planetscale_vtprotobuf_vtproto_ext_proto = out.File From 13b4bd5cd94ae011b0895b3bc587d28b41c22f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Tue, 17 Sep 2024 10:23:11 +0300 Subject: [PATCH 4/4] vtproto: update unique field option number MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Giedrius Statkevičius --- .../github.com/planetscale/vtprotobuf/vtproto/ext.proto | 2 +- vtproto/ext.pb.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto b/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto index 00c117c..6c1c628 100644 --- a/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto +++ b/include/github.com/planetscale/vtprotobuf/vtproto/ext.proto @@ -12,7 +12,7 @@ extend google.protobuf.MessageOptions { } extend google.protobuf.FieldOptions { - optional Opts options = 999999; + optional Opts options = 64150; } // These options should be used during schema definition, diff --git a/vtproto/ext.pb.go b/vtproto/ext.pb.go index 1b1908d..816fee1 100644 --- a/vtproto/ext.pb.go +++ b/vtproto/ext.pb.go @@ -82,9 +82,9 @@ var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes = []protoi { ExtendedType: (*descriptorpb.FieldOptions)(nil), ExtensionType: (*Opts)(nil), - Field: 999999, + Field: 64150, Name: "vtproto.options", - Tag: "bytes,999999,opt,name=options", + Tag: "bytes,64150,opt,name=options", Filename: "github.com/planetscale/vtprotobuf/vtproto/ext.proto", }, } @@ -97,7 +97,7 @@ var ( // Extension fields to descriptorpb.FieldOptions. var ( - // optional vtproto.Opts options = 999999; + // optional vtproto.Opts options = 64150; E_Options = &file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_extTypes[1] ) @@ -118,7 +118,7 @@ var file_github_com_planetscale_vtprotobuf_vtproto_ext_proto_rawDesc = []byte{ 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x3a, 0x48, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, - 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xbf, 0x84, 0x3d, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x96, 0xf5, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x76, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x73, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x42, 0x49, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x07,