Search in sources :

Example 1 with Scanner

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;
}
Also used : Scanner(com.google.api.codegen.util.Scanner) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) ArrayList(java.util.ArrayList)

Example 2 with Scanner

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();
}
Also used : IntStream(java.util.stream.IntStream) FieldModel(com.google.api.codegen.config.FieldModel) ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LongRunningConfig(com.google.api.codegen.config.LongRunningConfig) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) Map(java.util.Map) OutputContext(com.google.api.codegen.config.OutputContext) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleValueSet(com.google.api.codegen.SampleValueSet) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Set(java.util.Set) TypeModel(com.google.api.codegen.config.TypeModel) Scanner(com.google.api.codegen.util.Scanner) Name(com.google.api.codegen.util.Name) List(java.util.List) OutputSpec(com.google.api.codegen.OutputSpec) SampleSpec(com.google.api.codegen.config.SampleSpec) Preconditions(com.google.common.base.Preconditions) OutputView(com.google.api.codegen.viewmodel.OutputView) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView) TypeModel(com.google.api.codegen.config.TypeModel)

Example 3 with Scanner

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());
}
Also used : Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView)

Example 4 with Scanner

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()));
        }
    }
}
Also used : Scanner(com.google.api.codegen.util.Scanner)

Example 5 with Scanner

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");
    }
}
Also used : Scanner(com.google.api.codegen.util.Scanner) TypeModel(com.google.api.codegen.config.TypeModel) FieldModel(com.google.api.codegen.config.FieldModel) Test(org.junit.Test)

Aggregations

Scanner (com.google.api.codegen.util.Scanner)20 OutputView (com.google.api.codegen.viewmodel.OutputView)14 TypeModel (com.google.api.codegen.config.TypeModel)11 Test (org.junit.Test)11 FieldModel (com.google.api.codegen.config.FieldModel)7 ImmutableList (com.google.common.collect.ImmutableList)3 Map (java.util.Map)3 OutputSpec (com.google.api.codegen.OutputSpec)2 SampleValueSet (com.google.api.codegen.SampleValueSet)2 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)2 MethodContext (com.google.api.codegen.config.MethodContext)2 OutputContext (com.google.api.codegen.config.OutputContext)2 SampleConfig (com.google.api.codegen.config.SampleConfig)2 SampleContext (com.google.api.codegen.config.SampleContext)2 SampleSpec (com.google.api.codegen.config.SampleSpec)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 ResponseStatementProto (com.google.api.codegen.samplegen.v1p2.ResponseStatementProto)2 Name (com.google.api.codegen.util.Name)2 CallingForm (com.google.api.codegen.viewmodel.CallingForm)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2