use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class OutputTransformer method stringInterpolationView.
private OutputView.StringInterpolationView stringInterpolationView(MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext, List<String> configs) {
String format = configs.get(0);
ImmutableList.Builder<String> builder = ImmutableList.builder();
for (String path : configs.subList(1, configs.size())) {
OutputView.VariableView variable = accessor(new Scanner(path), methodContext, sampleContext, outputContext.scopeTable());
TypeModel type = variable.type();
// resource names, but should include them as well for completeness
if (type != null) {
outputContext.stringFormattedVariableTypes().add(type);
}
String formattedArg = methodContext.getNamer().getFormattedPrintArgName(methodContext.getTypeTable(), type, variable.variable(), variable.accessors());
builder.add(formattedArg);
}
ImmutableList<String> args = builder.build();
ImmutableList<String> stringWithInterpolatedArgs = methodContext.getNamer().getInterpolatedFormatAndArgs(format, args);
return OutputView.StringInterpolationView.newBuilder().format(stringWithInterpolatedArgs.get(0)).args(stringWithInterpolatedArgs.subList(1, stringWithInterpolatedArgs.size())).build();
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class OutputTransformer method writeFileView.
private OutputView.WriteFileView writeFileView(ResponseStatementProto.WriteFileStatement config, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
OutputView.StringInterpolationView fileName = stringInterpolationView(methodContext, sampleContext, outputContext, config.getFileNameList());
OutputView.VariableView contents = accessor(new Scanner(config.getContents()), methodContext, sampleContext, outputContext.scopeTable());
Preconditions.checkArgument(contents.type().isStringType() || contents.type().isBytesType(), "Output to file: expected 'string' or 'bytes', found %s", contents.type().getTypeName());
outputContext.fileOutputTypes().add(contents.type());
return OutputView.WriteFileView.newBuilder().fileName(fileName).contents(contents).isFirst(!outputContext.hasMultipleFileOutputs()).build();
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class OutputTransformer method arrayLoopView.
private OutputView.ArrayLoopView arrayLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
ScopeTable scope = outputContext.scopeTable();
String loopVariable = loop.getVariable();
assertIdentifierNotUsed(loopVariable, methodContext, sampleContext);
OutputView.VariableView accessor = accessorNewVariable(new Scanner(loop.getCollection()), methodContext, sampleContext, scope, loopVariable, true);
return OutputView.ArrayLoopView.newBuilder().variableType(scope.getTypeName(loopVariable)).variableName(methodContext.getNamer().localVarName(Name.from(loopVariable))).collection(accessor).body(loop.getBodyList().stream().map(body -> toView(body, methodContext, sampleContext, outputContext)).collect(ImmutableList.toImmutableList())).build();
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class InitCodeNode method resolveSampleParamConfigs.
private void resolveSampleParamConfigs(InitCodeContext context, Map<String, SampleParameterConfig> configs) {
for (Map.Entry<String, SampleParameterConfig> entry : configs.entrySet()) {
Scanner scanner = new Scanner(entry.getKey());
InitCodeNode parent = FieldStructureParser.parsePath(this, scanner);
int token = scanner.lastToken();
if (token == Scanner.EOF) {
parent.resolveSampleParamConfig(context, entry.getValue());
} else if (token == '%') {
Preconditions.checkArgument(scanner.scan() == Scanner.IDENT, "expected IDENT after '%': %s", entry.getKey());
String entityName = scanner.tokenStr();
Preconditions.checkArgument(scanner.scan() == Scanner.EOF, "expected EOF after entity name: %s", entityName);
parent.resolveSampleParamConfig(context, entityName, entry.getValue());
}
}
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class FieldStructureParser method parseConfig.
// Parses `config` to construct the `InitCodeNode` it specifies. `config` must be a valid
// config satisfying the eBNF grammar below:
//
// config = path ['%' ident] ['=' value];
// path = ident pathElem*
// pathElem = ('.' ident) | ('[' int ']') | ('{' value '}');
// value = int | string | ident | emptyObject;
// emptyObject = {};
//
// For compatibility with the previous parser, when ident is used as a value, the value is
// the name of the ident. Eg, if the ident is "x", the value is simply "x", not the content
// of the variable named "x".
//
private static InitCodeNode parseConfig(InitCodeNode root, String config, Map<String, InitValueConfig> initValueConfigMap) {
Scanner scanner = new Scanner(config);
InitCodeNode parent = parsePath(root, scanner);
int fieldNamePos = config.length();
int token = scanner.lastToken();
String entityName = null;
if (token == '%') {
fieldNamePos = scanner.pos() - 1;
Preconditions.checkArgument(scanner.scan() == Scanner.IDENT, "expected ident after '%': %s", config);
entityName = scanner.tokenStr();
token = scanner.scan();
}
InitValue initValue = null;
if (token == '=') {
String valueString = parseValue(scanner);
if (valueString.contains(InitFieldConfig.RANDOM_TOKEN)) {
initValue = InitValue.createRandom(valueString);
} else if (valueString.contains(PROJECT_ID_TOKEN)) {
Preconditions.checkArgument(valueString.equals(PROJECT_ID_TOKEN), "%s cannot be used as substring: %s", PROJECT_ID_TOKEN, config);
initValue = InitValue.createVariable(InitFieldConfig.PROJECT_ID_VARIABLE_NAME);
} else if (valueString.equals(EMPTY_OBJECT)) {
parent.setLineType(InitCodeLineType.StructureInitLine);
return root;
} else {
initValue = InitValue.createLiteral(valueString);
}
}
InitValueConfig valueConfig = createInitValueConfig(InitFieldConfig.newBuilder().fieldPath(config.substring(0, fieldNamePos)).entityName(entityName).value(initValue).build(), initValueConfigMap);
if (valueConfig != null) {
parent.updateInitValueConfig(InitCodeNode.mergeInitValueConfig(parent.getInitValueConfig(), valueConfig));
parent.setLineType(InitCodeLineType.SimpleInitLine);
}
return root;
}
Aggregations