Skip to content
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

Found and fixed a bug in FieldDescriptor.Cardinality implementation #270

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions linker/descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,12 @@ func (f *fldDescriptor) Cardinality() protoreflect.Cardinality {
case descriptorpb.FieldDescriptorProto_LABEL_REQUIRED:
return protoreflect.Required
case descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL:
if f.Syntax() == protoreflect.Editions {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment that explaining that FieldDescriptorProto.label is always set to LABEL_OPTIONAL for editions.

fieldPresence := descriptorpb.FeatureSet_FieldPresence(resolveFeature(f, fieldPresenceField).Enum())
if fieldPresence == descriptorpb.FeatureSet_LEGACY_REQUIRED {
return protoreflect.Required
}
}
return protoreflect.Optional
default:
return 0
Expand Down
5 changes: 5 additions & 0 deletions linker/descriptors_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,16 @@ func checkAttributesInFields(t *testing.T, exp, actual protoreflect.ExtensionDes
if !assert.Equal(t, expFld.Name(), actFld.Name(), "%s: field name at index %d", where, i) {
continue
}
assert.Equal(t, expFld.Number(), actFld.Number(), "%s: field number at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.Cardinality(), actFld.Cardinality(), "%s: field cardinality at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.Kind(), actFld.Kind(), "%s: field kind at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.IsList(), actFld.IsList(), "%s: field is list at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.IsMap(), actFld.IsMap(), "%s: field is map at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.JSONName(), actFld.JSONName(), "%s: field json name at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.HasJSONName(), actFld.HasJSONName(), "%s: field has json name at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.IsExtension(), actFld.IsExtension(), "%s: field is extension at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.IsPacked(), actFld.IsPacked(), "%s: field is packed at index %d (%s)", where, i, expFld.Name())
assert.Equal(t, expFld.ContainingOneof() == nil, actFld.ContainingOneof() == nil, "%s: field containing oneof at index %d (%s)", where, i, expFld.Name())

// default values

Expand Down
Loading