-
Notifications
You must be signed in to change notification settings - Fork 205
entproto: Add proto3 optional label support #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,7 @@ const ( | |||||
| var ( | ||||||
| ErrSchemaSkipped = errors.New("entproto: schema not annotated with Generate=true") | ||||||
| repeatedFieldLabel = descriptorpb.FieldDescriptorProto_LABEL_REPEATED | ||||||
| optionalFieldLabel = descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL | ||||||
| wktsPaths = map[string]string{ | ||||||
| // TODO: handle more Well-Known proto types | ||||||
| "google.protobuf.Timestamp": "google/protobuf/timestamp.proto", | ||||||
|
|
@@ -325,8 +326,7 @@ func (a *Adapter) toProtoMessageDescriptor(genType *gen.Type) (*descriptorpb.Des | |||||
| if _, ok := f.Annotations[SkipAnnotation]; ok { | ||||||
| continue | ||||||
| } | ||||||
|
|
||||||
| protoField, err := toProtoFieldDescriptor(f) | ||||||
| protoField, err := toProtoFieldDescriptor(f, msgAnnot.EnableOptionalLabel) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
@@ -457,7 +457,7 @@ func toProtoEnumDescriptor(fld *gen.Field) (*descriptorpb.EnumDescriptorProto, e | |||||
| return dp, nil | ||||||
| } | ||||||
|
|
||||||
| func toProtoFieldDescriptor(f *gen.Field) (*descriptorpb.FieldDescriptorProto, error) { | ||||||
| func toProtoFieldDescriptor(f *gen.Field, enableOptionalLabel bool) (*descriptorpb.FieldDescriptorProto, error) { | ||||||
| fieldDesc := &descriptorpb.FieldDescriptorProto{ | ||||||
| Name: &f.Name, | ||||||
| } | ||||||
|
|
@@ -480,7 +480,7 @@ func toProtoFieldDescriptor(f *gen.Field) (*descriptorpb.FieldDescriptorProto, e | |||||
| } | ||||||
| return fieldDesc, nil | ||||||
| } | ||||||
| typeDetails, err := extractProtoTypeDetails(f) | ||||||
| typeDetails, err := extractProtoTypeDetails(f, enableOptionalLabel) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
@@ -490,19 +490,21 @@ func toProtoFieldDescriptor(f *gen.Field) (*descriptorpb.FieldDescriptorProto, e | |||||
| } | ||||||
| if typeDetails.repeated { | ||||||
| fieldDesc.Label = &repeatedFieldLabel | ||||||
| } else if typeDetails.optional { | ||||||
| fieldDesc.Label = &optionalFieldLabel | ||||||
| } | ||||||
| return fieldDesc, nil | ||||||
| } | ||||||
|
|
||||||
| func extractProtoTypeDetails(f *gen.Field) (fieldType, error) { | ||||||
| func extractProtoTypeDetails(f *gen.Field, enableOptionalLabel bool) (fieldType, error) { | ||||||
| if f.Type.Type == field.TypeJSON { | ||||||
| return extractJSONDetails(f) | ||||||
| return extractJSONDetails(f) // repeat fields are not required for optional label. | ||||||
|
||||||
| return extractJSONDetails(f) // repeat fields are not required for optional label. | |
| return extractJSONDetails(f) // JSON fields only support repeated (array) types, not optional labels. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected 'repeat' to 'repeated' for grammatical accuracy.