From 8dc3cbd44d34ecbd605b184257933a13edc3f413 Mon Sep 17 00:00:00 2001 From: Johyn Papin Date: Sun, 7 May 2023 15:16:16 +0200 Subject: [PATCH] Replaces isNullable with isOptional. Signed-off-by: Johyn Papin --- protoc_plugin/lib/src/message_generator.dart | 8 ++++---- protoc_plugin/lib/src/protobuf_field.dart | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/protoc_plugin/lib/src/message_generator.dart b/protoc_plugin/lib/src/message_generator.dart index 3b1fc43b3..3b895a73e 100644 --- a/protoc_plugin/lib/src/message_generator.dart +++ b/protoc_plugin/lib/src/message_generator.dart @@ -490,7 +490,7 @@ class MessageGenerator extends ProtobufContainer { var defaultExpr = field.getDefaultExpr(); var names = field.memberNames; - if (useNullable && field.isNullable) { + if (useNullable && field.isOptional) { fieldTypeString += '?'; } @@ -503,7 +503,7 @@ class MessageGenerator extends ProtobufContainer { defaultExpr, field.isRepeated, field.isMapField, - useNullable && field.isNullable); + useNullable && field.isOptional); out.printlnAnnotated( '$fieldTypeString get ${names!.fieldName} => $getterExpr;', [ NamedLocation( @@ -531,7 +531,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( @@ -547,7 +547,7 @@ class MessageGenerator extends ProtobufContainer { ]); } else { final setterName = - useNullable && field.isNullable ? 'setFieldNullable' : 'setField'; + useNullable && field.isOptional ? 'setFieldNullable' : 'setField'; out.printlnAnnotated( 'set ${names.fieldName}' diff --git a/protoc_plugin/lib/src/protobuf_field.dart b/protoc_plugin/lib/src/protobuf_field.dart index 4410ca06d..11f1fb067 100644 --- a/protoc_plugin/lib/src/protobuf_field.dart +++ b/protoc_plugin/lib/src/protobuf_field.dart @@ -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. /// @@ -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) {