use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class InitCodeTransformer method sampleFuncParams.
/**
* Returns all the nodes to be rendered as sample function parameters.
*
* <p>If path is:
* <li>a normal node, returns that node.
* <li>a ReadFile node, returns the child node of that node.
* <li>a resource path, returns the child node whose key equals the entity name in the path.
*
* @param paramConfigMap the sample parameter configurations derived from {@code InitCodeContext}
*/
private List<InitCodeNode> sampleFuncParams(InitCodeNode root, List<String> paths, Map<String, SampleParameterConfig> paramConfigMap) {
List<InitCodeNode> params = new ArrayList<>();
for (String path : paths) {
Scanner scanner = new Scanner(path);
InitCodeNode node = FieldStructureParser.parsePath(root, scanner);
int token = scanner.lastToken();
if (token == '%') {
scanner.scan();
node = node.getChildren().get(scanner.tokenStr());
node.setDescription(paramConfigMap.get(path).comment());
params.add(node);
} else if (node.getLineType() == InitCodeLineType.ReadFileInitLine) {
node = node.getChildren().get(InitCodeNode.FILE_NAME_KEY);
node.setDescription(paramConfigMap.get(path).comment());
params.add(node);
} else {
node.setDescription(paramConfigMap.get(path).comment());
params.add(node);
}
}
return params;
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class OutputTransformer method mapLoopView.
private OutputView.MapLoopView mapLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
outputContext.mapSpecs().add(loop);
ScopeTable scope = outputContext.scopeTable();
String key = loop.getKey();
String value = loop.getValue();
OutputView.VariableView mapVar = accessor(new Scanner(loop.getMap()), methodContext, sampleContext, scope);
TypeModel keyType = mapVar.type().getMapKeyType();
TypeModel valueType = mapVar.type().getMapValueType();
String keyTypeName = methodContext.getTypeTable().getNicknameFor(keyType);
String valueTypeName = methodContext.getTypeTable().getNicknameFor(valueType);
if (!key.isEmpty()) {
assertIdentifierNotUsed(key, methodContext, sampleContext);
scope.put(key, keyType, keyTypeName);
}
if (!value.isEmpty()) {
assertIdentifierNotUsed(value, methodContext, sampleContext);
scope.put(value, valueType, valueTypeName);
}
return OutputView.MapLoopView.newBuilder().keyVariableName(methodContext.getNamer().localVarName(Name.anyLower(key))).keyType(keyTypeName).valueVariableName(methodContext.getNamer().localVarName(Name.anyLower(value))).valueType(valueTypeName).map(mapVar).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 OutputTransformer method toView.
private OutputView toView(ResponseStatementProto config, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
Runnable once = new Runnable() {
boolean ran;
@Override
public void run() {
Preconditions.checkArgument(!ran, "%s:%s: only one field of ResponseStatementProto may be set", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id());
ran = true;
}
};
OutputView view = null;
if (config.hasLoop()) {
once.run();
view = loopView(config.getLoop(), methodContext, sampleContext, outputContext);
}
if (config.getPrintCount() > 0) {
once.run();
view = printView(config.getPrintList(), methodContext, sampleContext, outputContext);
}
if (!config.getDefine().isEmpty()) {
once.run();
view = defineView(new Scanner(config.getDefine()), methodContext, sampleContext, outputContext);
}
if (config.getCommentCount() > 0) {
once.run();
view = commentView(config.getCommentList(), methodContext);
}
if (config.hasWriteFile()) {
once.run();
view = writeFileView(config.getWriteFile(), methodContext, sampleContext, outputContext);
}
return Preconditions.checkNotNull(view, "%s:%s: one field of ResponseStatementProto must be set", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id());
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class FieldStructureParser method parseEntityName.
/**
* Returns the entity name specified by `path` or null if `path` does not contain `%`.
*/
public static String parseEntityName(String path) {
Scanner scanner = new Scanner(path);
Preconditions.checkArgument(scanner.scan() == Scanner.IDENT, "expected identifier: %s", scanner.input());
int token;
String entityName = null;
while (true) {
token = scanner.scan();
switch(token) {
case '%':
Preconditions.checkArgument(entityName == null, "expected only one \"%%\" in path: %s", path);
Preconditions.checkArgument(scanner.scan() == Scanner.IDENT, "expected identifier after '%%': %s", scanner.input());
entityName = scanner.tokenStr();
break;
case '=':
case Scanner.EOF:
return entityName;
case '.':
Preconditions.checkArgument(scanner.scan() == Scanner.IDENT, "expected identifier after '.': %s", scanner.input());
break;
case '[':
Preconditions.checkArgument(scanner.scan() == Scanner.INT, "expected number after '[': %s", scanner.input());
Preconditions.checkArgument(scanner.scan() == ']', "expected closing ']': %s", scanner.input());
break;
case '{':
parseKey(scanner);
Preconditions.checkArgument(scanner.scan() == '}', "expected closing '}': %s", scanner.input());
break;
default:
throw new IllegalArgumentException(String.format("unexpected character '%c': %s", token, scanner.input()));
}
}
}
use of com.google.api.codegen.util.Scanner in project toolkit by googleapis.
the class OutputTransformerTest method testAccessorNewVariableMapKeyInvalidBooleanFail.
@Test
public void testAccessorNewVariableMapKeyInvalidBooleanFail() {
Scanner scanner = new Scanner("old_var.property{not_boolean}");
when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar");
TypeModel oldVarTypeModel = mock(TypeModel.class);
assertThat(parent.put("old_var", oldVarTypeModel, "OldVarType")).isTrue();
when(oldVarTypeModel.isMessage()).thenReturn(true);
when(oldVarTypeModel.isRepeated()).thenReturn(false);
when(oldVarTypeModel.isMap()).thenReturn(false);
FieldModel propertyFieldModel = mock(FieldModel.class);
when(oldVarTypeModel.getField("property")).thenReturn(propertyFieldModel);
TypeModel propertyTypeModel = mock(TypeModel.class);
when(propertyFieldModel.getType()).thenReturn(propertyTypeModel);
when(propertyTypeModel.isRepeated()).thenReturn(true);
when(propertyTypeModel.isMap()).thenReturn(true);
when(namer.getFieldGetFunctionName(propertyFieldModel)).thenReturn("getProperty");
when(namer.getFieldAccessorName(propertyFieldModel)).thenReturn(".getProperty()");
TypeModel boolTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("bool"));
TypeModel stringTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("string"));
when(propertyTypeModel.getMapKeyType()).thenReturn(boolTypeModel);
when(propertyTypeModel.getMapValueType()).thenReturn(stringTypeModel);
try {
accessorNewVariable(scanner, context, sampleContext, parent, "newVar", false);
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Could not assign value 'not_boolean' to type bool");
}
}
Aggregations