@Override public Builder mergeUnknownFields(UnknownFieldSet unknownFields) { if (getDescriptorForType().getFile().getSyntax() == Descriptors.FileDescriptor.Syntax.PROTO3 && CodedInputStream.getProto3DiscardUnknownFieldsDefault()) { return this; } this.unknownFields = UnknownFieldSet.newBuilder(this.unknownFields) .mergeFrom(unknownFields) .build(); return this; }
@Override void validate(FieldDescriptor fieldDescriptor) { checkArgument( !fieldDescriptor.isRepeated(), "%s is a repeated field; repeated fields cannot be absent, only empty", fieldDescriptor); checkArgument( fieldDescriptor.getContainingType().getFile().getSyntax() != Syntax.PROTO3 || fieldDescriptor.getJavaType() == JavaType.MESSAGE, "%s is a primitive field in a Proto 3 message; it cannot be absent", fieldDescriptor); } },
throws IOException { boolean discardUnknown = getDescriptorForType().getFile().getSyntax() == Syntax.PROTO3 ? input.shouldDiscardUnknownFieldsProto3() : input.shouldDiscardUnknownFields();
private static boolean supportFieldPresence(FileDescriptor file) { return file.getSyntax() == FileDescriptor.Syntax.PROTO2; }
/** * Returns whether or not we should check for presence to emulate jspb nullability semantics in * server side soy. */ static boolean shouldCheckFieldPresenceToEmulateJspbNullability(FieldDescriptor desc) { boolean hasBrokenSemantics = false; if (desc.hasDefaultValue() || desc.isRepeated()) { return false; } else if (desc.getFile().getSyntax() == Syntax.PROTO3 || !hasBrokenSemantics) { // in proto3 or proto2 with non-broken semantics we only need to check for presence for // message typed fields. return desc.getJavaType() == JavaType.MESSAGE; } else { return true; } }
boolean supportsUnknownEnumValue() { return getSyntax() == Syntax.PROTO3; } }
/** * Proto3 enums fields can accept and return unknown values via the get<Field>Value() methods, we * use those methods instead of the methods that deal with the enum constants in order to support * unknown enum values. If we didn't, any field with an unknown enum value would throw an * exception when we call {@code getNumber()} on the enum. * * <p>For comparison, in proto2 unknown values always get mapped to 0, so this problem doesn't * exist. Also, in proto2, the 'Value' functions don't exist, so we can't use them. */ private static boolean isProto3EnumField(FieldDescriptor descriptor) { return descriptor.getType() == Descriptors.FieldDescriptor.Type.ENUM && descriptor.getFile().getSyntax() == Syntax.PROTO3; }
/** * Proto3 enums fields can accept and return unknown values via the get<Field>Value() methods, we * use those methods instead of the methods that deal with the enum constants in order to support * unknown enum values. If we didn't, any field with an unknown enum value would throw an * exception when we call {@code getNumber()} on the enum. * * <p>For comparison, in proto2 unknown values always get mapped to 0, so this problem doesn't * exist. Also, in proto2, the 'Value' functions don't exist, so we can't use them. */ private static boolean isProto3EnumField(FieldDescriptor descriptor) { return descriptor.getType() == Descriptors.FieldDescriptor.Type.ENUM && descriptor.getFile().getSyntax() == Syntax.PROTO3; }
@Override public Builder mergeUnknownFields(UnknownFieldSet unknownFields) { if (getDescriptorForType().getFile().getSyntax() == Descriptors.FileDescriptor.Syntax.PROTO3) { // Proto3 discards unknown fields. return this; } this.unknownFields = UnknownFieldSet.newBuilder(this.unknownFields) .mergeFrom(unknownFields) .build(); return this; }
@Override public Builder setUnknownFields(UnknownFieldSet unknownFields) { if (getDescriptorForType().getFile().getSyntax() == Descriptors.FileDescriptor.Syntax.PROTO3) { // Proto3 discards unknown fields. return this; } this.unknownFields = unknownFields; return this; }
@Override Object protoFromSoy(SoyValue field) { // The proto reflection api wants the EnumValueDescriptor, not the actual enum instance int value = field.integerValue(); // in proto3 we preserve unknown enum values (for consistency with jbcsrc), but for proto2 // we don't, and so if the field is unknown we will return null which will trigger an NPE // again, for consistency with jbcsrc. if (enumDescriptor.getFile().getSyntax() == Syntax.PROTO3) { return enumDescriptor.findValueByNumberCreatingIfUnknown(value); } return enumDescriptor.findValueByNumber(value); } };
@Override Object protoFromSoy(SoyValue field) { // The proto reflection api wants the EnumValueDescriptor, not the actual enum instance int value = field.integerValue(); // in proto3 we preserve unknown enum values (for consistency with jbcsrc), but for proto2 // we don't, and so if the field is unknown we will return null which will trigger an NPE // again, for consistency with jbcsrc. if (enumDescriptor.getFile().getSyntax() == Syntax.PROTO3) { return enumDescriptor.findValueByNumberCreatingIfUnknown(value); } return enumDescriptor.findValueByNumber(value); } };
!fieldDescriptor.isRepeated() && fieldDescriptor.getContainingOneof() == null && fieldDescriptor.getFile().getSyntax() == Syntax.PROTO3; boolean ignoreFieldAbsence = isNonRepeatedProto3
} else if (field.getFile().getSyntax() == Descriptors.FileDescriptor.Syntax.PROTO3) { if (!field.isRepeated() && field.getJavaType() != FieldDescriptor.JavaType.MESSAGE
private static boolean supportFieldPresence(FileDescriptor file) { return file.getSyntax() == FileDescriptor.Syntax.PROTO2; }
private static boolean supportFieldPresence(FileDescriptor file) { return file.getSyntax() == FileDescriptor.Syntax.PROTO2; }
/** Does this field have the {@code [packed = true]} option or is this field * packable in proto3 and not explicitly setted to unpacked? */ @Override public boolean isPacked() { if (!isPackable()) { return false; } if (getFile().getSyntax() == FileDescriptor.Syntax.PROTO2) { return getOptions().getPacked(); } else { return !getOptions().hasPacked() || getOptions().getPacked(); } }
/** For internal use only. */ public boolean needsUtf8Check() { if (type != Type.STRING) { return false; } if (getContainingType().getOptions().getMapEntry()) { // Always enforce strict UTF-8 checking for map fields. return true; } if (getFile().getSyntax() == Syntax.PROTO3) { return true; } return getFile().getOptions().getJavaStringCheckUtf8(); }
boolean supportsUnknownEnumValue() { return getSyntax() == Syntax.PROTO3; } }
@Override public Builder setUnknownFields(UnknownFieldSet unknownFields) { if (getDescriptorForType().getFile().getSyntax() == Descriptors.FileDescriptor.Syntax.PROTO3 && CodedInputStream.getProto3DiscardUnknownFieldsDefault()) { return this; } this.unknownFields = unknownFields; return this; }