Search in sources :

Example 1 with FieldConfigNode

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

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

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

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

the class ConfigMerger method mergeVersion.

private ConfigNode mergeVersion(ConfigNode configNode, ConfigNode prevNode) {
    FieldConfigNode versionNode = MissingFieldTransformer.insert("config_schema_version", configNode, prevNode).generate();
    if (!NodeFinder.hasContent(versionNode.getChild())) {
        return versionNode.setChild(new ScalarConfigNode(versionNode.getStartLine(), CONFIG_SCHEMA_VERSION));
    }
    String version = versionNode.getChild().getText();
    if (CONFIG_SCHEMA_VERSION.equals(version)) {
        return versionNode;
    }
    helper.error(versionNode.getStartLine(), "The specified configuration schema version '%s' is unsupported.", version);
    return null;
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) ScalarConfigNode(com.google.api.codegen.configgen.nodes.ScalarConfigNode)

Example 5 with FieldConfigNode

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

the class ConfigMerger method mergeLicenseHeader.

private void mergeLicenseHeader(ConfigNode configNode, ConfigNode prevNode) {
    FieldConfigNode licenseHeaderNode = MissingFieldTransformer.insert("license_header", configNode, prevNode).generate();
    if (NodeFinder.hasContent(licenseHeaderNode.getChild())) {
        return;
    }
    FieldConfigNode copyrightFileNode = FieldConfigNode.createStringPair(NodeFinder.getNextLine(licenseHeaderNode), "copyright_file", CONFIG_DEFAULT_COPYRIGHT_FILE).setComment(new DefaultComment("The file containing the copyright line(s)."));
    FieldConfigNode licenseFileNode = FieldConfigNode.createStringPair(NodeFinder.getNextLine(copyrightFileNode), "license_file", CONFIG_DEFAULT_LICENSE_FILE).setComment(new DefaultComment("The file containing the raw license header without any copyright line(s)."));
    licenseHeaderNode.setChild(copyrightFileNode.insertNext(licenseFileNode)).setComment(new DefaultComment("The configuration for the license header to put on generated files."));
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) DefaultComment(com.google.api.codegen.configgen.nodes.metadata.DefaultComment)

Aggregations

FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)27 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)22 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)11 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)9 DefaultComment (com.google.api.codegen.configgen.nodes.metadata.DefaultComment)7 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)5 Test (org.junit.Test)5 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)3 Node (org.yaml.snakeyaml.nodes.Node)3 FixmeComment (com.google.api.codegen.configgen.nodes.metadata.FixmeComment)2 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)2 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)2 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)2 FieldModel (com.google.api.codegen.config.FieldModel)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Yaml (org.yaml.snakeyaml.Yaml)1 ComposerException (org.yaml.snakeyaml.composer.ComposerException)1