Java Code Examples for com.google.protobuf.Descriptors.FieldDescriptor#isRequired()

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#isRequired() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomProtobufGenerator.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a random protocol buffer, filling in all required fields but
 * with a p chance of not setting an optional field and p chance of having
 * an empty repeated field.
 */
@SuppressWarnings("unchecked")
public E generate(double p) {
  Builder builder = instance.newBuilderForType();
  Descriptor descriptor = instance.getDescriptorForType();
  for (FieldDescriptor field : descriptor.getFields()) {
    if (!field.isRequired() && random.nextDouble() < p) {
      continue;
    }
    builder.setField(field, getRandomValue(field, p));
  }
  return (E) builder.build();
}
 
Example 2
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) &&
            !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 3
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 4
Source File: DynamicMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static boolean isInitialized(Descriptor type,
                                     FieldSet<FieldDescriptor> fields) {
  // Check that all required fields are present.
  for (final FieldDescriptor field : type.getFields()) {
    if (field.isRequired()) {
      if (!fields.hasField(field)) {
        return false;
      }
    }
  }

  // Check that embedded messages are initialized.
  return fields.isInitialized();
}
 
Example 5
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) &&
            !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 6
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 7
Source File: DynamicMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static boolean isInitialized(Descriptor type,
                                     FieldSet<FieldDescriptor> fields) {
  // Check that all required fields are present.
  for (final FieldDescriptor field : type.getFields()) {
    if (field.isRequired()) {
      if (!fields.hasField(field)) {
        return false;
      }
    }
  }

  // Check that embedded messages are initialized.
  return fields.isInitialized();
}
 
Example 8
Source File: RandomProtobufGenerator.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a random protocol buffer, filling in all required fields but
 * with a p chance of not setting an optional field and p chance of having
 * an empty repeated field.
 */
@SuppressWarnings("unchecked")
public E generate(double p) {
  Builder builder = instance.newBuilderForType();
  Descriptor descriptor = instance.getDescriptorForType();
  for (FieldDescriptor field : descriptor.getFields()) {
    if (!field.isRequired() && random.nextDouble() < p) {
      continue;
    }
    builder.setField(field, getRandomValue(field, p));
  }
  return (E) builder.build();
}
 
Example 9
Source File: ProtoSchemaConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private Type.Repetition getRepetition(FieldDescriptor descriptor) {
  if (descriptor.isRequired()) {
    return Type.Repetition.REQUIRED;
  } else if (descriptor.isRepeated()) {
    return Type.Repetition.REPEATED;
  } else {
    return Type.Repetition.OPTIONAL;
  }
}
 
Example 10
Source File: MetaDataEvolutionValidator.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
private void validateField(@Nonnull FieldDescriptor oldFieldDescriptor, @Nonnull FieldDescriptor newFieldDescriptor,
                           @Nonnull Set<Pair<Descriptor, Descriptor>> seenDescriptors) {
    if (!oldFieldDescriptor.getName().equals(newFieldDescriptor.getName())) {
        // TODO: Field renaming should be allowed with some caveats about if the field is indexed or not
        throw new MetaDataException("field renamed",
                LogMessageKeys.OLD_FIELD_NAME, oldFieldDescriptor.getName(),
                LogMessageKeys.NEW_FIELD_NAME, newFieldDescriptor.getName());
    }
    if (!oldFieldDescriptor.getType().equals(newFieldDescriptor.getType())) {
        validateTypeChange(oldFieldDescriptor, newFieldDescriptor);
    }
    if (oldFieldDescriptor.isRequired() && !newFieldDescriptor.isRequired()) {
        throw new MetaDataException("required field is no longer required",
                LogMessageKeys.FIELD_NAME, oldFieldDescriptor.getName());
    } else if (oldFieldDescriptor.isOptional() && !newFieldDescriptor.isOptional()) {
        // TODO: In theory, optional -> repeated is okay, but only if the field is not indexed
        throw new MetaDataException("optional field is no longer optional",
                LogMessageKeys.FIELD_NAME, oldFieldDescriptor.getName());
    } else if (oldFieldDescriptor.isRepeated() && !newFieldDescriptor.isRepeated()) {
        throw new MetaDataException("repeated field is no longer repeated",
                LogMessageKeys.FIELD_NAME, oldFieldDescriptor.getName());
    }
    if (oldFieldDescriptor.getType().equals(FieldDescriptor.Type.ENUM)) {
        validateEnum(newFieldDescriptor.getName(), oldFieldDescriptor.getEnumType(), newFieldDescriptor.getEnumType());
    }
    if (oldFieldDescriptor.getType().equals(FieldDescriptor.Type.GROUP) || oldFieldDescriptor.getType().equals(FieldDescriptor.Type.MESSAGE)) {
        // Message types need to be validated against each other as well.
        final Descriptor oldMessageType = oldFieldDescriptor.getMessageType();
        final Descriptor newMessageType = newFieldDescriptor.getMessageType();
        validateMessage(oldMessageType, newMessageType, seenDescriptors);
    }
}
 
Example 11
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) &&
            !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 12
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 13
Source File: DynamicMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
static boolean isInitialized(Descriptor type,
                             FieldSet<FieldDescriptor> fields) {
  // Check that all required fields are present.
  for (final FieldDescriptor field : type.getFields()) {
    if (field.isRequired()) {
      if (!fields.hasField(field)) {
        return false;
      }
    }
  }

  // Check that embedded messages are initialized.
  return fields.isInitialized();
}
 
Example 14
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) &&
            !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 15
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 16
Source File: DynamicMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
private static boolean isInitialized(Descriptor type,
                                     FieldSet<FieldDescriptor> fields) {
  // Check that all required fields are present.
  for (final FieldDescriptor field : type.getFields()) {
    if (field.isRequired()) {
      if (!fields.hasField(field)) {
        return false;
      }
    }
  }

  // Check that embedded messages are initialized.
  return fields.isInitialized();
}
 
Example 17
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) &&
            !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 18
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 19
Source File: DynamicMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
static boolean isInitialized(Descriptor type,
                             FieldSet<FieldDescriptor> fields) {
  // Check that all required fields are present.
  for (final FieldDescriptor field : type.getFields()) {
    if (field.isRequired()) {
      if (!fields.hasField(field)) {
        return false;
      }
    }
  }

  // Check that embedded messages are initialized.
  return fields.isInitialized();
}
 
Example 20
Source File: ProtoGenerator.java    From fuchsia with Apache License 2.0 4 votes vote down vote up
private void generateProtoFromDescriptor(FieldDescriptor descriptor,
                                         Appendable out, String indent, Map<Descriptor, Boolean> descriptors)
        throws IOException {
    out.append(indent);
    if (descriptor.isRequired()) {
        out.append("required ");
    }

    if (descriptor.isOptional()) {
        out.append("optional ");
    }

    if (descriptor.isRepeated()) {
        out.append("repeated ");
    }

    if (descriptor.getType().equals(Type.MESSAGE)) {
        out.append(descriptor.getMessageType().getFullName() + " ");
        Descriptor messageType = descriptor.getMessageType();
        if (descriptors.get(messageType) == null) {
            descriptors.put(messageType, false);
        }
    } else if (descriptor.getType().equals(Type.ENUM)) {
        out.append(descriptor.getEnumType().getFullName() + " ");
    } else {
        out.append(descriptor.getType().toString().toLowerCase() + " ");
    }

    out.append(descriptor.getName() + " = " + descriptor.getNumber());

    if (descriptor.hasDefaultValue()) {
        out.append(" [default = ");
        Object defaultValue = descriptor.getDefaultValue();

        if (defaultValue instanceof EnumValueDescriptor) {
            out.append(((EnumValueDescriptor) defaultValue).getName());
        }

        out.append("]");
    }

    out.append(";\n");
}