use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class Sample method builderForType.
/**
* Generates a new stub {@link Message.Builder} with all the fields set to
* {@link Random random} values.
*
* <p> All the fields are guaranteed to be not {@code null} and not default.
* Number and {@code boolean} fields may or may not have their default values ({@code 0} and
* {@code false}).
*
* @param clazz Java class of the stub message
* @param <M> type of the required message
* @param <B> type of the {@link Message.Builder} for the message
* @return new instance of the {@link Message.Builder} for given type
* @see #valueFor(FieldDescriptor)
*/
public static <M extends Message, B extends Message.Builder> B builderForType(Class<M> clazz) {
checkClass(clazz);
final B builder = builderFor(clazz);
final Descriptor builderDescriptor = builder.getDescriptorForType();
final Collection<FieldDescriptor> fields = builderDescriptor.getFields();
for (FieldDescriptor field : fields) {
final Object value = valueFor(field);
if (field.isRepeated()) {
builder.addRepeatedField(field, value);
} else {
builder.setField(field, value);
}
}
return builder;
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project toolkit by googleapis.
the class ConfigYamlNodeReader method readMapNode.
private ConfigNode readMapNode(int prevLine, Node node, FieldDescriptor field) {
if (isEmpty(node)) {
return new NullConfigNode();
}
if (!(node instanceof MappingNode)) {
helper.error(node, "Expected a map to merge with '%s', found '%s'.", field.getFullName(), node.getNodeId());
return null;
}
Descriptor messageType = field.getMessageType();
ConfigNode configNode = new NullConfigNode();
ConfigNode prev = new NullConfigNode();
for (NodeTuple entry : ((MappingNode) node).getValue()) {
ConfigNode entryNode = readMapNodeEntry(entry, messageType);
ConfigNode commentNode = readCommentNode(prevLine, entry.getKeyNode(), entryNode);
prevLine = entry.getValueNode().getEndMark().getLine() + 1;
if (entryNode == null) {
continue;
}
prev.insertNext(commentNode);
prev = entryNode;
if (!configNode.isPresent()) {
configNode = commentNode;
}
}
return configNode;
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project toolkit by googleapis.
the class MessageGenerator method generate.
@VisitsBefore
boolean generate(FieldConfigNode node) {
String name = node.getText();
Descriptor messageType = messageBuilder.getDescriptorForType();
if (field != null && field.isMapField()) {
FieldDescriptor keyField = messageType.findFieldByName("key");
if (keyField == null) {
values = null;
return false;
}
messageBuilder.setField(keyField, name);
FieldDescriptor valueField = messageType.findFieldByName("value");
if (valueField == null) {
values = null;
return false;
}
Message.Builder childBuilder = valueField.getType() == FieldDescriptor.Type.MESSAGE ? messageBuilder.newBuilderForField(valueField) : null;
Object value = generateSingularValue(node.getChild(), valueField, childBuilder);
if (value == null) {
values = null;
return false;
}
messageBuilder.setField(valueField, value);
values.add(messageBuilder.build());
messageBuilder.clear();
} else {
if (name.equals(TYPE_KEY) && node.getChild().getText().equals(messageType.getFullName())) {
return true;
}
FieldDescriptor childField = messageType.findFieldByName(name);
if (childField == null) {
messageBuilder = null;
return false;
}
Message.Builder childBuilder = childField.getType() == FieldDescriptor.Type.MESSAGE ? messageBuilder.newBuilderForField(childField) : null;
MessageGenerator messageGenerator = new MessageGenerator(childBuilder, childField);
if (childField.isRepeated() || childField.getType() != FieldDescriptor.Type.MESSAGE) {
messageGenerator.values = new ArrayList<>();
}
messageGenerator.visit(node.getChild());
Object value = messageGenerator.getValue();
if (value == null) {
messageBuilder = null;
return false;
}
if (childField.isRepeated() || childField.getType() == FieldDescriptor.Type.MESSAGE) {
messageBuilder.setField(childField, value);
} else {
List<?> childValues = (List<?>) value;
if (childValues.size() != 1) {
messageBuilder = null;
return false;
}
messageBuilder.setField(childField, childValues.get(0));
}
}
return true;
}
Aggregations