Search in sources :

Example 56 with FieldDescriptor

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;
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 57 with FieldDescriptor

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;
}
Also used : MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) ListItemConfigNode(com.google.api.codegen.configgen.nodes.ListItemConfigNode) ScalarConfigNode(com.google.api.codegen.configgen.nodes.ScalarConfigNode) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Example 58 with FieldDescriptor

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;
}
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

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)58 Descriptor (com.google.protobuf.Descriptors.Descriptor)22 Test (org.junit.Test)17 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)11 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 BooleanFieldValidator (io.spine.validate.BooleanFieldValidator)9 ByteStringFieldValidator (io.spine.validate.ByteStringFieldValidator)9 FloatFieldValidator (io.spine.validate.FloatFieldValidator)9 LongFieldValidator (io.spine.validate.LongFieldValidator)9 StringFieldValidator (io.spine.validate.StringFieldValidator)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 TypicalData (protos.TypicalData)5 Collection (java.util.Collection)4 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)3 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)3 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)3 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)3 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)3 ByteString (com.google.protobuf.ByteString)3