Search in sources :

Example 21 with ConfigNode

use of com.google.api.codegen.configgen.nodes.ConfigNode in project toolkit by googleapis.

the class MissingFieldTransformerTest method testInsert.

@Test
public void testInsert() throws Exception {
    ConfigNode parent = new FieldConfigNode(0, "parent");
    Map<String, String> fields = ImmutableMap.of("A", "1", "C", "3");
    ConfigNode fieldA = ListTransformer.generateList(fields.entrySet(), parent, (startLine, entry) -> FieldConfigNode.createStringPair(startLine, entry.getKey(), entry.getValue()));
    MissingFieldTransformer.insert("B", parent, fieldA).generate();
    List<String> fieldNames = Arrays.asList("A", "B", "C");
    int index = 0;
    for (ConfigNode node : NodeFinder.getChildren(parent)) {
        Truth.assertThat(node.getText()).isEqualTo(fieldNames.get(index++));
    }
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) Test(org.junit.Test)

Example 22 with ConfigNode

use of com.google.api.codegen.configgen.nodes.ConfigNode in project toolkit by googleapis.

the class ConfigGeneratorApi method generateConfig.

private Map<String, String> generateConfig(String outputPath) {
    ConfigNode node = new ProtoConfigMerger().mergeConfig(model, outputPath);
    ConfigGenerator configGenerator = new ConfigGenerator(0);
    configGenerator.visit(node);
    return ImmutableMap.of(outputPath, configGenerator.toString());
}
Also used : ProtoConfigMerger(com.google.api.codegen.configgen.mergers.ProtoConfigMerger) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode)

Example 23 with ConfigNode

use of com.google.api.codegen.configgen.nodes.ConfigNode in project toolkit by googleapis.

the class ConfigYamlNodeReader method readMessageNode.

private ConfigNode readMessageNode(int prevLine, MappingNode node, Descriptor messageType) {
    ConfigNode configNode = new NullConfigNode();
    ConfigNode prev = new NullConfigNode();
    for (NodeTuple entry : node.getValue()) {
        ConfigNode entryNode = readMessageNodeEntry(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 : 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 24 with ConfigNode

use of com.google.api.codegen.configgen.nodes.ConfigNode 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 25 with ConfigNode

use of com.google.api.codegen.configgen.nodes.ConfigNode in project toolkit by googleapis.

the class ConfigYamlNodeReader method readListNode.

private ConfigNode readListNode(int prevLine, Node node, final FieldDescriptor field) {
    if (isEmpty(node)) {
        return new NullConfigNode();
    }
    if (!(node instanceof SequenceNode)) {
        return new ListItemConfigNode(helper.getStartLine(node)).setChild(readSingularField(prevLine, node, field));
    }
    ConfigNode configNode = new NullConfigNode();
    ConfigNode prev = new NullConfigNode();
    for (Node elem : ((SequenceNode) node).getValue()) {
        ConfigNode elemNode = new ListItemConfigNode(helper.getStartLine(elem)).setChild(readSingularField(prevLine, elem, field));
        ConfigNode commentNode = readCommentNode(prevLine, elem, elemNode);
        prevLine = elem.getEndMark().getLine() + 1;
        if (elemNode == null) {
            continue;
        }
        prev.insertNext(commentNode);
        prev = elemNode;
        if (!configNode.isPresent()) {
            configNode = commentNode;
        }
    }
    return configNode;
}
Also used : ListItemConfigNode(com.google.api.codegen.configgen.nodes.ListItemConfigNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) Node(org.yaml.snakeyaml.nodes.Node) 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) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) 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) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode)

Aggregations

ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)39 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)35 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)22 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)12 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)11 Test (org.junit.Test)5 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)5 DefaultComment (com.google.api.codegen.configgen.nodes.metadata.DefaultComment)4 FixmeComment (com.google.api.codegen.configgen.nodes.metadata.FixmeComment)4 Node (org.yaml.snakeyaml.nodes.Node)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)3 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)3 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)3 ConfigHelper (com.google.api.codegen.configgen.ConfigHelper)2 ConfigYamlReader (com.google.api.codegen.configgen.ConfigYamlReader)2 MessageGenerator (com.google.api.codegen.configgen.MessageGenerator)2 File (java.io.File)2 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)2 FieldModel (com.google.api.codegen.config.FieldModel)1 ProtoConfigMerger (com.google.api.codegen.configgen.mergers.ProtoConfigMerger)1