use of com.google.api.tools.framework.util.VisitsBefore 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