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