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);
}
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);
}
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;
}
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;
}
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."));
}
Aggregations