Search in sources :

Example 16 with Scanner

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

Example 17 with Scanner

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

Example 18 with Scanner

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

Example 19 with Scanner

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());
        }
    }
}
Also used : Scanner(com.google.api.codegen.util.Scanner) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SampleParameterConfig(com.google.api.codegen.config.SampleParameterConfig)

Example 20 with Scanner

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

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