本文整理了Java中com.google.protobuf.Message.hasField()
方法的一些代码示例,展示了Message.hasField()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.hasField()
方法的具体详情如下:
包路径:com.google.protobuf.Message
类名称:Message
方法名:hasField
暂无
代码示例来源:origin: apache/avro
@Override
protected Object getField(Object record, String name, int pos, Object state) {
Message m = (Message)record;
FieldDescriptor f = ((FieldDescriptor[])state)[pos];
switch (f.getType()) {
case MESSAGE:
if (!f.isRepeated() && !m.hasField(f))
return null;
default:
return m.getField(f);
}
}
代码示例来源:origin: SonarSource/sonarqube
private static void writeMessage(Message message, JsonWriter writer) {
MessageType type = MessageType.of(message);
for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) {
if (fieldDescriptor.isRepeated()) {
writer.name(fieldDescriptor.getName());
if (fieldDescriptor.isMapField()) {
writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer);
} else {
writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor));
}
} else if (message.hasField(fieldDescriptor)) {
writer.name(fieldDescriptor.getName());
Object fieldValue = message.getField(fieldDescriptor);
writeFieldValue(fieldDescriptor, fieldValue, writer);
}
}
}
/**
* Find out which field in the incoming message contains the payload that is.
* delivered to the service method.
*/
protected FieldDescriptor resolvePayloadField(Message message) {
for (FieldDescriptor field : message.getDescriptorForType().getFields()) {
if (message.hasField(field)) {
return field;
}
}
throw new RuntimeException("No payload found in message " + message);
}
代码示例来源:origin: google/closure-templates
@Override
boolean hasField(Message proto) {
// TODO(lukes): Currently we assume that a field is present if it is repeated, has an
// explicit default value or is set. However, the type of fields is not generally nullable,
// so we can return null for a non-nullable field type. Given the current ToFu implementation
// this is not a problem, but modifying the field types to be nullable for non-repeated fields
// with non explicit defaults should probably happen.
return !shouldCheckFieldPresenceToEmulateJspbNullability() || proto.hasField(getDescriptor());
}
}
代码示例来源:origin: com.google.template/soy
@Override
boolean hasField(Message proto) {
// TODO(lukes): Currently we assume that a field is present if it is repeated, has an
// explicit default value or is set. However, the type of fields is not generally nullable,
// so we can return null for a non-nullable field type. Given the current ToFu implementation
// this is not a problem, but modifying the field types to be nullable for non-repeated fields
// with non explicit defaults should probably happen.
return !shouldCheckFieldPresenceToEmulateJspbNullability() || proto.hasField(getDescriptor());
}
}
代码示例来源:origin: com.twitter.elephantbird/elephant-bird-core
public static boolean isFieldSetByName(Message message, String name) {
return message.hasField(message.getDescriptorForType().findFieldByName(name));
}
代码示例来源:origin: com.google.template/soy
@Override
public SoyValue getProtoField(String name) {
FieldWithInterpreter field = clazz().fields.get(name);
if (field == null) {
throw new IllegalArgumentException(
"Proto " + proto.getClass().getName() + " does not have a field of name " + name);
}
if (field.shouldCheckFieldPresenceToEmulateJspbNullability()
&& !proto.hasField(field.getDescriptor())) {
return NullData.INSTANCE;
}
return field.interpretField(proto).resolve();
}
代码示例来源:origin: xyz.codemeans.protobuf4j/protobuf4j-core
@Override
public boolean isFieldSet(T msg, String fieldName) {
Descriptors.FieldDescriptor fd = checkFieldDescriptor(fieldName);
if (fd.isRepeated()) {
return !((Collection<?>) msg.getField(fd)).isEmpty();
}
return msg.hasField(fd);
}
代码示例来源:origin: google/closure-templates
/**
* Gets a value for the field for the underlying proto object. Not intended for general use.
*
* @param name The proto field name.
* @return The value of the given field for the underlying proto object, or NullData if either the
* field does not exist or the value is not set in the underlying proto (according to the jspb
* semantics)
*/
public SoyValue getProtoField(String name) {
FieldWithInterpreter field = clazz().fields.get(name);
if (field == null) {
throw new IllegalArgumentException(
"Proto " + proto.getClass().getName() + " does not have a field of name " + name);
}
if (field.shouldCheckFieldPresenceToEmulateJspbNullability()
&& !proto.hasField(field.getDescriptor())) {
return NullData.INSTANCE;
}
return field.interpretField(proto);
}
代码示例来源:origin: org.apache.avro/avro-protobuf
@Override
protected Object getField(Object record, String name, int pos, Object state) {
Message m = (Message)record;
FieldDescriptor f = ((FieldDescriptor[])state)[pos];
switch (f.getType()) {
case MESSAGE:
if (!f.isRepeated() && !m.hasField(f))
return null;
default:
return m.getField(f);
}
}
代码示例来源:origin: com.truward.brikar.protobuf/brikar-protobuf-jackson
public static void writeJson(@Nonnull Message message, @Nonnull JsonGenerator jg) throws IOException {
final Descriptors.Descriptor descriptor = message.getDescriptorForType();
jg.writeStartObject();
for (final Descriptors.FieldDescriptor fieldDescriptor : descriptor.getFields()) {
if (!(fieldDescriptor.isRepeated() || fieldDescriptor.isRequired()) && !message.hasField(fieldDescriptor)) {
continue;
}
jg.writeFieldName(fieldDescriptor.getName());
final Object value = message.getField(fieldDescriptor);
if (!writeValue(value, jg)) {
throw new IOException("Unable to serialize field '" + fieldDescriptor.getName() + "' in " + message +
": unhandled field value");
}
}
jg.writeEndObject();
}
代码示例来源:origin: FoundationDB/fdb-record-layer
@Nonnull
@Override
protected Message getUnionField(@Nonnull Descriptors.Descriptor unionDescriptor,
@Nonnull Message storedRecord) {
final List<Descriptors.OneofDescriptor> oneofs = unionDescriptor.getOneofs();
if (!oneofs.isEmpty()) {
Descriptors.FieldDescriptor unionField = storedRecord.getOneofFieldDescriptor(oneofs.get(0));
if (unionField != null) {
return (Message)storedRecord.getField(unionField);
}
} else {
for (Descriptors.FieldDescriptor unionField : unionDescriptor.getFields()) {
if (storedRecord.hasField(unionField)) {
return (Message)storedRecord.getField(unionField);
}
}
}
throw new RecordSerializationException("Union record does not have any fields")
.addLogInfo("unionDescriptorFullName", unionDescriptor.getFullName())
.addLogInfo("recordType", storedRecord.getDescriptorForType().getName());
}
代码示例来源:origin: com.google.api/api-compiler
if (!proto3 && !messageToMergeForm.hasField(fieldInLocationsToMerge)) {
configBuilder.clearField(fieldInLocationsToMerge);
代码示例来源:origin: googleapis/api-compiler
if (!proto3 && !messageToMergeForm.hasField(fieldInLocationsToMerge)) {
configBuilder.clearField(fieldInLocationsToMerge);
代码示例来源:origin: cloudera-labs/envelope
/**
* Retrieves and converts Protobuf fields from a Message.
* <p>
* If the field in the {@link com.google.protobuf.Descriptors.Descriptor} exists in the {@link Message}, the value is
* retrieved and converted using {@link #getFieldValue(Descriptors.FieldDescriptor, Object, DataType)}.
* Otherwise, the field value is {@code null}.
* The extraction honors the order of the {@code Descriptor}.
*
* @param dsc the Protobuf Descriptor with all fields
* @param msg the Message with the current field values
* @param schema the Dataset schema derived from the Descriptor
* @return a list of converted values
*/
public static List<Object> buildRowValues(Descriptors.Descriptor dsc, Message msg, StructType schema) {
List<Object> values = new ArrayList<>();
Object val;
for (Descriptors.FieldDescriptor fd : dsc.getFields()) {
if ( (!fd.isRepeated() && msg.hasField(fd)) || (fd.isRepeated() && msg.getRepeatedFieldCount(fd) > 0) ) {
val = getFieldValue(fd, msg.getField(fd), schema.apply(fd.getName()).dataType());
} else {
LOG.trace("FieldDescriptor[{}] => not found", fd.getFullName());
val = null;
}
values.add(val);
}
return values;
}
代码示例来源:origin: FoundationDB/fdb-record-layer
@Nonnull
@Override
public <M extends Message> List<Key.Evaluated> evaluateFunction(@Nullable FDBRecord<M> record,
@Nullable Message message,
@Nonnull Key.Evaluated arguments) {
if (message == null) {
return Collections.emptyList();
}
List<Key.Evaluated> keys = new ArrayList<>();
Descriptors.Descriptor descriptor = message.getDescriptorForType();
Descriptors.FieldDescriptor strField = descriptor.findFieldByNumber(TypesRecord.STR_VALUE_FIELD_NUMBER);
Descriptors.FieldDescriptor strListField = descriptor.findFieldByNumber(TypesRecord.STR_LIST_VALUE_FIELD_NUMBER);
if (message.hasField(strField)) {
keys.add(toKey((String) message.getField(strField)));
}
final int len = message.getRepeatedFieldCount(strListField);
for (int i = 0; i < len; i++) {
keys.add(toKey((String) message.getRepeatedField(strListField, i)));
}
return keys;
}
代码示例来源:origin: com.google.protobuf/protobuf-java-util
continue;
if (!source.hasField(field) && !destination.hasField(field)) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
if (options.replaceMessageFields()) {
if (!source.hasField(field)) {
destination.clearField(field);
} else {
if (source.hasField(field)) {
destination.getFieldBuilder(field).mergeFrom((Message) source.getField(field));
if (source.hasField(field) || !options.replacePrimitiveFields()) {
destination.setField(field, source.getField(field));
} else {
代码示例来源:origin: FoundationDB/fdb-record-layer
throw new RecordCoreException(String.format("unknown fan type: %s", fanType));
} else if (fieldDescriptor != null && (nullStandin == Key.Evaluated.NullStandin.NOT_NULL || message.hasField(fieldDescriptor))) {
Object value = message.getField(fieldDescriptor);
if (fieldDescriptor.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE &&
代码示例来源:origin: org.sonarsource.sonarqube/sonar-core
private static void writeMessage(Message message, JsonWriter writer) {
MessageType type = MessageType.of(message);
for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) {
if (fieldDescriptor.isRepeated()) {
writer.name(fieldDescriptor.getName());
if (fieldDescriptor.isMapField()) {
writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer);
} else {
writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor));
}
} else if (message.hasField(fieldDescriptor)) {
writer.name(fieldDescriptor.getName());
Object fieldValue = message.getField(fieldDescriptor);
writeFieldValue(fieldDescriptor, fieldValue, writer);
}
}
}
代码示例来源:origin: org.openbase.bco/dal.lib
continue;
if (value.hasField(field) && requestedState.hasField(field) && !(value.getField(field).equals(requestedState.getField(field)))) {
equalFields = false;
break;
内容来源于网络,如有侵权,请联系作者删除!