com.google.protobuf.Message.hasField()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(398)

本文整理了Java中com.google.protobuf.Message.hasField()方法的一些代码示例,展示了Message.hasField()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.hasField()方法的具体详情如下:
包路径:com.google.protobuf.Message
类名称:Message
方法名:hasField

Message.hasField介绍

暂无

代码示例

代码示例来源:origin: apache/avro

  1. @Override
  2. protected Object getField(Object record, String name, int pos, Object state) {
  3. Message m = (Message)record;
  4. FieldDescriptor f = ((FieldDescriptor[])state)[pos];
  5. switch (f.getType()) {
  6. case MESSAGE:
  7. if (!f.isRepeated() && !m.hasField(f))
  8. return null;
  9. default:
  10. return m.getField(f);
  11. }
  12. }

代码示例来源:origin: SonarSource/sonarqube

  1. private static void writeMessage(Message message, JsonWriter writer) {
  2. MessageType type = MessageType.of(message);
  3. for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) {
  4. if (fieldDescriptor.isRepeated()) {
  5. writer.name(fieldDescriptor.getName());
  6. if (fieldDescriptor.isMapField()) {
  7. writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer);
  8. } else {
  9. writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor));
  10. }
  11. } else if (message.hasField(fieldDescriptor)) {
  12. writer.name(fieldDescriptor.getName());
  13. Object fieldValue = message.getField(fieldDescriptor);
  14. writeFieldValue(fieldDescriptor, fieldValue, writer);
  15. }
  16. }
  17. }

代码示例来源:origin: org.ow2.chameleon.fuchsia.base.protobuffer/org.ow2.chameleon.fuchsia.base.protobuffer.cxf-protobuf

  1. /**
  2. * Find out which field in the incoming message contains the payload that is.
  3. * delivered to the service method.
  4. */
  5. protected FieldDescriptor resolvePayloadField(Message message) {
  6. for (FieldDescriptor field : message.getDescriptorForType().getFields()) {
  7. if (message.hasField(field)) {
  8. return field;
  9. }
  10. }
  11. throw new RuntimeException("No payload found in message " + message);
  12. }

代码示例来源:origin: google/closure-templates

  1. @Override
  2. boolean hasField(Message proto) {
  3. // TODO(lukes): Currently we assume that a field is present if it is repeated, has an
  4. // explicit default value or is set. However, the type of fields is not generally nullable,
  5. // so we can return null for a non-nullable field type. Given the current ToFu implementation
  6. // this is not a problem, but modifying the field types to be nullable for non-repeated fields
  7. // with non explicit defaults should probably happen.
  8. return !shouldCheckFieldPresenceToEmulateJspbNullability() || proto.hasField(getDescriptor());
  9. }
  10. }

代码示例来源:origin: com.google.template/soy

  1. @Override
  2. boolean hasField(Message proto) {
  3. // TODO(lukes): Currently we assume that a field is present if it is repeated, has an
  4. // explicit default value or is set. However, the type of fields is not generally nullable,
  5. // so we can return null for a non-nullable field type. Given the current ToFu implementation
  6. // this is not a problem, but modifying the field types to be nullable for non-repeated fields
  7. // with non explicit defaults should probably happen.
  8. return !shouldCheckFieldPresenceToEmulateJspbNullability() || proto.hasField(getDescriptor());
  9. }
  10. }

代码示例来源:origin: com.twitter.elephantbird/elephant-bird-core

  1. public static boolean isFieldSetByName(Message message, String name) {
  2. return message.hasField(message.getDescriptorForType().findFieldByName(name));
  3. }

代码示例来源:origin: com.google.template/soy

  1. @Override
  2. public SoyValue getProtoField(String name) {
  3. FieldWithInterpreter field = clazz().fields.get(name);
  4. if (field == null) {
  5. throw new IllegalArgumentException(
  6. "Proto " + proto.getClass().getName() + " does not have a field of name " + name);
  7. }
  8. if (field.shouldCheckFieldPresenceToEmulateJspbNullability()
  9. && !proto.hasField(field.getDescriptor())) {
  10. return NullData.INSTANCE;
  11. }
  12. return field.interpretField(proto).resolve();
  13. }

代码示例来源:origin: xyz.codemeans.protobuf4j/protobuf4j-core

  1. @Override
  2. public boolean isFieldSet(T msg, String fieldName) {
  3. Descriptors.FieldDescriptor fd = checkFieldDescriptor(fieldName);
  4. if (fd.isRepeated()) {
  5. return !((Collection<?>) msg.getField(fd)).isEmpty();
  6. }
  7. return msg.hasField(fd);
  8. }

代码示例来源:origin: google/closure-templates

  1. /**
  2. * Gets a value for the field for the underlying proto object. Not intended for general use.
  3. *
  4. * @param name The proto field name.
  5. * @return The value of the given field for the underlying proto object, or NullData if either the
  6. * field does not exist or the value is not set in the underlying proto (according to the jspb
  7. * semantics)
  8. */
  9. public SoyValue getProtoField(String name) {
  10. FieldWithInterpreter field = clazz().fields.get(name);
  11. if (field == null) {
  12. throw new IllegalArgumentException(
  13. "Proto " + proto.getClass().getName() + " does not have a field of name " + name);
  14. }
  15. if (field.shouldCheckFieldPresenceToEmulateJspbNullability()
  16. && !proto.hasField(field.getDescriptor())) {
  17. return NullData.INSTANCE;
  18. }
  19. return field.interpretField(proto);
  20. }

代码示例来源:origin: org.apache.avro/avro-protobuf

  1. @Override
  2. protected Object getField(Object record, String name, int pos, Object state) {
  3. Message m = (Message)record;
  4. FieldDescriptor f = ((FieldDescriptor[])state)[pos];
  5. switch (f.getType()) {
  6. case MESSAGE:
  7. if (!f.isRepeated() && !m.hasField(f))
  8. return null;
  9. default:
  10. return m.getField(f);
  11. }
  12. }

代码示例来源:origin: com.truward.brikar.protobuf/brikar-protobuf-jackson

  1. public static void writeJson(@Nonnull Message message, @Nonnull JsonGenerator jg) throws IOException {
  2. final Descriptors.Descriptor descriptor = message.getDescriptorForType();
  3. jg.writeStartObject();
  4. for (final Descriptors.FieldDescriptor fieldDescriptor : descriptor.getFields()) {
  5. if (!(fieldDescriptor.isRepeated() || fieldDescriptor.isRequired()) && !message.hasField(fieldDescriptor)) {
  6. continue;
  7. }
  8. jg.writeFieldName(fieldDescriptor.getName());
  9. final Object value = message.getField(fieldDescriptor);
  10. if (!writeValue(value, jg)) {
  11. throw new IOException("Unable to serialize field '" + fieldDescriptor.getName() + "' in " + message +
  12. ": unhandled field value");
  13. }
  14. }
  15. jg.writeEndObject();
  16. }

代码示例来源:origin: FoundationDB/fdb-record-layer

  1. @Nonnull
  2. @Override
  3. protected Message getUnionField(@Nonnull Descriptors.Descriptor unionDescriptor,
  4. @Nonnull Message storedRecord) {
  5. final List<Descriptors.OneofDescriptor> oneofs = unionDescriptor.getOneofs();
  6. if (!oneofs.isEmpty()) {
  7. Descriptors.FieldDescriptor unionField = storedRecord.getOneofFieldDescriptor(oneofs.get(0));
  8. if (unionField != null) {
  9. return (Message)storedRecord.getField(unionField);
  10. }
  11. } else {
  12. for (Descriptors.FieldDescriptor unionField : unionDescriptor.getFields()) {
  13. if (storedRecord.hasField(unionField)) {
  14. return (Message)storedRecord.getField(unionField);
  15. }
  16. }
  17. }
  18. throw new RecordSerializationException("Union record does not have any fields")
  19. .addLogInfo("unionDescriptorFullName", unionDescriptor.getFullName())
  20. .addLogInfo("recordType", storedRecord.getDescriptorForType().getName());
  21. }

代码示例来源:origin: com.google.api/api-compiler

  1. if (!proto3 && !messageToMergeForm.hasField(fieldInLocationsToMerge)) {
  2. configBuilder.clearField(fieldInLocationsToMerge);

代码示例来源:origin: googleapis/api-compiler

  1. if (!proto3 && !messageToMergeForm.hasField(fieldInLocationsToMerge)) {
  2. configBuilder.clearField(fieldInLocationsToMerge);

代码示例来源:origin: cloudera-labs/envelope

  1. /**
  2. * Retrieves and converts Protobuf fields from a Message.
  3. * <p>
  4. * If the field in the {@link com.google.protobuf.Descriptors.Descriptor} exists in the {@link Message}, the value is
  5. * retrieved and converted using {@link #getFieldValue(Descriptors.FieldDescriptor, Object, DataType)}.
  6. * Otherwise, the field value is {@code null}.
  7. * The extraction honors the order of the {@code Descriptor}.
  8. *
  9. * @param dsc the Protobuf Descriptor with all fields
  10. * @param msg the Message with the current field values
  11. * @param schema the Dataset schema derived from the Descriptor
  12. * @return a list of converted values
  13. */
  14. public static List<Object> buildRowValues(Descriptors.Descriptor dsc, Message msg, StructType schema) {
  15. List<Object> values = new ArrayList<>();
  16. Object val;
  17. for (Descriptors.FieldDescriptor fd : dsc.getFields()) {
  18. if ( (!fd.isRepeated() && msg.hasField(fd)) || (fd.isRepeated() && msg.getRepeatedFieldCount(fd) > 0) ) {
  19. val = getFieldValue(fd, msg.getField(fd), schema.apply(fd.getName()).dataType());
  20. } else {
  21. LOG.trace("FieldDescriptor[{}] => not found", fd.getFullName());
  22. val = null;
  23. }
  24. values.add(val);
  25. }
  26. return values;
  27. }

代码示例来源:origin: FoundationDB/fdb-record-layer

  1. @Nonnull
  2. @Override
  3. public <M extends Message> List<Key.Evaluated> evaluateFunction(@Nullable FDBRecord<M> record,
  4. @Nullable Message message,
  5. @Nonnull Key.Evaluated arguments) {
  6. if (message == null) {
  7. return Collections.emptyList();
  8. }
  9. List<Key.Evaluated> keys = new ArrayList<>();
  10. Descriptors.Descriptor descriptor = message.getDescriptorForType();
  11. Descriptors.FieldDescriptor strField = descriptor.findFieldByNumber(TypesRecord.STR_VALUE_FIELD_NUMBER);
  12. Descriptors.FieldDescriptor strListField = descriptor.findFieldByNumber(TypesRecord.STR_LIST_VALUE_FIELD_NUMBER);
  13. if (message.hasField(strField)) {
  14. keys.add(toKey((String) message.getField(strField)));
  15. }
  16. final int len = message.getRepeatedFieldCount(strListField);
  17. for (int i = 0; i < len; i++) {
  18. keys.add(toKey((String) message.getRepeatedField(strListField, i)));
  19. }
  20. return keys;
  21. }

代码示例来源:origin: com.google.protobuf/protobuf-java-util

  1. continue;
  2. if (!source.hasField(field) && !destination.hasField(field)) {
  3. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
  4. if (options.replaceMessageFields()) {
  5. if (!source.hasField(field)) {
  6. destination.clearField(field);
  7. } else {
  8. if (source.hasField(field)) {
  9. destination.getFieldBuilder(field).mergeFrom((Message) source.getField(field));
  10. if (source.hasField(field) || !options.replacePrimitiveFields()) {
  11. destination.setField(field, source.getField(field));
  12. } else {

代码示例来源:origin: FoundationDB/fdb-record-layer

  1. throw new RecordCoreException(String.format("unknown fan type: %s", fanType));
  2. } else if (fieldDescriptor != null && (nullStandin == Key.Evaluated.NullStandin.NOT_NULL || message.hasField(fieldDescriptor))) {
  3. Object value = message.getField(fieldDescriptor);
  4. if (fieldDescriptor.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE &&

代码示例来源:origin: org.sonarsource.sonarqube/sonar-core

  1. private static void writeMessage(Message message, JsonWriter writer) {
  2. MessageType type = MessageType.of(message);
  3. for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) {
  4. if (fieldDescriptor.isRepeated()) {
  5. writer.name(fieldDescriptor.getName());
  6. if (fieldDescriptor.isMapField()) {
  7. writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer);
  8. } else {
  9. writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor));
  10. }
  11. } else if (message.hasField(fieldDescriptor)) {
  12. writer.name(fieldDescriptor.getName());
  13. Object fieldValue = message.getField(fieldDescriptor);
  14. writeFieldValue(fieldDescriptor, fieldValue, writer);
  15. }
  16. }
  17. }

代码示例来源:origin: org.openbase.bco/dal.lib

  1. continue;
  2. if (value.hasField(field) && requestedState.hasField(field) && !(value.getField(field).equals(requestedState.getField(field)))) {
  3. equalFields = false;
  4. break;

相关文章