Search in sources :

Example 1 with SampleContext

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

the class StaticLangGapicSamplesTransformer method getSampleContexts.

public List<SampleContext> getSampleContexts(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
    ImmutableList.Builder<SampleContext> sampleContexts = ImmutableList.builder();
    // Loop through sample configs and and map each sample ID to its matching calling forms.
    // We need this information when we need to create, in a language-specific way, unique
    // sample ids when one sample id has multiple matching calling forms
    Map<String, List<CallingForm>> configsAndMatchingForms = new HashMap<>();
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            List<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            sampleConfigs = firstNonNull(sampleConfigs, ImmutableList.<SampleConfig>of());
            for (SampleConfig config : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = namer.getMatchingCallingForms(methodContext, config.callingPattern());
                List<CallingForm> existingForms = configsAndMatchingForms.get(config.id());
                if (existingForms == null) {
                    existingForms = new ArrayList<>();
                    configsAndMatchingForms.put(config.id(), existingForms);
                }
                existingForms.addAll(allMatchingCallingForms);
            }
        }
    }
    SampleFileRegistry registry = new SampleFileRegistry(namer, configsAndMatchingForms);
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            ImmutableList<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            if (sampleConfigs == null) {
                continue;
            }
            for (SampleConfig sampleConfig : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = configsAndMatchingForms.get(sampleConfig.id());
                for (CallingForm form : allMatchingCallingForms) {
                    InitCodeOutputType initCodeOutputType = InitCodeOutputType.SingleObject;
                    // In Java and C#, we need to handle flattening.
                    if (CallingForm.isFlattened(form)) {
                        if (!methodContext.getMethodConfig().isFlattening()) {
                            continue;
                        }
                        initCodeOutputType = InitCodeOutputType.FieldList;
                        methodContext = methodContext.getSurfaceInterfaceContext().asFlattenedMethodContext(methodContext, methodContext.getMethodConfig().getFlatteningConfigs().get(0));
                    }
                    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).clientMethodType(fromCallingForm(form)).sampleConfig(sampleConfig).initCodeOutputType(initCodeOutputType).methodContext(methodContext).build();
                    sampleContexts.add(sampleContext);
                }
            }
        }
    }
    return sampleContexts.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) SampleConfig(com.google.api.codegen.config.SampleConfig) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 2 with SampleContext

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

the class SampleManifestTransformer method transform.

@Override
public List<ViewModel> transform(ProtoApiModel model, GapicProductConfig productConfig) {
    SurfaceNamer surfaceNamer = newSurfaceNamer.apply(productConfig);
    ModelTypeTable typeTable = newTypeTable.apply(productConfig.getPackageName());
    FeatureConfig featureConfig = newFeatureConfig.apply(productConfig);
    ImmutableList.Builder<SampleManifestView.SampleEntry> builder = ImmutableList.builder();
    List<InterfaceContext> interfaceContexts = Streams.stream(model.getInterfaces(productConfig)).filter(iface -> productConfig.hasInterfaceConfig(iface)).map(iface -> GapicInterfaceContext.create(iface, productConfig, typeTable, surfaceNamer, featureConfig)).collect(ImmutableList.toImmutableList());
    List<SampleContext> sampleContexts = metadataNamer.getSampleContexts(interfaceContexts, productConfig);
    for (SampleContext context : sampleContexts) {
        String sample = context.uniqueSampleId();
        String path = metadataNamer.getSamplePath(sample);
        String regionTag = context.sampleConfig().regionTag();
        String className = metadataNamer.getSampleClassName(sample);
        builder.add(SampleManifestView.SampleEntry.create(sample, path, className, regionTag));
    }
    ImmutableList<SampleManifestView.SampleEntry> entries = builder.build();
    // Only generate manifest files when samples are configured through sample config.
    if (entries.isEmpty()) {
        return Collections.emptyList();
    }
    SampleManifestView.Builder sampleManifestView = SampleManifestView.newBuilder();
    sampleManifestView.environment(metadataNamer.getEnvironment());
    sampleManifestView.bin(metadataNamer.getBin());
    sampleManifestView.basePath(metadataNamer.getBasePath(productConfig));
    sampleManifestView.packageName(metadataNamer.getPackageName(productConfig));
    sampleManifestView.invocation(metadataNamer.getInvocation());
    sampleManifestView.schemaVersion(MANIFEST_SCHEMA_VERSION);
    sampleManifestView.sampleEntries(entries);
    sampleManifestView.outputPath(getManifestOutputPath(model, productConfig));
    sampleManifestView.templateFileName(TEMPLATE_NAME);
    return Collections.singletonList(sampleManifestView.build());
}
Also used : GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) SampleContext(com.google.api.codegen.config.SampleContext) Streams(com.google.common.collect.Streams) Function(java.util.function.Function) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) InterfaceContext(com.google.api.codegen.config.InterfaceContext) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) SampleManifestView(com.google.api.codegen.viewmodel.metadata.SampleManifestView) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Paths(java.nio.file.Paths) ViewModel(com.google.api.codegen.viewmodel.ViewModel) VersionMatcher(com.google.api.codegen.util.VersionMatcher) Splitter(com.google.common.base.Splitter) Collections(java.util.Collections) ImmutableList(com.google.common.collect.ImmutableList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) SampleManifestView(com.google.api.codegen.viewmodel.metadata.SampleManifestView) SampleContext(com.google.api.codegen.config.SampleContext)

Example 3 with SampleContext

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

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

the class OutputTransformer method toViews.

// TODO(hzyi): Entry point for generating output views using gapic config. To be deprecated.
@Deprecated
ImmutableList<OutputView> toViews(List<OutputSpec> configs, MethodContext context, SampleValueSet valueSet, CallingForm form, OutputContext outputContext) {
    SampleConfig sampleConfig = SampleConfig.newBuilder().id(valueSet.getId()).type(SampleSpec.SampleType.STANDALONE).build();
    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(valueSet.getId()).sampleType(SampleSpec.SampleType.STANDALONE).sampleConfig(sampleConfig).initCodeOutputType(InitCodeOutputType.FieldList).callingForm(form).build();
    return toViews(fromOutputSpecs(configs), context, sampleContext, outputContext);
}
Also used : SampleConfig(com.google.api.codegen.config.SampleConfig) SampleContext(com.google.api.codegen.config.SampleContext)

Example 5 with SampleContext

use of com.google.api.codegen.config.SampleContext 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)

Aggregations

SampleContext (com.google.api.codegen.config.SampleContext)8 ImmutableList (com.google.common.collect.ImmutableList)6 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)5 InterfaceContext (com.google.api.codegen.config.InterfaceContext)5 SampleConfig (com.google.api.codegen.config.SampleConfig)5 List (java.util.List)5 MethodContext (com.google.api.codegen.config.MethodContext)4 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)4 CallingForm (com.google.api.codegen.viewmodel.CallingForm)4 HashMap (java.util.HashMap)4 ViewModel (com.google.api.codegen.viewmodel.ViewModel)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 OutputSpec (com.google.api.codegen.OutputSpec)2 SampleValueSet (com.google.api.codegen.SampleValueSet)2 FieldModel (com.google.api.codegen.config.FieldModel)2 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)2 MethodModel (com.google.api.codegen.config.MethodModel)2 OutputContext (com.google.api.codegen.config.OutputContext)2 SampleSpec (com.google.api.codegen.config.SampleSpec)2