Skip to content

Commit

Permalink
Replaces isNullable with isOptional.
Browse files Browse the repository at this point in the history
Signed-off-by: Johyn Papin <contact@johyn.me>
  • Loading branch information
Johyn Papin committed Jul 9, 2023
1 parent 4bd55db commit 2024979
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions protoc_plugin/lib/src/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class MessageGenerator extends ProtobufContainer {
final defaultExpr = field.getDefaultExpr();
final names = field.memberNames;

if (useNullable && field.isNullable) {
if (useNullable && field.isOptional) {
fieldTypeString += '?';
}

Expand All @@ -511,7 +511,7 @@ class MessageGenerator extends ProtobufContainer {
defaultExpr,
field.isRepeated,
field.isMapField,
useNullable && field.isNullable);
useNullable && field.isOptional);
out.printlnAnnotated(
'$fieldTypeString get ${names!.fieldName} => $getterExpr;', [
NamedLocation(
Expand Down Expand Up @@ -539,7 +539,7 @@ class MessageGenerator extends ProtobufContainer {
_emitOverrideIf(field.overridesSetter, out);
_emitIndexAnnotation(field.number, out);
if (fastSetter != null) {
if (useNullable && field.isNullable) {
if (useNullable && field.isOptional) {
fastSetter += 'Nullable';
}
out.printlnAnnotated(
Expand All @@ -555,7 +555,7 @@ class MessageGenerator extends ProtobufContainer {
]);
} else {
final setterName =
useNullable && field.isNullable ? 'setFieldNullable' : 'setField';
useNullable && field.isOptional ? 'setFieldNullable' : 'setField';

out.printlnAnnotated(
'set ${names.fieldName}'
Expand Down
12 changes: 6 additions & 6 deletions protoc_plugin/lib/src/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class ProtobufField {
bool get isRepeated =>
descriptor.label == FieldDescriptorProto_Label.LABEL_REPEATED;

bool get isOptional {
if (isRepeated) return false;
if (isRequired || !descriptor.proto3Optional) return false;
return true;
}

/// Whether a numeric field is repeated and must be encoded with packed
/// encoding.
///
Expand Down Expand Up @@ -140,12 +146,6 @@ class ProtobufField {
// for example in package:protobuf/src/protobuf/mixins/well_known.dart.
}

bool get isNullable {
if (isRepeated) return false;
if (isRequired) return false;
return descriptor.proto3Optional || baseType.isMessage;
}

/// Returns the expression to use for the Dart type.
String getDartType() {
if (isMapField) {
Expand Down

0 comments on commit 2024979

Please sign in to comment.