Search in sources :

Example 1 with ConfigNode

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

the class ConfigYamlNodeReader method readMapNodeEntry.

private ConfigNode readMapNodeEntry(NodeTuple entry, Descriptor messageType) {
    Node keyNode = entry.getKeyNode();
    if (!(keyNode instanceof ScalarNode)) {
        helper.error(keyNode, "Expected a scalar value for key in '%s', found '%s'.", messageType.getFullName(), keyNode.getNodeId());
        return null;
    }
    String key = ((ScalarNode) keyNode).getValue();
    FieldDescriptor valueField = messageType.findFieldByName("value");
    ConfigNode valueConfigNode = readField(keyNode.getEndMark().getLine() + 1, entry.getValueNode(), valueField);
    if (valueConfigNode == null) {
        return null;
    }
    return new FieldConfigNode(helper.getStartLine(keyNode), key).setChild(valueConfigNode);
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) 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) 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) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 2 with ConfigNode

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

the class ConfigYamlNodeReader method readMessageNodeEntry.

private ConfigNode readMessageNodeEntry(NodeTuple entry, Descriptor messageType) {
    Node keyNode = entry.getKeyNode();
    if (!(keyNode instanceof ScalarNode)) {
        helper.error(keyNode, "Expected a scalar value for key in '%s', found '%s'.", messageType.getFullName(), keyNode.getNodeId());
        return null;
    }
    String key = ((ScalarNode) keyNode).getValue();
    FieldDescriptor field = messageType.findFieldByName(key);
    ConfigNode valueConfigNode = null;
    if (field != null) {
        valueConfigNode = readField(keyNode.getEndMark().getLine() + 1, entry.getValueNode(), field);
    } else if (key.equals(TYPE_KEY)) {
        valueConfigNode = readScalarNode(entry.getValueNode(), field);
    } else {
        helper.error(keyNode, "Found field '%s' which is unknown in '%s'.", key, messageType.getFullName());
    }
    if (valueConfigNode == null) {
        return null;
    }
    return new FieldConfigNode(helper.getStartLine(keyNode), key).setChild(valueConfigNode);
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) 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) 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) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 3 with ConfigNode

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

the class ConfigYamlReader method generateConfigNode.

public FieldConfigNode generateConfigNode(File file, ConfigHelper helper) {
    int initialErrorCount = helper.getErrorCount();
    String input;
    try {
        input = Files.toString(file, Charsets.UTF_8);
    } catch (IOException e) {
        helper.error("Cannot read configuration file: %s", e.getMessage());
        return null;
    }
    if (input.trim().isEmpty()) {
        helper.error("Empty YAML document");
        return null;
    }
    Node tree;
    try {
        tree = new Yaml().compose(new StringReader(input));
    } catch (ComposerException e) {
        helper.error(e.getProblemMark(), "Parsing error: %s", e.getMessage());
        return null;
    } catch (Exception e) {
        helper.error("Parsing error: %s", e.getMessage());
        return null;
    }
    if (tree == null) {
        helper.error("Parsing error");
        return null;
    }
    if (!(tree instanceof MappingNode)) {
        helper.error(tree, "Expected a map as a root object.");
        return null;
    }
    List<String> lines = Splitter.on(System.lineSeparator()).splitToList(input);
    ConfigNode configNode = new ConfigYamlNodeReader(lines, helper).readMessageNode((MappingNode) tree, ConfigProto.getDescriptor());
    return helper.getErrorCount() == initialErrorCount ? new FieldConfigNode(0, "").setChild(configNode) : null;
}
Also used : MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) Node(org.yaml.snakeyaml.nodes.Node) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) ComposerException(org.yaml.snakeyaml.composer.ComposerException) IOException(java.io.IOException) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) StringReader(java.io.StringReader) ComposerException(org.yaml.snakeyaml.composer.ComposerException) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode)

Example 4 with ConfigNode

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

the class ListTransformer method generateList.

/**
 * @param elements The data to transform into ConfigNodes
 * @param parentNode The parent of the generated list
 * @param elementTransformer Determines how to transform an individual element
 * @return The head of the list.
 */
public static <T> ConfigNode generateList(Iterable<T> elements, ConfigNode parentNode, ElementTransformer<T> elementTransformer) {
    ConfigNode elementNode = new NullConfigNode();
    ConfigNode prev = null;
    for (T elem : elements) {
        int startLine = NodeFinder.getNextLine(prev == null ? parentNode : prev);
        ConfigNode node = elementTransformer.generateElement(startLine, elem);
        if (node == null) {
            continue;
        }
        if (prev == null) {
            parentNode.setChild(node);
        } else {
            prev.insertNext(node);
        }
        if (!elementNode.isPresent()) {
            elementNode = node;
        }
        prev = node;
    }
    return elementNode;
}
Also used : NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) ListItemConfigNode(com.google.api.codegen.configgen.nodes.ListItemConfigNode) ScalarConfigNode(com.google.api.codegen.configgen.nodes.ScalarConfigNode) NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode)

Example 5 with ConfigNode

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

the class ProtoPageStreamingTransformer method generateResponseValueNode.

@Override
public ConfigNode generateResponseValueNode(ConfigNode parentNode, MethodModel method, ConfigHelper helper) {
    if (!hasResponseTokenField(method)) {
        return new NullConfigNode();
    }
    String resourcesFieldName = getResourcesFieldName(method, helper);
    if (resourcesFieldName == null) {
        return new NullConfigNode();
    }
    ConfigNode tokenFieldNode = FieldConfigNode.createStringPair(NodeFinder.getNextLine(parentNode), "token_field", PAGING_PARAMETERS.getNameForNextPageToken());
    parentNode.setChild(tokenFieldNode);
    ConfigNode resourcesFieldNode = FieldConfigNode.createStringPair(NodeFinder.getNextLine(tokenFieldNode), "resources_field", resourcesFieldName);
    return tokenFieldNode.insertNext(resourcesFieldNode);
}
Also used : NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode)

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