Search in sources :

Example 1 with TypeModel

use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.

the class JavaSampleImportTransformer method addOutputImports.

@Override
protected void addOutputImports(MethodContext context, OutputContext outputContext) {
    ImportTypeTable typeTable = context.getTypeTable();
    OutputTransformer.ScopeTable scopeTable = outputContext.scopeTable();
    for (TypeModel type : scopeTable.allTypes()) {
        if (type == null) {
            saveResourceTypeName(context);
        } else {
            typeTable.getAndSaveNicknameFor(type);
        }
    }
    if (outputContext.hasMaps()) {
        typeTable.getAndSaveNicknameFor("java.util.Map");
    }
    if (outputContext.hasBytesFileOutput()) {
        typeTable.getAndSaveNicknameFor("java.io.FileOutputStream");
        typeTable.getAndSaveNicknameFor("java.io.OutputStream");
    }
    if (outputContext.hasStringFileOutput()) {
        typeTable.getAndSaveNicknameFor("java.io.FileWriter");
    }
}
Also used : ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) TypeModel(com.google.api.codegen.config.TypeModel) OutputTransformer(com.google.api.codegen.transformer.OutputTransformer)

Example 2 with TypeModel

use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.

the class StaticLangApiMethodTransformer method generateAsyncOperationRequestObjectMethod.

// Used by: C# (and indirectly by Java via the overload above)
public StaticLangApiMethodView generateAsyncOperationRequestObjectMethod(MethodContext context, List<ParamWithSimpleDoc> additionalParams, boolean requiresOperationMethod, SampleContext sampleContext) {
    MethodModel method = context.getMethodModel();
    SurfaceNamer namer = context.getNamer();
    StaticLangApiMethodView.Builder methodViewBuilder = StaticLangApiMethodView.newBuilder();
    setCommonFields(context, methodViewBuilder);
    methodViewBuilder.name(namer.getAsyncApiMethodName(context.getMethodModel(), context.getMethodConfig().getVisibility()));
    methodViewBuilder.exampleName(namer.getAsyncApiMethodExampleName(method));
    setRequestObjectMethodFields(context, namer.getOperationCallableMethodName(method), Synchronicity.Async, additionalParams, methodViewBuilder, context.getCallingForms(), sampleContext);
    if (requiresOperationMethod) {
        // Only for protobuf-based APIs.
        methodViewBuilder.operationMethod(lroTransformer.generateDetailView(context));
    }
    if (context.isLongRunningMethodContext()) {
        // Only for protobuf-based APIs.
        TypeModel returnType = context.getLongRunningConfig().getReturnType();
        methodViewBuilder.responseTypeName(context.getTypeTable().getAndSaveNicknameFor(returnType));
        methodViewBuilder.operationMethod(lroTransformer.generateDetailView(context));
    } else {
        throw new IllegalArgumentException("Discovery-based APIs do not have LongRunning operations.");
    }
    return methodViewBuilder.type(ClientMethodType.AsyncOperationRequestObjectMethod).build();
}
Also used : Builder(com.google.api.codegen.viewmodel.StaticLangApiMethodView.Builder) MethodModel(com.google.api.codegen.config.MethodModel) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) TypeModel(com.google.api.codegen.config.TypeModel)

Example 3 with TypeModel

use of com.google.api.codegen.config.TypeModel 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 4 with TypeModel

use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.

the class InitCodeNode method validateKeyValue.

private static void validateKeyValue(TypeModel parentType, String key) {
    if (parentType.isMap()) {
        TypeModel keyType = parentType.getMapKeyType();
        validateValue(keyType, key);
    } else if (parentType.isRepeated()) {
        validateValue(INT_TYPE, key);
    } else {
    // Don't validate message types, field will be missing for a bad key
    }
}
Also used : TypeModel(com.google.api.codegen.config.TypeModel)

Example 5 with TypeModel

use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.

the class OutputTransformerTest method testScopeTablePut.

@Test
public void testScopeTablePut() {
    TypeModel stringTypeModel = ProtoTypeRef.create(TypeRef.fromPrimitiveName("string"));
    assertThat(parent.put("str", stringTypeModel, "String")).isTrue();
}
Also used : TypeModel(com.google.api.codegen.config.TypeModel) Test(org.junit.Test)

Aggregations

TypeModel (com.google.api.codegen.config.TypeModel)32 MethodModel (com.google.api.codegen.config.MethodModel)12 Test (org.junit.Test)12 Scanner (com.google.api.codegen.util.Scanner)10 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)10 Builder (com.google.api.codegen.viewmodel.StaticLangApiMethodView.Builder)10 OutputView (com.google.api.codegen.viewmodel.OutputView)8 FieldModel (com.google.api.codegen.config.FieldModel)7 ImmutableList (com.google.common.collect.ImmutableList)4 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)3 ArrayList (java.util.ArrayList)3 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)2 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)2 OutputTransformer (com.google.api.codegen.transformer.OutputTransformer)2 LongRunningOperationDetailView (com.google.api.codegen.viewmodel.LongRunningOperationDetailView)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 HashSet (java.util.HashSet)2 OutputSpec (com.google.api.codegen.OutputSpec)1 SampleValueSet (com.google.api.codegen.SampleValueSet)1 FieldConfig (com.google.api.codegen.config.FieldConfig)1