From a4e6e74dda865607b5d52608463cf17bd5fb757f Mon Sep 17 00:00:00 2001 From: Tomasz Krakowiak Date: Thu, 16 Dec 2021 22:08:53 +0100 Subject: [PATCH] Implement the new proto3 field presence feature #34 --- .../java/pbandk/testpb/Proto3Presence.java | 2615 +++++++++++++++++ .../kotlin/pbandk/gen/CodeGenerator.kt | 2 +- .../kotlin/pbandk/gen/FileBuilder.kt | 24 +- .../src/commonMain/kotlin/pbandk/gen/Main.kt | 105 +- .../src/jvmTest/kotlin/CodeGeneratorTest.kt | 18 + .../resources/protos/proto_3_presence.proto | 23 + .../kotlin/pbandk/testpb/proto_3_presence.kt | 271 ++ .../pbandk/testpb/proto_3_presence.proto | 26 + runtime/src/jvmTest/kotlin/TestUtils.kt | 25 + .../kotlin/pbandk/JvmProto3PresenceTest.kt | 59 + 10 files changed, 3115 insertions(+), 53 deletions(-) create mode 100644 jvm-test-types/src/main/java/pbandk/testpb/Proto3Presence.java create mode 100644 protoc-gen-pbandk/lib/src/jvmTest/resources/protos/proto_3_presence.proto create mode 100644 runtime/src/commonTest/kotlin/pbandk/testpb/proto_3_presence.kt create mode 100644 runtime/src/commonTest/proto/pbandk/testpb/proto_3_presence.proto create mode 100644 runtime/src/jvmTest/kotlin/TestUtils.kt create mode 100644 runtime/src/jvmTest/kotlin/pbandk/JvmProto3PresenceTest.kt diff --git a/jvm-test-types/src/main/java/pbandk/testpb/Proto3Presence.java b/jvm-test-types/src/main/java/pbandk/testpb/Proto3Presence.java new file mode 100644 index 00000000..be8a2713 --- /dev/null +++ b/jvm-test-types/src/main/java/pbandk/testpb/Proto3Presence.java @@ -0,0 +1,2615 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: pbandk/testpb/proto_3_presence.proto + +package pbandk.testpb; + +public final class Proto3Presence { + private Proto3Presence() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + /** + * Protobuf enum {@code pbandk.testpb.Proto3PresenceEnum} + */ + public enum Proto3PresenceEnum + implements com.google.protobuf.ProtocolMessageEnum { + /** + * PROTO3_PRESENCE_ENUM_UNSPECIFIED = 0; + */ + PROTO3_PRESENCE_ENUM_UNSPECIFIED(0), + /** + * PROTO3_PRESENCE_ENUM_SPECIFIED = 1; + */ + PROTO3_PRESENCE_ENUM_SPECIFIED(1), + UNRECOGNIZED(-1), + ; + + /** + * PROTO3_PRESENCE_ENUM_UNSPECIFIED = 0; + */ + public static final int PROTO3_PRESENCE_ENUM_UNSPECIFIED_VALUE = 0; + /** + * PROTO3_PRESENCE_ENUM_SPECIFIED = 1; + */ + public static final int PROTO3_PRESENCE_ENUM_SPECIFIED_VALUE = 1; + + + public final int getNumber() { + if (this == UNRECOGNIZED) { + throw new java.lang.IllegalArgumentException( + "Can't get the number of an unknown enum value."); + } + return value; + } + + /** + * @param value The numeric wire value of the corresponding enum entry. + * @return The enum associated with the given numeric wire value. + * @deprecated Use {@link #forNumber(int)} instead. + */ + @java.lang.Deprecated + public static Proto3PresenceEnum valueOf(int value) { + return forNumber(value); + } + + /** + * @param value The numeric wire value of the corresponding enum entry. + * @return The enum associated with the given numeric wire value. + */ + public static Proto3PresenceEnum forNumber(int value) { + switch (value) { + case 0: return PROTO3_PRESENCE_ENUM_UNSPECIFIED; + case 1: return PROTO3_PRESENCE_ENUM_SPECIFIED; + default: return null; + } + } + + public static com.google.protobuf.Internal.EnumLiteMap + internalGetValueMap() { + return internalValueMap; + } + private static final com.google.protobuf.Internal.EnumLiteMap< + Proto3PresenceEnum> internalValueMap = + new com.google.protobuf.Internal.EnumLiteMap() { + public Proto3PresenceEnum findValueByNumber(int number) { + return Proto3PresenceEnum.forNumber(number); + } + }; + + public final com.google.protobuf.Descriptors.EnumValueDescriptor + getValueDescriptor() { + if (this == UNRECOGNIZED) { + throw new java.lang.IllegalStateException( + "Can't get the descriptor of an unrecognized enum value."); + } + return getDescriptor().getValues().get(ordinal()); + } + public final com.google.protobuf.Descriptors.EnumDescriptor + getDescriptorForType() { + return getDescriptor(); + } + public static final com.google.protobuf.Descriptors.EnumDescriptor + getDescriptor() { + return pbandk.testpb.Proto3Presence.getDescriptor().getEnumTypes().get(0); + } + + private static final Proto3PresenceEnum[] VALUES = values(); + + public static Proto3PresenceEnum valueOf( + com.google.protobuf.Descriptors.EnumValueDescriptor desc) { + if (desc.getType() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "EnumValueDescriptor is not for this type."); + } + if (desc.getIndex() == -1) { + return UNRECOGNIZED; + } + return VALUES[desc.getIndex()]; + } + + private final int value; + + private Proto3PresenceEnum(int value) { + this.value = value; + } + + // @@protoc_insertion_point(enum_scope:pbandk.testpb.Proto3PresenceEnum) + } + + public interface Proto3PresenceMessageOrBuilder extends + // @@protoc_insertion_point(interface_extends:pbandk.testpb.Proto3PresenceMessage) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 requiredInt = 1; + * @return The requiredInt. + */ + int getRequiredInt(); + } + /** + * Protobuf type {@code pbandk.testpb.Proto3PresenceMessage} + */ + public static final class Proto3PresenceMessage extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:pbandk.testpb.Proto3PresenceMessage) + Proto3PresenceMessageOrBuilder { + private static final long serialVersionUID = 0L; + // Use Proto3PresenceMessage.newBuilder() to construct. + private Proto3PresenceMessage(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private Proto3PresenceMessage() { + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new Proto3PresenceMessage(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private Proto3PresenceMessage( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 8: { + + requiredInt_ = input.readInt32(); + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMessage_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMessage_fieldAccessorTable + .ensureFieldAccessorsInitialized( + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.class, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder.class); + } + + public static final int REQUIREDINT_FIELD_NUMBER = 1; + private int requiredInt_; + /** + * int32 requiredInt = 1; + * @return The requiredInt. + */ + @java.lang.Override + public int getRequiredInt() { + return requiredInt_; + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (requiredInt_ != 0) { + output.writeInt32(1, requiredInt_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (requiredInt_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, requiredInt_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof pbandk.testpb.Proto3Presence.Proto3PresenceMessage)) { + return super.equals(obj); + } + pbandk.testpb.Proto3Presence.Proto3PresenceMessage other = (pbandk.testpb.Proto3Presence.Proto3PresenceMessage) obj; + + if (getRequiredInt() + != other.getRequiredInt()) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + REQUIREDINT_FIELD_NUMBER; + hash = (53 * hash) + getRequiredInt(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(pbandk.testpb.Proto3Presence.Proto3PresenceMessage prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code pbandk.testpb.Proto3PresenceMessage} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:pbandk.testpb.Proto3PresenceMessage) + pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMessage_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMessage_fieldAccessorTable + .ensureFieldAccessorsInitialized( + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.class, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder.class); + } + + // Construct using pbandk.testpb.Proto3Presence.Proto3PresenceMessage.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + requiredInt_ = 0; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMessage_descriptor; + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage getDefaultInstanceForType() { + return pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance(); + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage build() { + pbandk.testpb.Proto3Presence.Proto3PresenceMessage result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage buildPartial() { + pbandk.testpb.Proto3Presence.Proto3PresenceMessage result = new pbandk.testpb.Proto3Presence.Proto3PresenceMessage(this); + result.requiredInt_ = requiredInt_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof pbandk.testpb.Proto3Presence.Proto3PresenceMessage) { + return mergeFrom((pbandk.testpb.Proto3Presence.Proto3PresenceMessage)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(pbandk.testpb.Proto3Presence.Proto3PresenceMessage other) { + if (other == pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance()) return this; + if (other.getRequiredInt() != 0) { + setRequiredInt(other.getRequiredInt()); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + pbandk.testpb.Proto3Presence.Proto3PresenceMessage parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (pbandk.testpb.Proto3Presence.Proto3PresenceMessage) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int requiredInt_ ; + /** + * int32 requiredInt = 1; + * @return The requiredInt. + */ + @java.lang.Override + public int getRequiredInt() { + return requiredInt_; + } + /** + * int32 requiredInt = 1; + * @param value The requiredInt to set. + * @return This builder for chaining. + */ + public Builder setRequiredInt(int value) { + + requiredInt_ = value; + onChanged(); + return this; + } + /** + * int32 requiredInt = 1; + * @return This builder for chaining. + */ + public Builder clearRequiredInt() { + + requiredInt_ = 0; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:pbandk.testpb.Proto3PresenceMessage) + } + + // @@protoc_insertion_point(class_scope:pbandk.testpb.Proto3PresenceMessage) + private static final pbandk.testpb.Proto3Presence.Proto3PresenceMessage DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new pbandk.testpb.Proto3Presence.Proto3PresenceMessage(); + } + + public static pbandk.testpb.Proto3Presence.Proto3PresenceMessage getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public Proto3PresenceMessage parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new Proto3PresenceMessage(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + public interface Proto3PresenceMainOrBuilder extends + // @@protoc_insertion_point(interface_extends:pbandk.testpb.Proto3PresenceMain) + com.google.protobuf.MessageOrBuilder { + + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + * @return Whether the optionalMessage field is set. + */ + boolean hasOptionalMessage(); + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + * @return The optionalMessage. + */ + pbandk.testpb.Proto3Presence.Proto3PresenceMessage getOptionalMessage(); + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder getOptionalMessageOrBuilder(); + + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + * @return Whether the message field is set. + */ + boolean hasMessage(); + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + * @return The message. + */ + pbandk.testpb.Proto3Presence.Proto3PresenceMessage getMessage(); + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder getMessageOrBuilder(); + + /** + * optional string optional_string = 3; + * @return Whether the optionalString field is set. + */ + boolean hasOptionalString(); + /** + * optional string optional_string = 3; + * @return The optionalString. + */ + java.lang.String getOptionalString(); + /** + * optional string optional_string = 3; + * @return The bytes for optionalString. + */ + com.google.protobuf.ByteString + getOptionalStringBytes(); + + /** + * string string = 4; + * @return The string. + */ + java.lang.String getString(); + /** + * string string = 4; + * @return The bytes for string. + */ + com.google.protobuf.ByteString + getStringBytes(); + + /** + * optional int32 optional_int = 5; + * @return Whether the optionalInt field is set. + */ + boolean hasOptionalInt(); + /** + * optional int32 optional_int = 5; + * @return The optionalInt. + */ + int getOptionalInt(); + + /** + * int32 int = 6; + * @return The int. + */ + int getInt(); + + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return Whether the optionalEnum field is set. + */ + boolean hasOptionalEnum(); + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return The enum numeric value on the wire for optionalEnum. + */ + int getOptionalEnumValue(); + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return The optionalEnum. + */ + pbandk.testpb.Proto3Presence.Proto3PresenceEnum getOptionalEnum(); + + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return The enum numeric value on the wire for enum. + */ + int getEnumValue(); + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return The enum. + */ + pbandk.testpb.Proto3Presence.Proto3PresenceEnum getEnum(); + + /** + * string one_of_string = 9; + * @return Whether the oneOfString field is set. + */ + boolean hasOneOfString(); + /** + * string one_of_string = 9; + * @return The oneOfString. + */ + java.lang.String getOneOfString(); + /** + * string one_of_string = 9; + * @return The bytes for oneOfString. + */ + com.google.protobuf.ByteString + getOneOfStringBytes(); + + /** + * int32 one_of_int = 10; + * @return Whether the oneOfInt field is set. + */ + boolean hasOneOfInt(); + /** + * int32 one_of_int = 10; + * @return The oneOfInt. + */ + int getOneOfInt(); + + public pbandk.testpb.Proto3Presence.Proto3PresenceMain.OneOfCase getOneOfCase(); + } + /** + * Protobuf type {@code pbandk.testpb.Proto3PresenceMain} + */ + public static final class Proto3PresenceMain extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:pbandk.testpb.Proto3PresenceMain) + Proto3PresenceMainOrBuilder { + private static final long serialVersionUID = 0L; + // Use Proto3PresenceMain.newBuilder() to construct. + private Proto3PresenceMain(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private Proto3PresenceMain() { + optionalString_ = ""; + string_ = ""; + optionalEnum_ = 0; + enum_ = 0; + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new Proto3PresenceMain(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private Proto3PresenceMain( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 10: { + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder subBuilder = null; + if (((bitField0_ & 0x00000001) != 0)) { + subBuilder = optionalMessage_.toBuilder(); + } + optionalMessage_ = input.readMessage(pbandk.testpb.Proto3Presence.Proto3PresenceMessage.parser(), extensionRegistry); + if (subBuilder != null) { + subBuilder.mergeFrom(optionalMessage_); + optionalMessage_ = subBuilder.buildPartial(); + } + bitField0_ |= 0x00000001; + break; + } + case 18: { + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder subBuilder = null; + if (message_ != null) { + subBuilder = message_.toBuilder(); + } + message_ = input.readMessage(pbandk.testpb.Proto3Presence.Proto3PresenceMessage.parser(), extensionRegistry); + if (subBuilder != null) { + subBuilder.mergeFrom(message_); + message_ = subBuilder.buildPartial(); + } + + break; + } + case 26: { + java.lang.String s = input.readStringRequireUtf8(); + bitField0_ |= 0x00000002; + optionalString_ = s; + break; + } + case 34: { + java.lang.String s = input.readStringRequireUtf8(); + + string_ = s; + break; + } + case 40: { + bitField0_ |= 0x00000004; + optionalInt_ = input.readInt32(); + break; + } + case 48: { + + int_ = input.readInt32(); + break; + } + case 56: { + int rawValue = input.readEnum(); + bitField0_ |= 0x00000008; + optionalEnum_ = rawValue; + break; + } + case 64: { + int rawValue = input.readEnum(); + + enum_ = rawValue; + break; + } + case 74: { + java.lang.String s = input.readStringRequireUtf8(); + oneOfCase_ = 9; + oneOf_ = s; + break; + } + case 80: { + oneOf_ = input.readInt32(); + oneOfCase_ = 10; + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMain_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMain_fieldAccessorTable + .ensureFieldAccessorsInitialized( + pbandk.testpb.Proto3Presence.Proto3PresenceMain.class, pbandk.testpb.Proto3Presence.Proto3PresenceMain.Builder.class); + } + + private int bitField0_; + private int oneOfCase_ = 0; + private java.lang.Object oneOf_; + public enum OneOfCase + implements com.google.protobuf.Internal.EnumLite, + com.google.protobuf.AbstractMessage.InternalOneOfEnum { + ONE_OF_STRING(9), + ONE_OF_INT(10), + ONEOF_NOT_SET(0); + private final int value; + private OneOfCase(int value) { + this.value = value; + } + /** + * @param value The number of the enum to look for. + * @return The enum associated with the given number. + * @deprecated Use {@link #forNumber(int)} instead. + */ + @java.lang.Deprecated + public static OneOfCase valueOf(int value) { + return forNumber(value); + } + + public static OneOfCase forNumber(int value) { + switch (value) { + case 9: return ONE_OF_STRING; + case 10: return ONE_OF_INT; + case 0: return ONEOF_NOT_SET; + default: return null; + } + } + public int getNumber() { + return this.value; + } + }; + + public OneOfCase + getOneOfCase() { + return OneOfCase.forNumber( + oneOfCase_); + } + + public static final int OPTIONAL_MESSAGE_FIELD_NUMBER = 1; + private pbandk.testpb.Proto3Presence.Proto3PresenceMessage optionalMessage_; + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + * @return Whether the optionalMessage field is set. + */ + @java.lang.Override + public boolean hasOptionalMessage() { + return ((bitField0_ & 0x00000001) != 0); + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + * @return The optionalMessage. + */ + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage getOptionalMessage() { + return optionalMessage_ == null ? pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : optionalMessage_; + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder getOptionalMessageOrBuilder() { + return optionalMessage_ == null ? pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : optionalMessage_; + } + + public static final int MESSAGE_FIELD_NUMBER = 2; + private pbandk.testpb.Proto3Presence.Proto3PresenceMessage message_; + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + * @return Whether the message field is set. + */ + @java.lang.Override + public boolean hasMessage() { + return message_ != null; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + * @return The message. + */ + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage getMessage() { + return message_ == null ? pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : message_; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder getMessageOrBuilder() { + return getMessage(); + } + + public static final int OPTIONAL_STRING_FIELD_NUMBER = 3; + private volatile java.lang.Object optionalString_; + /** + * optional string optional_string = 3; + * @return Whether the optionalString field is set. + */ + @java.lang.Override + public boolean hasOptionalString() { + return ((bitField0_ & 0x00000002) != 0); + } + /** + * optional string optional_string = 3; + * @return The optionalString. + */ + @java.lang.Override + public java.lang.String getOptionalString() { + java.lang.Object ref = optionalString_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + optionalString_ = s; + return s; + } + } + /** + * optional string optional_string = 3; + * @return The bytes for optionalString. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getOptionalStringBytes() { + java.lang.Object ref = optionalString_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + optionalString_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int STRING_FIELD_NUMBER = 4; + private volatile java.lang.Object string_; + /** + * string string = 4; + * @return The string. + */ + @java.lang.Override + public java.lang.String getString() { + java.lang.Object ref = string_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + string_ = s; + return s; + } + } + /** + * string string = 4; + * @return The bytes for string. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getStringBytes() { + java.lang.Object ref = string_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + string_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int OPTIONAL_INT_FIELD_NUMBER = 5; + private int optionalInt_; + /** + * optional int32 optional_int = 5; + * @return Whether the optionalInt field is set. + */ + @java.lang.Override + public boolean hasOptionalInt() { + return ((bitField0_ & 0x00000004) != 0); + } + /** + * optional int32 optional_int = 5; + * @return The optionalInt. + */ + @java.lang.Override + public int getOptionalInt() { + return optionalInt_; + } + + public static final int INT_FIELD_NUMBER = 6; + private int int_; + /** + * int32 int = 6; + * @return The int. + */ + @java.lang.Override + public int getInt() { + return int_; + } + + public static final int OPTIONAL_ENUM_FIELD_NUMBER = 7; + private int optionalEnum_; + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return Whether the optionalEnum field is set. + */ + @java.lang.Override public boolean hasOptionalEnum() { + return ((bitField0_ & 0x00000008) != 0); + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return The enum numeric value on the wire for optionalEnum. + */ + @java.lang.Override public int getOptionalEnumValue() { + return optionalEnum_; + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return The optionalEnum. + */ + @java.lang.Override public pbandk.testpb.Proto3Presence.Proto3PresenceEnum getOptionalEnum() { + @SuppressWarnings("deprecation") + pbandk.testpb.Proto3Presence.Proto3PresenceEnum result = pbandk.testpb.Proto3Presence.Proto3PresenceEnum.valueOf(optionalEnum_); + return result == null ? pbandk.testpb.Proto3Presence.Proto3PresenceEnum.UNRECOGNIZED : result; + } + + public static final int ENUM_FIELD_NUMBER = 8; + private int enum_; + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return The enum numeric value on the wire for enum. + */ + @java.lang.Override public int getEnumValue() { + return enum_; + } + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return The enum. + */ + @java.lang.Override public pbandk.testpb.Proto3Presence.Proto3PresenceEnum getEnum() { + @SuppressWarnings("deprecation") + pbandk.testpb.Proto3Presence.Proto3PresenceEnum result = pbandk.testpb.Proto3Presence.Proto3PresenceEnum.valueOf(enum_); + return result == null ? pbandk.testpb.Proto3Presence.Proto3PresenceEnum.UNRECOGNIZED : result; + } + + public static final int ONE_OF_STRING_FIELD_NUMBER = 9; + /** + * string one_of_string = 9; + * @return Whether the oneOfString field is set. + */ + public boolean hasOneOfString() { + return oneOfCase_ == 9; + } + /** + * string one_of_string = 9; + * @return The oneOfString. + */ + public java.lang.String getOneOfString() { + java.lang.Object ref = ""; + if (oneOfCase_ == 9) { + ref = oneOf_; + } + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + if (oneOfCase_ == 9) { + oneOf_ = s; + } + return s; + } + } + /** + * string one_of_string = 9; + * @return The bytes for oneOfString. + */ + public com.google.protobuf.ByteString + getOneOfStringBytes() { + java.lang.Object ref = ""; + if (oneOfCase_ == 9) { + ref = oneOf_; + } + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + if (oneOfCase_ == 9) { + oneOf_ = b; + } + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int ONE_OF_INT_FIELD_NUMBER = 10; + /** + * int32 one_of_int = 10; + * @return Whether the oneOfInt field is set. + */ + @java.lang.Override + public boolean hasOneOfInt() { + return oneOfCase_ == 10; + } + /** + * int32 one_of_int = 10; + * @return The oneOfInt. + */ + @java.lang.Override + public int getOneOfInt() { + if (oneOfCase_ == 10) { + return (java.lang.Integer) oneOf_; + } + return 0; + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (((bitField0_ & 0x00000001) != 0)) { + output.writeMessage(1, getOptionalMessage()); + } + if (message_ != null) { + output.writeMessage(2, getMessage()); + } + if (((bitField0_ & 0x00000002) != 0)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 3, optionalString_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(string_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 4, string_); + } + if (((bitField0_ & 0x00000004) != 0)) { + output.writeInt32(5, optionalInt_); + } + if (int_ != 0) { + output.writeInt32(6, int_); + } + if (((bitField0_ & 0x00000008) != 0)) { + output.writeEnum(7, optionalEnum_); + } + if (enum_ != pbandk.testpb.Proto3Presence.Proto3PresenceEnum.PROTO3_PRESENCE_ENUM_UNSPECIFIED.getNumber()) { + output.writeEnum(8, enum_); + } + if (oneOfCase_ == 9) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 9, oneOf_); + } + if (oneOfCase_ == 10) { + output.writeInt32( + 10, (int)((java.lang.Integer) oneOf_)); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (((bitField0_ & 0x00000001) != 0)) { + size += com.google.protobuf.CodedOutputStream + .computeMessageSize(1, getOptionalMessage()); + } + if (message_ != null) { + size += com.google.protobuf.CodedOutputStream + .computeMessageSize(2, getMessage()); + } + if (((bitField0_ & 0x00000002) != 0)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, optionalString_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(string_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(4, string_); + } + if (((bitField0_ & 0x00000004) != 0)) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(5, optionalInt_); + } + if (int_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(6, int_); + } + if (((bitField0_ & 0x00000008) != 0)) { + size += com.google.protobuf.CodedOutputStream + .computeEnumSize(7, optionalEnum_); + } + if (enum_ != pbandk.testpb.Proto3Presence.Proto3PresenceEnum.PROTO3_PRESENCE_ENUM_UNSPECIFIED.getNumber()) { + size += com.google.protobuf.CodedOutputStream + .computeEnumSize(8, enum_); + } + if (oneOfCase_ == 9) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(9, oneOf_); + } + if (oneOfCase_ == 10) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size( + 10, (int)((java.lang.Integer) oneOf_)); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof pbandk.testpb.Proto3Presence.Proto3PresenceMain)) { + return super.equals(obj); + } + pbandk.testpb.Proto3Presence.Proto3PresenceMain other = (pbandk.testpb.Proto3Presence.Proto3PresenceMain) obj; + + if (hasOptionalMessage() != other.hasOptionalMessage()) return false; + if (hasOptionalMessage()) { + if (!getOptionalMessage() + .equals(other.getOptionalMessage())) return false; + } + if (hasMessage() != other.hasMessage()) return false; + if (hasMessage()) { + if (!getMessage() + .equals(other.getMessage())) return false; + } + if (hasOptionalString() != other.hasOptionalString()) return false; + if (hasOptionalString()) { + if (!getOptionalString() + .equals(other.getOptionalString())) return false; + } + if (!getString() + .equals(other.getString())) return false; + if (hasOptionalInt() != other.hasOptionalInt()) return false; + if (hasOptionalInt()) { + if (getOptionalInt() + != other.getOptionalInt()) return false; + } + if (getInt() + != other.getInt()) return false; + if (hasOptionalEnum() != other.hasOptionalEnum()) return false; + if (hasOptionalEnum()) { + if (optionalEnum_ != other.optionalEnum_) return false; + } + if (enum_ != other.enum_) return false; + if (!getOneOfCase().equals(other.getOneOfCase())) return false; + switch (oneOfCase_) { + case 9: + if (!getOneOfString() + .equals(other.getOneOfString())) return false; + break; + case 10: + if (getOneOfInt() + != other.getOneOfInt()) return false; + break; + case 0: + default: + } + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + if (hasOptionalMessage()) { + hash = (37 * hash) + OPTIONAL_MESSAGE_FIELD_NUMBER; + hash = (53 * hash) + getOptionalMessage().hashCode(); + } + if (hasMessage()) { + hash = (37 * hash) + MESSAGE_FIELD_NUMBER; + hash = (53 * hash) + getMessage().hashCode(); + } + if (hasOptionalString()) { + hash = (37 * hash) + OPTIONAL_STRING_FIELD_NUMBER; + hash = (53 * hash) + getOptionalString().hashCode(); + } + hash = (37 * hash) + STRING_FIELD_NUMBER; + hash = (53 * hash) + getString().hashCode(); + if (hasOptionalInt()) { + hash = (37 * hash) + OPTIONAL_INT_FIELD_NUMBER; + hash = (53 * hash) + getOptionalInt(); + } + hash = (37 * hash) + INT_FIELD_NUMBER; + hash = (53 * hash) + getInt(); + if (hasOptionalEnum()) { + hash = (37 * hash) + OPTIONAL_ENUM_FIELD_NUMBER; + hash = (53 * hash) + optionalEnum_; + } + hash = (37 * hash) + ENUM_FIELD_NUMBER; + hash = (53 * hash) + enum_; + switch (oneOfCase_) { + case 9: + hash = (37 * hash) + ONE_OF_STRING_FIELD_NUMBER; + hash = (53 * hash) + getOneOfString().hashCode(); + break; + case 10: + hash = (37 * hash) + ONE_OF_INT_FIELD_NUMBER; + hash = (53 * hash) + getOneOfInt(); + break; + case 0: + default: + } + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(pbandk.testpb.Proto3Presence.Proto3PresenceMain prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code pbandk.testpb.Proto3PresenceMain} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:pbandk.testpb.Proto3PresenceMain) + pbandk.testpb.Proto3Presence.Proto3PresenceMainOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMain_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMain_fieldAccessorTable + .ensureFieldAccessorsInitialized( + pbandk.testpb.Proto3Presence.Proto3PresenceMain.class, pbandk.testpb.Proto3Presence.Proto3PresenceMain.Builder.class); + } + + // Construct using pbandk.testpb.Proto3Presence.Proto3PresenceMain.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + getOptionalMessageFieldBuilder(); + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + if (optionalMessageBuilder_ == null) { + optionalMessage_ = null; + } else { + optionalMessageBuilder_.clear(); + } + bitField0_ = (bitField0_ & ~0x00000001); + if (messageBuilder_ == null) { + message_ = null; + } else { + message_ = null; + messageBuilder_ = null; + } + optionalString_ = ""; + bitField0_ = (bitField0_ & ~0x00000002); + string_ = ""; + + optionalInt_ = 0; + bitField0_ = (bitField0_ & ~0x00000004); + int_ = 0; + + optionalEnum_ = 0; + bitField0_ = (bitField0_ & ~0x00000008); + enum_ = 0; + + oneOfCase_ = 0; + oneOf_ = null; + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return pbandk.testpb.Proto3Presence.internal_static_pbandk_testpb_Proto3PresenceMain_descriptor; + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMain getDefaultInstanceForType() { + return pbandk.testpb.Proto3Presence.Proto3PresenceMain.getDefaultInstance(); + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMain build() { + pbandk.testpb.Proto3Presence.Proto3PresenceMain result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMain buildPartial() { + pbandk.testpb.Proto3Presence.Proto3PresenceMain result = new pbandk.testpb.Proto3Presence.Proto3PresenceMain(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + if (((from_bitField0_ & 0x00000001) != 0)) { + if (optionalMessageBuilder_ == null) { + result.optionalMessage_ = optionalMessage_; + } else { + result.optionalMessage_ = optionalMessageBuilder_.build(); + } + to_bitField0_ |= 0x00000001; + } + if (messageBuilder_ == null) { + result.message_ = message_; + } else { + result.message_ = messageBuilder_.build(); + } + if (((from_bitField0_ & 0x00000002) != 0)) { + to_bitField0_ |= 0x00000002; + } + result.optionalString_ = optionalString_; + result.string_ = string_; + if (((from_bitField0_ & 0x00000004) != 0)) { + result.optionalInt_ = optionalInt_; + to_bitField0_ |= 0x00000004; + } + result.int_ = int_; + if (((from_bitField0_ & 0x00000008) != 0)) { + to_bitField0_ |= 0x00000008; + } + result.optionalEnum_ = optionalEnum_; + result.enum_ = enum_; + if (oneOfCase_ == 9) { + result.oneOf_ = oneOf_; + } + if (oneOfCase_ == 10) { + result.oneOf_ = oneOf_; + } + result.bitField0_ = to_bitField0_; + result.oneOfCase_ = oneOfCase_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof pbandk.testpb.Proto3Presence.Proto3PresenceMain) { + return mergeFrom((pbandk.testpb.Proto3Presence.Proto3PresenceMain)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(pbandk.testpb.Proto3Presence.Proto3PresenceMain other) { + if (other == pbandk.testpb.Proto3Presence.Proto3PresenceMain.getDefaultInstance()) return this; + if (other.hasOptionalMessage()) { + mergeOptionalMessage(other.getOptionalMessage()); + } + if (other.hasMessage()) { + mergeMessage(other.getMessage()); + } + if (other.hasOptionalString()) { + bitField0_ |= 0x00000002; + optionalString_ = other.optionalString_; + onChanged(); + } + if (!other.getString().isEmpty()) { + string_ = other.string_; + onChanged(); + } + if (other.hasOptionalInt()) { + setOptionalInt(other.getOptionalInt()); + } + if (other.getInt() != 0) { + setInt(other.getInt()); + } + if (other.hasOptionalEnum()) { + setOptionalEnum(other.getOptionalEnum()); + } + if (other.enum_ != 0) { + setEnumValue(other.getEnumValue()); + } + switch (other.getOneOfCase()) { + case ONE_OF_STRING: { + oneOfCase_ = 9; + oneOf_ = other.oneOf_; + onChanged(); + break; + } + case ONE_OF_INT: { + setOneOfInt(other.getOneOfInt()); + break; + } + case ONEOF_NOT_SET: { + break; + } + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + pbandk.testpb.Proto3Presence.Proto3PresenceMain parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (pbandk.testpb.Proto3Presence.Proto3PresenceMain) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + private int oneOfCase_ = 0; + private java.lang.Object oneOf_; + public OneOfCase + getOneOfCase() { + return OneOfCase.forNumber( + oneOfCase_); + } + + public Builder clearOneOf() { + oneOfCase_ = 0; + oneOf_ = null; + onChanged(); + return this; + } + + private int bitField0_; + + private pbandk.testpb.Proto3Presence.Proto3PresenceMessage optionalMessage_; + private com.google.protobuf.SingleFieldBuilderV3< + pbandk.testpb.Proto3Presence.Proto3PresenceMessage, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder, pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder> optionalMessageBuilder_; + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + * @return Whether the optionalMessage field is set. + */ + public boolean hasOptionalMessage() { + return ((bitField0_ & 0x00000001) != 0); + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + * @return The optionalMessage. + */ + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage getOptionalMessage() { + if (optionalMessageBuilder_ == null) { + return optionalMessage_ == null ? pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : optionalMessage_; + } else { + return optionalMessageBuilder_.getMessage(); + } + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + public Builder setOptionalMessage(pbandk.testpb.Proto3Presence.Proto3PresenceMessage value) { + if (optionalMessageBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + optionalMessage_ = value; + onChanged(); + } else { + optionalMessageBuilder_.setMessage(value); + } + bitField0_ |= 0x00000001; + return this; + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + public Builder setOptionalMessage( + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder builderForValue) { + if (optionalMessageBuilder_ == null) { + optionalMessage_ = builderForValue.build(); + onChanged(); + } else { + optionalMessageBuilder_.setMessage(builderForValue.build()); + } + bitField0_ |= 0x00000001; + return this; + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + public Builder mergeOptionalMessage(pbandk.testpb.Proto3Presence.Proto3PresenceMessage value) { + if (optionalMessageBuilder_ == null) { + if (((bitField0_ & 0x00000001) != 0) && + optionalMessage_ != null && + optionalMessage_ != pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance()) { + optionalMessage_ = + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.newBuilder(optionalMessage_).mergeFrom(value).buildPartial(); + } else { + optionalMessage_ = value; + } + onChanged(); + } else { + optionalMessageBuilder_.mergeFrom(value); + } + bitField0_ |= 0x00000001; + return this; + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + public Builder clearOptionalMessage() { + if (optionalMessageBuilder_ == null) { + optionalMessage_ = null; + onChanged(); + } else { + optionalMessageBuilder_.clear(); + } + bitField0_ = (bitField0_ & ~0x00000001); + return this; + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder getOptionalMessageBuilder() { + bitField0_ |= 0x00000001; + onChanged(); + return getOptionalMessageFieldBuilder().getBuilder(); + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + public pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder getOptionalMessageOrBuilder() { + if (optionalMessageBuilder_ != null) { + return optionalMessageBuilder_.getMessageOrBuilder(); + } else { + return optionalMessage_ == null ? + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : optionalMessage_; + } + } + /** + * optional .pbandk.testpb.Proto3PresenceMessage optional_message = 1; + */ + private com.google.protobuf.SingleFieldBuilderV3< + pbandk.testpb.Proto3Presence.Proto3PresenceMessage, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder, pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder> + getOptionalMessageFieldBuilder() { + if (optionalMessageBuilder_ == null) { + optionalMessageBuilder_ = new com.google.protobuf.SingleFieldBuilderV3< + pbandk.testpb.Proto3Presence.Proto3PresenceMessage, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder, pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder>( + getOptionalMessage(), + getParentForChildren(), + isClean()); + optionalMessage_ = null; + } + return optionalMessageBuilder_; + } + + private pbandk.testpb.Proto3Presence.Proto3PresenceMessage message_; + private com.google.protobuf.SingleFieldBuilderV3< + pbandk.testpb.Proto3Presence.Proto3PresenceMessage, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder, pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder> messageBuilder_; + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + * @return Whether the message field is set. + */ + public boolean hasMessage() { + return messageBuilder_ != null || message_ != null; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + * @return The message. + */ + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage getMessage() { + if (messageBuilder_ == null) { + return message_ == null ? pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : message_; + } else { + return messageBuilder_.getMessage(); + } + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + public Builder setMessage(pbandk.testpb.Proto3Presence.Proto3PresenceMessage value) { + if (messageBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + message_ = value; + onChanged(); + } else { + messageBuilder_.setMessage(value); + } + + return this; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + public Builder setMessage( + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder builderForValue) { + if (messageBuilder_ == null) { + message_ = builderForValue.build(); + onChanged(); + } else { + messageBuilder_.setMessage(builderForValue.build()); + } + + return this; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + public Builder mergeMessage(pbandk.testpb.Proto3Presence.Proto3PresenceMessage value) { + if (messageBuilder_ == null) { + if (message_ != null) { + message_ = + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.newBuilder(message_).mergeFrom(value).buildPartial(); + } else { + message_ = value; + } + onChanged(); + } else { + messageBuilder_.mergeFrom(value); + } + + return this; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + public Builder clearMessage() { + if (messageBuilder_ == null) { + message_ = null; + onChanged(); + } else { + message_ = null; + messageBuilder_ = null; + } + + return this; + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + public pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder getMessageBuilder() { + + onChanged(); + return getMessageFieldBuilder().getBuilder(); + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + public pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder getMessageOrBuilder() { + if (messageBuilder_ != null) { + return messageBuilder_.getMessageOrBuilder(); + } else { + return message_ == null ? + pbandk.testpb.Proto3Presence.Proto3PresenceMessage.getDefaultInstance() : message_; + } + } + /** + * .pbandk.testpb.Proto3PresenceMessage message = 2; + */ + private com.google.protobuf.SingleFieldBuilderV3< + pbandk.testpb.Proto3Presence.Proto3PresenceMessage, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder, pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder> + getMessageFieldBuilder() { + if (messageBuilder_ == null) { + messageBuilder_ = new com.google.protobuf.SingleFieldBuilderV3< + pbandk.testpb.Proto3Presence.Proto3PresenceMessage, pbandk.testpb.Proto3Presence.Proto3PresenceMessage.Builder, pbandk.testpb.Proto3Presence.Proto3PresenceMessageOrBuilder>( + getMessage(), + getParentForChildren(), + isClean()); + message_ = null; + } + return messageBuilder_; + } + + private java.lang.Object optionalString_ = ""; + /** + * optional string optional_string = 3; + * @return Whether the optionalString field is set. + */ + public boolean hasOptionalString() { + return ((bitField0_ & 0x00000002) != 0); + } + /** + * optional string optional_string = 3; + * @return The optionalString. + */ + public java.lang.String getOptionalString() { + java.lang.Object ref = optionalString_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + optionalString_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string optional_string = 3; + * @return The bytes for optionalString. + */ + public com.google.protobuf.ByteString + getOptionalStringBytes() { + java.lang.Object ref = optionalString_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + optionalString_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * optional string optional_string = 3; + * @param value The optionalString to set. + * @return This builder for chaining. + */ + public Builder setOptionalString( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000002; + optionalString_ = value; + onChanged(); + return this; + } + /** + * optional string optional_string = 3; + * @return This builder for chaining. + */ + public Builder clearOptionalString() { + bitField0_ = (bitField0_ & ~0x00000002); + optionalString_ = getDefaultInstance().getOptionalString(); + onChanged(); + return this; + } + /** + * optional string optional_string = 3; + * @param value The bytes for optionalString to set. + * @return This builder for chaining. + */ + public Builder setOptionalStringBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + bitField0_ |= 0x00000002; + optionalString_ = value; + onChanged(); + return this; + } + + private java.lang.Object string_ = ""; + /** + * string string = 4; + * @return The string. + */ + public java.lang.String getString() { + java.lang.Object ref = string_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + string_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string string = 4; + * @return The bytes for string. + */ + public com.google.protobuf.ByteString + getStringBytes() { + java.lang.Object ref = string_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + string_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string string = 4; + * @param value The string to set. + * @return This builder for chaining. + */ + public Builder setString( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + string_ = value; + onChanged(); + return this; + } + /** + * string string = 4; + * @return This builder for chaining. + */ + public Builder clearString() { + + string_ = getDefaultInstance().getString(); + onChanged(); + return this; + } + /** + * string string = 4; + * @param value The bytes for string to set. + * @return This builder for chaining. + */ + public Builder setStringBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + string_ = value; + onChanged(); + return this; + } + + private int optionalInt_ ; + /** + * optional int32 optional_int = 5; + * @return Whether the optionalInt field is set. + */ + @java.lang.Override + public boolean hasOptionalInt() { + return ((bitField0_ & 0x00000004) != 0); + } + /** + * optional int32 optional_int = 5; + * @return The optionalInt. + */ + @java.lang.Override + public int getOptionalInt() { + return optionalInt_; + } + /** + * optional int32 optional_int = 5; + * @param value The optionalInt to set. + * @return This builder for chaining. + */ + public Builder setOptionalInt(int value) { + bitField0_ |= 0x00000004; + optionalInt_ = value; + onChanged(); + return this; + } + /** + * optional int32 optional_int = 5; + * @return This builder for chaining. + */ + public Builder clearOptionalInt() { + bitField0_ = (bitField0_ & ~0x00000004); + optionalInt_ = 0; + onChanged(); + return this; + } + + private int int_ ; + /** + * int32 int = 6; + * @return The int. + */ + @java.lang.Override + public int getInt() { + return int_; + } + /** + * int32 int = 6; + * @param value The int to set. + * @return This builder for chaining. + */ + public Builder setInt(int value) { + + int_ = value; + onChanged(); + return this; + } + /** + * int32 int = 6; + * @return This builder for chaining. + */ + public Builder clearInt() { + + int_ = 0; + onChanged(); + return this; + } + + private int optionalEnum_ = 0; + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return Whether the optionalEnum field is set. + */ + @java.lang.Override public boolean hasOptionalEnum() { + return ((bitField0_ & 0x00000008) != 0); + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return The enum numeric value on the wire for optionalEnum. + */ + @java.lang.Override public int getOptionalEnumValue() { + return optionalEnum_; + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @param value The enum numeric value on the wire for optionalEnum to set. + * @return This builder for chaining. + */ + public Builder setOptionalEnumValue(int value) { + bitField0_ |= 0x00000008; + optionalEnum_ = value; + onChanged(); + return this; + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return The optionalEnum. + */ + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceEnum getOptionalEnum() { + @SuppressWarnings("deprecation") + pbandk.testpb.Proto3Presence.Proto3PresenceEnum result = pbandk.testpb.Proto3Presence.Proto3PresenceEnum.valueOf(optionalEnum_); + return result == null ? pbandk.testpb.Proto3Presence.Proto3PresenceEnum.UNRECOGNIZED : result; + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @param value The optionalEnum to set. + * @return This builder for chaining. + */ + public Builder setOptionalEnum(pbandk.testpb.Proto3Presence.Proto3PresenceEnum value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000008; + optionalEnum_ = value.getNumber(); + onChanged(); + return this; + } + /** + * optional .pbandk.testpb.Proto3PresenceEnum optional_enum = 7; + * @return This builder for chaining. + */ + public Builder clearOptionalEnum() { + bitField0_ = (bitField0_ & ~0x00000008); + optionalEnum_ = 0; + onChanged(); + return this; + } + + private int enum_ = 0; + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return The enum numeric value on the wire for enum. + */ + @java.lang.Override public int getEnumValue() { + return enum_; + } + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @param value The enum numeric value on the wire for enum to set. + * @return This builder for chaining. + */ + public Builder setEnumValue(int value) { + + enum_ = value; + onChanged(); + return this; + } + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return The enum. + */ + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceEnum getEnum() { + @SuppressWarnings("deprecation") + pbandk.testpb.Proto3Presence.Proto3PresenceEnum result = pbandk.testpb.Proto3Presence.Proto3PresenceEnum.valueOf(enum_); + return result == null ? pbandk.testpb.Proto3Presence.Proto3PresenceEnum.UNRECOGNIZED : result; + } + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @param value The enum to set. + * @return This builder for chaining. + */ + public Builder setEnum(pbandk.testpb.Proto3Presence.Proto3PresenceEnum value) { + if (value == null) { + throw new NullPointerException(); + } + + enum_ = value.getNumber(); + onChanged(); + return this; + } + /** + * .pbandk.testpb.Proto3PresenceEnum enum = 8; + * @return This builder for chaining. + */ + public Builder clearEnum() { + + enum_ = 0; + onChanged(); + return this; + } + + /** + * string one_of_string = 9; + * @return Whether the oneOfString field is set. + */ + @java.lang.Override + public boolean hasOneOfString() { + return oneOfCase_ == 9; + } + /** + * string one_of_string = 9; + * @return The oneOfString. + */ + @java.lang.Override + public java.lang.String getOneOfString() { + java.lang.Object ref = ""; + if (oneOfCase_ == 9) { + ref = oneOf_; + } + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + if (oneOfCase_ == 9) { + oneOf_ = s; + } + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string one_of_string = 9; + * @return The bytes for oneOfString. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getOneOfStringBytes() { + java.lang.Object ref = ""; + if (oneOfCase_ == 9) { + ref = oneOf_; + } + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + if (oneOfCase_ == 9) { + oneOf_ = b; + } + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string one_of_string = 9; + * @param value The oneOfString to set. + * @return This builder for chaining. + */ + public Builder setOneOfString( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + oneOfCase_ = 9; + oneOf_ = value; + onChanged(); + return this; + } + /** + * string one_of_string = 9; + * @return This builder for chaining. + */ + public Builder clearOneOfString() { + if (oneOfCase_ == 9) { + oneOfCase_ = 0; + oneOf_ = null; + onChanged(); + } + return this; + } + /** + * string one_of_string = 9; + * @param value The bytes for oneOfString to set. + * @return This builder for chaining. + */ + public Builder setOneOfStringBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + oneOfCase_ = 9; + oneOf_ = value; + onChanged(); + return this; + } + + /** + * int32 one_of_int = 10; + * @return Whether the oneOfInt field is set. + */ + public boolean hasOneOfInt() { + return oneOfCase_ == 10; + } + /** + * int32 one_of_int = 10; + * @return The oneOfInt. + */ + public int getOneOfInt() { + if (oneOfCase_ == 10) { + return (java.lang.Integer) oneOf_; + } + return 0; + } + /** + * int32 one_of_int = 10; + * @param value The oneOfInt to set. + * @return This builder for chaining. + */ + public Builder setOneOfInt(int value) { + oneOfCase_ = 10; + oneOf_ = value; + onChanged(); + return this; + } + /** + * int32 one_of_int = 10; + * @return This builder for chaining. + */ + public Builder clearOneOfInt() { + if (oneOfCase_ == 10) { + oneOfCase_ = 0; + oneOf_ = null; + onChanged(); + } + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:pbandk.testpb.Proto3PresenceMain) + } + + // @@protoc_insertion_point(class_scope:pbandk.testpb.Proto3PresenceMain) + private static final pbandk.testpb.Proto3Presence.Proto3PresenceMain DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new pbandk.testpb.Proto3Presence.Proto3PresenceMain(); + } + + public static pbandk.testpb.Proto3Presence.Proto3PresenceMain getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public Proto3PresenceMain parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new Proto3PresenceMain(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public pbandk.testpb.Proto3Presence.Proto3PresenceMain getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_pbandk_testpb_Proto3PresenceMessage_descriptor; + private static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_pbandk_testpb_Proto3PresenceMessage_fieldAccessorTable; + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_pbandk_testpb_Proto3PresenceMain_descriptor; + private static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_pbandk_testpb_Proto3PresenceMain_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n$pbandk/testpb/proto_3_presence.proto\022\r" + + "pbandk.testpb\",\n\025Proto3PresenceMessage\022\023" + + "\n\013requiredInt\030\001 \001(\005\"\332\003\n\022Proto3PresenceMa" + + "in\022C\n\020optional_message\030\001 \001(\0132$.pbandk.te" + + "stpb.Proto3PresenceMessageH\001\210\001\001\0225\n\007messa" + + "ge\030\002 \001(\0132$.pbandk.testpb.Proto3PresenceM" + + "essage\022\034\n\017optional_string\030\003 \001(\tH\002\210\001\001\022\016\n\006" + + "string\030\004 \001(\t\022\031\n\014optional_int\030\005 \001(\005H\003\210\001\001\022" + + "\013\n\003int\030\006 \001(\005\022=\n\roptional_enum\030\007 \001(\0162!.pb" + + "andk.testpb.Proto3PresenceEnumH\004\210\001\001\022/\n\004e" + + "num\030\010 \001(\0162!.pbandk.testpb.Proto3Presence" + + "Enum\022\027\n\rone_of_string\030\t \001(\tH\000\022\024\n\none_of_" + + "int\030\n \001(\005H\000B\007\n\005oneOfB\023\n\021_optional_messag" + + "eB\022\n\020_optional_stringB\017\n\r_optional_intB\020" + + "\n\016_optional_enum*^\n\022Proto3PresenceEnum\022$" + + "\n PROTO3_PRESENCE_ENUM_UNSPECIFIED\020\000\022\"\n\036" + + "PROTO3_PRESENCE_ENUM_SPECIFIED\020\001b\006proto3" + }; + descriptor = com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }); + internal_static_pbandk_testpb_Proto3PresenceMessage_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_pbandk_testpb_Proto3PresenceMessage_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_pbandk_testpb_Proto3PresenceMessage_descriptor, + new java.lang.String[] { "RequiredInt", }); + internal_static_pbandk_testpb_Proto3PresenceMain_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_pbandk_testpb_Proto3PresenceMain_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_pbandk_testpb_Proto3PresenceMain_descriptor, + new java.lang.String[] { "OptionalMessage", "Message", "OptionalString", "String", "OptionalInt", "Int", "OptionalEnum", "Enum", "OneOfString", "OneOfInt", "OneOf", "OptionalMessage", "OptionalString", "OptionalInt", "OptionalEnum", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/CodeGenerator.kt b/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/CodeGenerator.kt index 1de51418..c183db98 100644 --- a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/CodeGenerator.kt +++ b/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/CodeGenerator.kt @@ -521,7 +521,7 @@ public open class CodeGenerator( } } - protected val File.Field.Numbered.Standard.hasPresence: Boolean get() = (file.version == 2 && optional) + protected val File.Field.Numbered.Standard.hasPresence: Boolean get() = optional protected fun File.Field.Numbered.Standard.mapEntry(): File.Type.Message? = if (!map) null else (localType as? File.Type.Message)?.takeIf { it.mapEntry } diff --git a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/FileBuilder.kt b/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/FileBuilder.kt index e08ea114..3438a821 100644 --- a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/FileBuilder.kt +++ b/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/FileBuilder.kt @@ -115,8 +115,16 @@ internal open class FileBuilder(val namer: Namer = Namer.Standard, val supportMa } + oneofFields.groupBy { it.oneofIndex!! }.mapNotNull { (oneofIndex, fields) -> - msgDesc.oneofDecl[oneofIndex].name?.let { oneofName -> - oneofFieldFromProto(ctx, oneofName, fields, usedFieldNames, usedTypeNames) + // "Every proto3 optional field is placed into a one-field oneof. + // We call this a "synthetic" oneof, as it was not present in the source .proto file." + // https://github.com/protocolbuffers/protobuf/blob/master/docs/implementing_proto3_presence.md#background + val synthetic = fields.size == 1 && (fields[0].proto3Optional ?: false) + if (synthetic) { + numberedFieldFromProto(ctx, fields[0], usedFieldNames) + } else { + msgDesc.oneofDecl[oneofIndex].name?.let { oneofName -> + oneofFieldFromProto(ctx, oneofName, fields, usedFieldNames, usedTypeNames) + } } } } @@ -131,6 +139,12 @@ internal open class FileBuilder(val namer: Namer = Namer.Standard, val supportMa ): File.Field.OneOf { val fields = oneofFields.map { // wrapper fields are not supposed to be used inside of oneof's + numberedFieldFromProto( + ctx, + it, + mutableSetOf(), + ctx.fileDesc.syntax.let { it == "proto2" || it == null} + ) as File.Field.Numbered.Standard numberedFieldFromProto(ctx, it, mutableSetOf(), true) as File.Field.Numbered.Standard } return File.Field.OneOf( @@ -180,7 +194,9 @@ internal open class FileBuilder(val namer: Namer = Namer.Standard, val supportMa localTypeName = fieldDesc.typeName, repeated = fieldDesc.label == FieldDescriptorProto.Label.REPEATED, jsonName = fieldDesc.jsonName, - optional = !alwaysRequired && fieldDesc.label == FieldDescriptorProto.Label.OPTIONAL, + optional = !alwaysRequired && + ((fieldDesc.label == FieldDescriptorProto.Label.OPTIONAL && ctx.fileDesc.usesProto2Syntax) || + (fieldDesc.proto3Optional ?: false)), packed = !type.neverPacked && (fieldDesc.options?.packed ?: (ctx.fileDesc.syntax == "proto3")), map = supportMaps && fieldDesc.label == FieldDescriptorProto.Label.REPEATED && @@ -252,3 +268,5 @@ internal open class FileBuilder(val namer: Namer = Namer.Standard, val supportMa companion object : FileBuilder() } + +private val FileDescriptorProto.usesProto2Syntax: Boolean get() = syntax == null || syntax == "proto2" diff --git a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/Main.kt b/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/Main.kt index c31f77e8..a38b431d 100644 --- a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/Main.kt +++ b/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/Main.kt @@ -4,7 +4,9 @@ import pbandk.gen.pb.CodeGeneratorRequest import pbandk.gen.pb.CodeGeneratorResponse private var logDebug = false -private inline fun debug(fn: () -> String) { if (logDebug) Platform.stderrPrintln(fn()) } +private inline fun debug(fn: () -> String) { + if (logDebug) Platform.stderrPrintln(fn()) +} public fun main() { Platform.stdoutWriteResponse(runGenerator(Platform.stdinReadRequest())) @@ -25,61 +27,66 @@ internal fun runGenerator(request: CodeGeneratorRequest): CodeGeneratorResponse // Convert to file model and generate the code only for ones requested val kotlinTypeMappings = mutableMapOf() - return CodeGeneratorResponse(file = request.protoFile.flatMap { protoFile -> - val packageName = protoFile.`package` - debug { "Reading ${protoFile.name}, package: $packageName" } + return CodeGeneratorResponse( + supportedFeatures = CodeGeneratorResponse.Feature.PROTO3_OPTIONAL.value.toLong(), + file = request.protoFile.flatMap { protoFile -> + val packageName = protoFile.`package` + debug { "Reading ${protoFile.name}, package: $packageName" } - val needToGenerate = request.fileToGenerate.contains(protoFile.name) + val needToGenerate = request.fileToGenerate.contains(protoFile.name) - // Convert the file to our model - val file = FileBuilder.buildFile(FileBuilder.Context(protoFile, params.let { - // As a special case, if we're not generating it but it's a well-known type package, change it our known one - if (needToGenerate || packageName != "google.protobuf") it - else it + ("kotlin_package" to "pbandk.wkt") - })) + // Convert the file to our model + val file = FileBuilder.buildFile(FileBuilder.Context(protoFile, params.let { + // As a special case, if we're not generating it but it's a well-known type package, change it our known one + if (needToGenerate || packageName != "google.protobuf") it + else it + ("kotlin_package" to "pbandk.wkt") + })) - // Update the type mappings - kotlinTypeMappings += file.kotlinTypeMappings() + // Update the type mappings + kotlinTypeMappings += file.kotlinTypeMappings() - // Generate if necessary - if (!needToGenerate) emptyList() else { - // Package name + file name (s/proto/kt) - val fileNameSansPath = protoFile.name!!.substringAfterLast('/') - val filePath = (file.kotlinPackageName?.replace('.', '/')?.plus('/') ?: "") + - fileNameSansPath.removeSuffix(".proto") + ".kt" - debug { "Generating $filePath" } - val code = CodeGenerator(file = file, kotlinTypeMappings = kotlinTypeMappings, params = params).generate() + // Generate if necessary + if (!needToGenerate) emptyList() else { + // Package name + file name (s/proto/kt) + val fileNameSansPath = protoFile.name!!.substringAfterLast('/') + val filePath = (file.kotlinPackageName?.replace('.', '/')?.plus('/') ?: "") + + fileNameSansPath.removeSuffix(".proto") + ".kt" + debug { "Generating $filePath" } + val code = + CodeGenerator(file = file, kotlinTypeMappings = kotlinTypeMappings, params = params).generate() - // Do service gen if generator present - var extraServiceCode = "" - val serviceFiles = if (serviceGen == null) emptyList() else protoFile.service.flatMap { protoService -> - val results = serviceGen.generate(ServiceGenerator.Service( - file = file, - filePath = filePath, - existingCode = code, - kotlinTypeMappings = kotlinTypeMappings, - raw = protoService, - rawRequest = request, - debugFn = ::debug - )) - results.mapNotNull { result -> - // If the result is for this file, just append - if (result.otherFilePath == null) { - extraServiceCode += "\n" + result.code - null - } else CodeGeneratorResponse.File( - name = result.otherFilePath, - insertionPoint = result.otherFileInsertionPoint, - content = result.code + // Do service gen if generator present + var extraServiceCode = "" + val serviceFiles = if (serviceGen == null) emptyList() else protoFile.service.flatMap { protoService -> + val results = serviceGen.generate( + ServiceGenerator.Service( + file = file, + filePath = filePath, + existingCode = code, + kotlinTypeMappings = kotlinTypeMappings, + raw = protoService, + rawRequest = request, + debugFn = ::debug + ) ) + results.mapNotNull { result -> + // If the result is for this file, just append + if (result.otherFilePath == null) { + extraServiceCode += "\n" + result.code + null + } else CodeGeneratorResponse.File( + name = result.otherFilePath, + insertionPoint = result.otherFileInsertionPoint, + content = result.code + ) + } } - } - val primaryFiles = - if (file.types.isEmpty() && extraServiceCode.isEmpty()) emptyList() - else listOf(CodeGeneratorResponse.File(name = filePath, content = code + extraServiceCode)) + val primaryFiles = + if (file.types.isEmpty() && extraServiceCode.isEmpty()) emptyList() + else listOf(CodeGeneratorResponse.File(name = filePath, content = code + extraServiceCode)) - primaryFiles + serviceFiles - } - }) + primaryFiles + serviceFiles + } + }) } \ No newline at end of file diff --git a/protoc-gen-pbandk/lib/src/jvmTest/kotlin/CodeGeneratorTest.kt b/protoc-gen-pbandk/lib/src/jvmTest/kotlin/CodeGeneratorTest.kt index 521bfc01..ae2c5d3b 100644 --- a/protoc-gen-pbandk/lib/src/jvmTest/kotlin/CodeGeneratorTest.kt +++ b/protoc-gen-pbandk/lib/src/jvmTest/kotlin/CodeGeneratorTest.kt @@ -9,8 +9,10 @@ import pbandk.wkt.FileDescriptorSet import java.io.File import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.hasAnnotation +import kotlin.reflect.full.memberProperties import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertTrue class CodeGeneratorTest { @@ -58,6 +60,22 @@ class CodeGeneratorTest { valueClazz.classLoader.loadClass("foobar.Value\$Value") } + @Test + fun testProto3Optional() { + val result = compileProto("proto_3_presence.proto") + assertEquals(ExitCode.OK, result.exitCode, result.messages) + + val mainClazz = result.classLoader.loadClass("pbandk.testpb.Proto3PresenceMain").kotlin + assertTrue("optionalMessage should be nullable") { mainClazz.memberProperties.find { it.name == "optionalMessage" }!!.returnType.isMarkedNullable } + assertTrue("message should be nullable") { mainClazz.memberProperties.find { it.name == "message" }!!.returnType.isMarkedNullable } + assertTrue("optionalString should be nullable") { mainClazz.memberProperties.find { it.name == "optionalString" }!!.returnType.isMarkedNullable } + assertFalse("string should not be nullable") { mainClazz.memberProperties.find { it.name == "string" }!!.returnType.isMarkedNullable } + assertTrue("optionalInt should be nullable") { mainClazz.memberProperties.find { it.name == "optionalInt" }!!.returnType.isMarkedNullable } + assertFalse("int should not be nullable") { mainClazz.memberProperties.find { it.name == "int" }!!.returnType.isMarkedNullable } + assertTrue("optionalEnum should be nullable") { mainClazz.memberProperties.find { it.name == "optionalEnum" }!!.returnType.isMarkedNullable } + assertFalse("enum should not be nullable") { mainClazz.memberProperties.find { it.name == "enum" }!!.returnType.isMarkedNullable } + } + private fun compileProto(inputProto: String): KotlinCompilation.Result { val gen = runGenerator( CodeGeneratorRequest( diff --git a/protoc-gen-pbandk/lib/src/jvmTest/resources/protos/proto_3_presence.proto b/protoc-gen-pbandk/lib/src/jvmTest/resources/protos/proto_3_presence.proto new file mode 100644 index 00000000..05469749 --- /dev/null +++ b/protoc-gen-pbandk/lib/src/jvmTest/resources/protos/proto_3_presence.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; +package pbandk.testpb; + +message Proto3PresenceMessage {} + +enum Proto3PresenceEnum { + PROTO3_PRESENCE_ENUM_UNSPECIFIED = 0; +} + +message Proto3PresenceMain { + optional Proto3PresenceMessage optional_message = 1; + Proto3PresenceMessage message = 2; + optional string optional_string = 3; + string string = 4; + optional int32 optional_int = 5; + int32 int = 6; + optional Proto3PresenceEnum optional_enum = 7; + Proto3PresenceEnum enum = 8; + oneof one_of { + string one_of_string = 9; + int32 one_of_int = 10; + } +} \ No newline at end of file diff --git a/runtime/src/commonTest/kotlin/pbandk/testpb/proto_3_presence.kt b/runtime/src/commonTest/kotlin/pbandk/testpb/proto_3_presence.kt new file mode 100644 index 00000000..f6addd59 --- /dev/null +++ b/runtime/src/commonTest/kotlin/pbandk/testpb/proto_3_presence.kt @@ -0,0 +1,271 @@ +@file:OptIn(pbandk.PublicForGeneratedCode::class) + +package pbandk.testpb + +@pbandk.Export +public sealed class Proto3PresenceEnum(override val value: Int, override val name: String? = null) : pbandk.Message.Enum { + override fun equals(other: kotlin.Any?): Boolean = other is Proto3PresenceEnum && other.value == value + override fun hashCode(): Int = value.hashCode() + override fun toString(): String = "Proto3PresenceEnum.${name ?: "UNRECOGNIZED"}(value=$value)" + + public object PROTO3_PRESENCE_ENUM_UNSPECIFIED : Proto3PresenceEnum(0, "PROTO3_PRESENCE_ENUM_UNSPECIFIED") + public object PROTO3_PRESENCE_ENUM_SPECIFIED : Proto3PresenceEnum(1, "PROTO3_PRESENCE_ENUM_SPECIFIED") + public class UNRECOGNIZED(value: Int) : Proto3PresenceEnum(value) + + public companion object : pbandk.Message.Enum.Companion { + public val values: List by lazy { listOf(PROTO3_PRESENCE_ENUM_UNSPECIFIED, PROTO3_PRESENCE_ENUM_SPECIFIED) } + override fun fromValue(value: Int): Proto3PresenceEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value) + override fun fromName(name: String): Proto3PresenceEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No Proto3PresenceEnum with name: $name") + } +} + +@pbandk.Export +public data class Proto3PresenceMessage( + val requiredInt: Int = 0, + override val unknownFields: Map = emptyMap() +) : pbandk.Message { + override operator fun plus(other: pbandk.Message?): pbandk.testpb.Proto3PresenceMessage = protoMergeImpl(other) + override val descriptor: pbandk.MessageDescriptor get() = Companion.descriptor + override val protoSize: Int by lazy { super.protoSize } + public companion object : pbandk.Message.Companion { + public val defaultInstance: pbandk.testpb.Proto3PresenceMessage by lazy { pbandk.testpb.Proto3PresenceMessage() } + override fun decodeWith(u: pbandk.MessageDecoder): pbandk.testpb.Proto3PresenceMessage = pbandk.testpb.Proto3PresenceMessage.decodeWithImpl(u) + + override val descriptor: pbandk.MessageDescriptor by lazy { + val fieldsList = ArrayList>(1) + fieldsList.apply { + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "requiredInt", + number = 1, + type = pbandk.FieldDescriptor.Type.Primitive.Int32(), + jsonName = "requiredInt", + value = pbandk.testpb.Proto3PresenceMessage::requiredInt + ) + ) + } + pbandk.MessageDescriptor( + fullName = "pbandk.testpb.Proto3PresenceMessage", + messageClass = pbandk.testpb.Proto3PresenceMessage::class, + messageCompanion = this, + fields = fieldsList + ) + } + } +} + +@pbandk.Export +public data class Proto3PresenceMain( + val message: pbandk.testpb.Proto3PresenceMessage? = null, + val string: String = "", + val int: Int = 0, + val enum: pbandk.testpb.Proto3PresenceEnum = pbandk.testpb.Proto3PresenceEnum.fromValue(0), + val optionalMessage: pbandk.testpb.Proto3PresenceMessage? = null, + val optionalString: String? = null, + val optionalInt: Int? = null, + val optionalEnum: pbandk.testpb.Proto3PresenceEnum? = null, + val oneOf: OneOf<*>? = null, + override val unknownFields: Map = emptyMap() +) : pbandk.Message { + public sealed class OneOf(value: V) : pbandk.Message.OneOf(value) { + public class OneOfString(oneOfString: String = "") : OneOf(oneOfString) + public class OneOfInt(oneOfInt: Int = 0) : OneOf(oneOfInt) + } + + val oneOfString: String? + get() = (oneOf as? OneOf.OneOfString)?.value + val oneOfInt: Int? + get() = (oneOf as? OneOf.OneOfInt)?.value + + override operator fun plus(other: pbandk.Message?): pbandk.testpb.Proto3PresenceMain = protoMergeImpl(other) + override val descriptor: pbandk.MessageDescriptor get() = Companion.descriptor + override val protoSize: Int by lazy { super.protoSize } + public companion object : pbandk.Message.Companion { + public val defaultInstance: pbandk.testpb.Proto3PresenceMain by lazy { pbandk.testpb.Proto3PresenceMain() } + override fun decodeWith(u: pbandk.MessageDecoder): pbandk.testpb.Proto3PresenceMain = pbandk.testpb.Proto3PresenceMain.decodeWithImpl(u) + + override val descriptor: pbandk.MessageDescriptor by lazy { + val fieldsList = ArrayList>(10) + fieldsList.apply { + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "optional_message", + number = 1, + type = pbandk.FieldDescriptor.Type.Message(messageCompanion = pbandk.testpb.Proto3PresenceMessage.Companion), + jsonName = "optionalMessage", + value = pbandk.testpb.Proto3PresenceMain::optionalMessage + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "message", + number = 2, + type = pbandk.FieldDescriptor.Type.Message(messageCompanion = pbandk.testpb.Proto3PresenceMessage.Companion), + jsonName = "message", + value = pbandk.testpb.Proto3PresenceMain::message + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "optional_string", + number = 3, + type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true), + jsonName = "optionalString", + value = pbandk.testpb.Proto3PresenceMain::optionalString + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "string", + number = 4, + type = pbandk.FieldDescriptor.Type.Primitive.String(), + jsonName = "string", + value = pbandk.testpb.Proto3PresenceMain::string + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "optional_int", + number = 5, + type = pbandk.FieldDescriptor.Type.Primitive.Int32(hasPresence = true), + jsonName = "optionalInt", + value = pbandk.testpb.Proto3PresenceMain::optionalInt + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "int", + number = 6, + type = pbandk.FieldDescriptor.Type.Primitive.Int32(), + jsonName = "int", + value = pbandk.testpb.Proto3PresenceMain::int + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "optional_enum", + number = 7, + type = pbandk.FieldDescriptor.Type.Enum(enumCompanion = pbandk.testpb.Proto3PresenceEnum.Companion, hasPresence = true), + jsonName = "optionalEnum", + value = pbandk.testpb.Proto3PresenceMain::optionalEnum + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "enum", + number = 8, + type = pbandk.FieldDescriptor.Type.Enum(enumCompanion = pbandk.testpb.Proto3PresenceEnum.Companion), + jsonName = "enum", + value = pbandk.testpb.Proto3PresenceMain::enum + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "one_of_string", + number = 9, + type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true), + oneofMember = true, + jsonName = "oneOfString", + value = pbandk.testpb.Proto3PresenceMain::oneOfString + ) + ) + add( + pbandk.FieldDescriptor( + messageDescriptor = this@Companion::descriptor, + name = "one_of_int", + number = 10, + type = pbandk.FieldDescriptor.Type.Primitive.Int32(hasPresence = true), + oneofMember = true, + jsonName = "oneOfInt", + value = pbandk.testpb.Proto3PresenceMain::oneOfInt + ) + ) + } + pbandk.MessageDescriptor( + fullName = "pbandk.testpb.Proto3PresenceMain", + messageClass = pbandk.testpb.Proto3PresenceMain::class, + messageCompanion = this, + fields = fieldsList + ) + } + } +} + +@pbandk.Export +@pbandk.JsName("orDefaultForProto3PresenceMessage") +public fun Proto3PresenceMessage?.orDefault(): pbandk.testpb.Proto3PresenceMessage = this ?: Proto3PresenceMessage.defaultInstance + +private fun Proto3PresenceMessage.protoMergeImpl(plus: pbandk.Message?): Proto3PresenceMessage = (plus as? Proto3PresenceMessage)?.let { + it.copy( + unknownFields = unknownFields + plus.unknownFields + ) +} ?: this + +@Suppress("UNCHECKED_CAST") +private fun Proto3PresenceMessage.Companion.decodeWithImpl(u: pbandk.MessageDecoder): Proto3PresenceMessage { + var requiredInt = 0 + + val unknownFields = u.readMessage(this) { _fieldNumber, _fieldValue -> + when (_fieldNumber) { + 1 -> requiredInt = _fieldValue as Int + } + } + return Proto3PresenceMessage(requiredInt, unknownFields) +} + +@pbandk.Export +@pbandk.JsName("orDefaultForProto3PresenceMain") +public fun Proto3PresenceMain?.orDefault(): pbandk.testpb.Proto3PresenceMain = this ?: Proto3PresenceMain.defaultInstance + +private fun Proto3PresenceMain.protoMergeImpl(plus: pbandk.Message?): Proto3PresenceMain = (plus as? Proto3PresenceMain)?.let { + it.copy( + message = message?.plus(plus.message) ?: plus.message, + optionalMessage = optionalMessage?.plus(plus.optionalMessage) ?: plus.optionalMessage, + optionalString = plus.optionalString ?: optionalString, + optionalInt = plus.optionalInt ?: optionalInt, + optionalEnum = plus.optionalEnum ?: optionalEnum, + oneOf = plus.oneOf ?: oneOf, + unknownFields = unknownFields + plus.unknownFields + ) +} ?: this + +@Suppress("UNCHECKED_CAST") +private fun Proto3PresenceMain.Companion.decodeWithImpl(u: pbandk.MessageDecoder): Proto3PresenceMain { + var message: pbandk.testpb.Proto3PresenceMessage? = null + var string = "" + var int = 0 + var enum: pbandk.testpb.Proto3PresenceEnum = pbandk.testpb.Proto3PresenceEnum.fromValue(0) + var optionalMessage: pbandk.testpb.Proto3PresenceMessage? = null + var optionalString: String? = null + var optionalInt: Int? = null + var optionalEnum: pbandk.testpb.Proto3PresenceEnum? = null + var oneOf: Proto3PresenceMain.OneOf<*>? = null + + val unknownFields = u.readMessage(this) { _fieldNumber, _fieldValue -> + when (_fieldNumber) { + 1 -> optionalMessage = _fieldValue as pbandk.testpb.Proto3PresenceMessage + 2 -> message = _fieldValue as pbandk.testpb.Proto3PresenceMessage + 3 -> optionalString = _fieldValue as String + 4 -> string = _fieldValue as String + 5 -> optionalInt = _fieldValue as Int + 6 -> int = _fieldValue as Int + 7 -> optionalEnum = _fieldValue as pbandk.testpb.Proto3PresenceEnum + 8 -> enum = _fieldValue as pbandk.testpb.Proto3PresenceEnum + 9 -> oneOf = Proto3PresenceMain.OneOf.OneOfString(_fieldValue as String) + 10 -> oneOf = Proto3PresenceMain.OneOf.OneOfInt(_fieldValue as Int) + } + } + return Proto3PresenceMain(message, string, int, enum, + optionalMessage, optionalString, optionalInt, optionalEnum, + oneOf, unknownFields) +} diff --git a/runtime/src/commonTest/proto/pbandk/testpb/proto_3_presence.proto b/runtime/src/commonTest/proto/pbandk/testpb/proto_3_presence.proto new file mode 100644 index 00000000..23880ec7 --- /dev/null +++ b/runtime/src/commonTest/proto/pbandk/testpb/proto_3_presence.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package pbandk.testpb; + +message Proto3PresenceMessage { + int32 requiredInt = 1; +} + +enum Proto3PresenceEnum { + PROTO3_PRESENCE_ENUM_UNSPECIFIED = 0; + PROTO3_PRESENCE_ENUM_SPECIFIED = 1; +} + +message Proto3PresenceMain { + optional Proto3PresenceMessage optional_message = 1; + Proto3PresenceMessage message = 2; + optional string optional_string = 3; + string string = 4; + optional int32 optional_int = 5; + int32 int = 6; + optional Proto3PresenceEnum optional_enum = 7; + Proto3PresenceEnum enum = 8; + oneof oneOf { + string one_of_string = 9; + int32 one_of_int = 10; + } +} diff --git a/runtime/src/jvmTest/kotlin/TestUtils.kt b/runtime/src/jvmTest/kotlin/TestUtils.kt new file mode 100644 index 00000000..96d63800 --- /dev/null +++ b/runtime/src/jvmTest/kotlin/TestUtils.kt @@ -0,0 +1,25 @@ +package pbandk + +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals + +/** Generate a Java version of the proto and deserialize Kotlin version and vice-versa */ +fun kotlinJavaRoundtripTest( + builtJavaObj: com.google.protobuf.Message, + builtKotlinObj: T, + kotlinCompanion: Message.Companion +) { + assertEquals(builtJavaObj.serializedSize, builtKotlinObj.protoSize) + + val builtJavaBytes = builtJavaObj.toByteArray() + val builtKotlinBytes = builtKotlinObj.encodeToByteArray() + assertContentEquals(builtJavaBytes, builtKotlinBytes) + + val gendJavaObj = builtJavaObj.parserForType.parseFrom(builtKotlinBytes) + val gendKotlinObj = kotlinCompanion.decodeFromByteArray(builtJavaBytes) + assertEquals(builtJavaObj, gendJavaObj) + assertEquals(builtKotlinObj, gendKotlinObj) + + assertEquals(builtKotlinObj.protoSize, gendKotlinObj.protoSize) + assertContentEquals(builtKotlinBytes, gendKotlinObj.encodeToByteArray()) +} \ No newline at end of file diff --git a/runtime/src/jvmTest/kotlin/pbandk/JvmProto3PresenceTest.kt b/runtime/src/jvmTest/kotlin/pbandk/JvmProto3PresenceTest.kt new file mode 100644 index 00000000..5165ff77 --- /dev/null +++ b/runtime/src/jvmTest/kotlin/pbandk/JvmProto3PresenceTest.kt @@ -0,0 +1,59 @@ +package pbandk.pbandk + +import org.junit.Test +import pbandk.kotlinJavaRoundtripTest +import pbandk.testpb.Proto3Presence +import pbandk.testpb.Proto3PresenceEnum +import pbandk.testpb.Proto3PresenceMain +import pbandk.testpb.Proto3PresenceMessage +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class JvmProto3PresenceTest { + @Test + fun testProto3Presence() { + val descriptor = Proto3PresenceMain.descriptor + val javaDescriptor = Proto3Presence.Proto3PresenceMain.getDescriptor() + val fieldsPairs = descriptor.fields.map { it to javaDescriptor.fields.find { jit -> jit.name == it.name }!! } + fieldsPairs.forEach { (field, jField) -> + // If instead of assertEquals, to generate more verbose error messages. + if(jField.hasPresence()) { + assertTrue("${field.name} should have presence.") { field.type.hasPresence } + } else { + assertFalse("${field.name} should NOT have presence.") { field.type.hasPresence } + } + } + } + + @Test + fun testDefaults() { + val proto = Proto3PresenceMain() + val jProto = Proto3Presence.Proto3PresenceMain.newBuilder().build() + kotlinJavaRoundtripTest(jProto, proto, Proto3PresenceMain.Companion) + } + + @Test + fun testCustomized() { + val proto = Proto3PresenceMain( + message = Proto3PresenceMessage(1), + optionalMessage = Proto3PresenceMessage(1), + string = "test", + optionalString = "test", + int = 1, + optionalInt = 1, + enum = Proto3PresenceEnum.PROTO3_PRESENCE_ENUM_SPECIFIED, + optionalEnum = Proto3PresenceEnum.PROTO3_PRESENCE_ENUM_SPECIFIED + ) + val jProto = Proto3Presence.Proto3PresenceMain.newBuilder() + .setMessage(Proto3Presence.Proto3PresenceMessage.newBuilder().setRequiredInt(1).build()) + .setOptionalMessage(Proto3Presence.Proto3PresenceMessage.newBuilder().setRequiredInt(1).build()) + .setString("test") + .setOptionalString("test") + .setInt(1) + .setOptionalInt(1) + .setEnum(Proto3Presence.Proto3PresenceEnum.PROTO3_PRESENCE_ENUM_SPECIFIED) + .setOptionalEnum(Proto3Presence.Proto3PresenceEnum.PROTO3_PRESENCE_ENUM_SPECIFIED) + .build() + kotlinJavaRoundtripTest(jProto, proto, Proto3PresenceMain.Companion) + } +} \ No newline at end of file