Search in sources :

Example 1 with VisitsBefore

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;
}
Also used : Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) VisitsBefore(com.google.api.tools.framework.util.VisitsBefore)

Aggregations

VisitsBefore (com.google.api.tools.framework.util.VisitsBefore)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 Message (com.google.protobuf.Message)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1