-
Notifications
You must be signed in to change notification settings - Fork 69
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
V2: Introduce DescMessage.field #839
Conversation
@@ -543,6 +543,7 @@ function addFields( | |||
const oneof = findOneof(proto, allOneofs); | |||
const field = newField(proto, message, reg, oneof, mapEntries); | |||
message.fields.push(field); | |||
message.field[field.localName] = field; |
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.
We're eagerly populating the record - it's quite simple and cheap.
} from "@bufbuild/protobuf/wkt"; | ||
|
||
/** | ||
* Get comments on the package element in the protobuf source. | ||
*/ | ||
export function getPackageComments(desc: DescFile): DescComments { | ||
return findComments(desc.proto.sourceCodeInfo, [ | ||
FieldNumber.FileDescriptorProto_Package, | ||
FileDescriptorProtoDesc.field.package.number, |
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.
That's a nice practical example of where this feature can be useful. I imagine it will also be useful to construct FieldMasks.
field: DescField, | ||
): boolean { | ||
const field = messageDesc.fields.find((f) => f.localName === fieldName); | ||
if (field) { | ||
return unsafeIsSet(message, field); | ||
} | ||
return false; | ||
return ( | ||
field.parent.typeName == message.$typeName && unsafeIsSet(message, field) | ||
); |
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.
Same as before, a foreign field is never set, and clearing a foreign field is a no-op.
In some situations, it's useful to be able to refer to a field by name.
DescMessage
provides all fields in the propertyfields: DescField[]
- that's convenient to loop through all fields, but it's awkward to pick a field by name.This PR adds another property to
DescMessage
:In generated code, the property is a type-safe record. You get autocomplete for field names, and a typo is a compile-time error:
This PR also changes the signatures of
isFieldSet
andclearField
to make use of this new feature. Instead of having to pass in the message descriptor, the message, and the field name, both functions only require two arguments now: The message, and the field: