Search in sources :

Example 1 with MethodSampleView

use of com.google.api.codegen.viewmodel.MethodSampleView in project toolkit by googleapis.

the class StaticLangGapicSamplesTransformer method generateSamplesFromGapicConfigs.

private List<ViewModel> generateSamplesFromGapicConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig, SurfaceNamer namer) {
    List<MethodSampleView> allSamples = interfaceContexts.stream().flatMap(iface -> apiMethodTransformer.generateApiMethods(iface).stream()).flatMap(method -> method.samples().stream()).collect(ImmutableList.toImmutableList());
    SampleFileRegistry registry = new SampleFileRegistry(namer, allSamples);
    List<ViewModel> sampleFileViews = new ArrayList<>();
    for (InterfaceContext context : interfaceContexts) {
        List<StaticLangApiMethodView> methods = apiMethodTransformer.generateApiMethods(context);
        for (StaticLangApiMethodView method : methods) {
            for (MethodSampleView sample : method.samples()) {
                sampleFileViews.add(newSampleFileView(productConfig, context, registry.getSampleClassName(sample, method.name()), registry.getSampleFileName(sample, method.name()), method, sample));
            }
        }
    }
    return sampleFileViews;
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) SampleEntryPointView(com.google.api.codegen.viewmodel.SampleEntryPointView) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) MethodModel(com.google.api.codegen.config.MethodModel) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Map(java.util.Map) StaticLangSampleClassView(com.google.api.codegen.viewmodel.StaticLangSampleClassView) StaticLangFileView(com.google.api.codegen.viewmodel.StaticLangFileView) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Streams(com.google.common.collect.Streams) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) List(java.util.List) Paths(java.nio.file.Paths) SampleSpec(com.google.api.codegen.config.SampleSpec) ImmutableTable(com.google.common.collect.ImmutableTable) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext)

Example 2 with MethodSampleView

use of com.google.api.codegen.viewmodel.MethodSampleView in project toolkit by googleapis.

the class SampleTransformer method generateSamples.

/**
 * Returns a list of MethodSampleViews for the given MethodContext.
 *
 * @param initContext
 * @param methodContext
 * @param fieldConfigs
 * @param initCodeOutputType
 * @param callingForms The list of calling forms applicable to this method, for which we will
 *     generate samples if so configured via context.getMethodConfig()
 * @return A list of of the MethodSampleView, each of which corresponds to a specific sample
 *     forthe method
 */
public List<MethodSampleView> generateSamples(MethodContext methodContext, InitCodeContext initContext, Collection<FieldConfig> fieldConfigs, InitCodeOutputType initCodeOutputType, List<CallingForm> callingForms) {
    CallingForm defaultCallingForm = methodContext.getNamer().getDefaultCallingForm(methodContext);
    List<MethodSampleView> methodSampleViews = new ArrayList<>();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    SampleValueSet defaultValueSet = defaultValueSet(methodConfig);
    for (SampleConfig sampleConfig : methodConfig.getSampleSpec().getSampleConfigs(callingForms, defaultCallingForm, defaultValueSet, sampleType())) {
        // Do not override outer initContext
        InitCodeContext thisContext = initContext;
        if (thisContext == null) {
            thisContext = createInitCodeContext(methodContext, fieldConfigs, initCodeOutputType, sampleConfig.valueSet());
        }
        methodSampleViews.add(generateSample(sampleConfig, methodContext, thisContext));
    }
    return methodSampleViews;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) SampleConfig(com.google.api.codegen.config.SampleConfig) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ArrayList(java.util.ArrayList) SampleValueSet(com.google.api.codegen.SampleValueSet) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 3 with MethodSampleView

use of com.google.api.codegen.viewmodel.MethodSampleView in project toolkit by googleapis.

the class StaticLangApiMethodTransformer method setRequestObjectMethodFields.

private void setRequestObjectMethodFields(MethodContext context, String callableMethodName, Synchronicity sync, List<ParamWithSimpleDoc> additionalParams, StaticLangApiMethodView.Builder methodViewBuilder, List<CallingForm> callingForms, SampleContext sampleContext) {
    MethodModel method = context.getMethodModel();
    SurfaceNamer namer = context.getNamer();
    List<ParamDocView> paramDocs = new ArrayList<>();
    paramDocs.addAll(getRequestObjectParamDocs(context));
    paramDocs.addAll(ParamWithSimpleDoc.asParamDocViews(additionalParams));
    methodViewBuilder.doc(ApiMethodDocView.newBuilder().mainDocLines(namer.getDocLines(method, context.getMethodConfig())).paramDocs(paramDocs).throwsDocLines(namer.getThrowsDocLines(context.getMethodConfig())).returnsDocLines(namer.getReturnDocLines(context.getSurfaceInterfaceContext(), context, sync)).build());
    if (sampleContext == null) {
        sampleTransformer.generateSamples(methodViewBuilder, context, context.getMethodConfig().getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, callingForms);
    } else {
        MethodSampleView sampleView = sampleTransformer.generateSample(context, sampleContext);
        methodViewBuilder.samples(ImmutableList.of(sampleView));
    }
    methodViewBuilder.methodParams(new ArrayList<RequestObjectParamView>());
    methodViewBuilder.requestObjectParams(new ArrayList<RequestObjectParamView>());
    methodViewBuilder.pathTemplateChecks(new ArrayList<PathTemplateCheckView>());
    RequestObjectMethodDetailView.Builder detailBuilder = RequestObjectMethodDetailView.newBuilder();
    detailBuilder.accessModifier(context.getNamer().getVisiblityKeyword(context.getMethodConfig().getVisibility()));
    detailBuilder.callableMethodName(callableMethodName);
    methodViewBuilder.requestObjectMethod(detailBuilder.build());
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) PathTemplateCheckView(com.google.api.codegen.viewmodel.PathTemplateCheckView) ArrayList(java.util.ArrayList) SimpleParamDocView(com.google.api.codegen.viewmodel.SimpleParamDocView) ParamDocView(com.google.api.codegen.viewmodel.ParamDocView) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView) RequestObjectMethodDetailView(com.google.api.codegen.viewmodel.RequestObjectMethodDetailView)

Example 4 with MethodSampleView

use of com.google.api.codegen.viewmodel.MethodSampleView in project toolkit by googleapis.

the class StaticLangApiMethodTransformer method setCallableMethodFields.

private void setCallableMethodFields(MethodContext context, String callableName, Builder methodViewBuilder, List<CallingForm> callingForms, SampleContext sampleContext) {
    MethodModel method = context.getMethodModel();
    methodViewBuilder.doc(ApiMethodDocView.newBuilder().mainDocLines(context.getNamer().getDocLines(method, context.getMethodConfig())).paramDocs(new ArrayList<ParamDocView>()).throwsDocLines(new ArrayList<String>()).build());
    if (sampleContext == null) {
        sampleTransformer.generateSamples(methodViewBuilder, context, context.getMethodConfig().getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, callingForms);
    } else {
        MethodSampleView sampleView = sampleTransformer.generateSample(context, sampleContext);
        methodViewBuilder.samples(ImmutableList.of(sampleView));
    }
    methodViewBuilder.methodParams(new ArrayList<RequestObjectParamView>());
    methodViewBuilder.requestObjectParams(new ArrayList<RequestObjectParamView>());
    methodViewBuilder.pathTemplateChecks(new ArrayList<PathTemplateCheckView>());
    String requestTypeFullName = context.getMethodModel().getInputTypeName(context.getTypeTable()).getFullName();
    String requestType = context.getTypeTable().getAndSaveNicknameFor(requestTypeFullName);
    String genericAwareResponseTypeFullName = context.getNamer().getGenericAwareResponseTypeName(context);
    String genericAwareResponseType = context.getTypeTable().getAndSaveNicknameFor(genericAwareResponseTypeFullName);
    MethodConfig methodConfig = context.getMethodConfig();
    ApiCallableImplType callableImplType = ApiCallableImplType.SimpleApiCallable;
    if (methodConfig.isGrpcStreaming()) {
        callableImplType = ApiCallableImplType.of(methodConfig.getGrpcStreamingType());
    } else if (methodConfig.isBatching()) {
        callableImplType = ApiCallableImplType.BatchingApiCallable;
    }
    methodViewBuilder.callableMethod(CallableMethodDetailView.newBuilder().requestType(requestType).genericAwareResponseType(genericAwareResponseType).callableName(callableName).interfaceTypeName(context.getNamer().getApiCallableTypeName(callableImplType.serviceMethodType())).build());
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) PathTemplateCheckView(com.google.api.codegen.viewmodel.PathTemplateCheckView) ArrayList(java.util.ArrayList) ApiCallableImplType(com.google.api.codegen.viewmodel.ApiCallableImplType) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView)

Example 5 with MethodSampleView

use of com.google.api.codegen.viewmodel.MethodSampleView in project toolkit by googleapis.

the class DynamicLangApiMethodTransformer method generateMethodCommon.

private void generateMethodCommon(GapicMethodContext context, InitCodeContext initContext, boolean packageHasMultipleServices, MethodModel method, OptionalArrayMethodView.Builder apiMethod, List<CallingForm> callingForms, SampleContext sampleContext) {
    SurfaceNamer namer = context.getNamer();
    apiMethod.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    apiMethod.topLevelAliasedApiClassName(namer.getTopLevelAliasedApiClassName((context.getInterfaceConfig()), packageHasMultipleServices));
    apiMethod.versionAliasedApiClassName(namer.getVersionAliasedApiClassName((context.getInterfaceConfig()), packageHasMultipleServices));
    apiMethod.apiVariableName(namer.getApiWrapperVariableName(context.getInterfaceConfig()));
    apiMethod.apiModuleName(namer.getApiWrapperModuleName());
    apiMethod.localPackageName(namer.getLocalPackageName());
    apiMethod.doc(generateMethodDoc(context));
    apiMethod.name(namer.getApiMethodName(method, context.getMethodConfig().getVisibility()));
    apiMethod.requestVariableName(namer.getRequestVariableName(method));
    apiMethod.requestTypeName(namer.getAndSaveTypeName(context.getTypeTable(), context.getMethodModel().getInputType()));
    apiMethod.responseTypeName(namer.getAndSaveTypeName(context.getTypeTable(), context.getMethodModel().getOutputType()));
    apiMethod.hasReturnValue(!ServiceMessages.s_isEmptyType(context.getMethod().getOutputType()));
    apiMethod.key(namer.getMethodKey(method));
    apiMethod.grpcMethodName(namer.getGrpcMethodName(method));
    apiMethod.rerouteToGrpcInterface(context.getMethodConfig().getRerouteToGrpcInterface());
    apiMethod.stubName(namer.getStubName(context.getTargetInterface()));
    apiMethod.methodParams(apiMethodParamTransformer.generateMethodParams(context));
    Iterable<FieldConfig> filteredFieldConfigs = removePageTokenFieldConfig(context, context.getMethodConfig().getOptionalFieldConfigs());
    List<RequestObjectParamView> requiredParams = generateRequestObjectParams(context, context.getMethodConfig().getRequiredFieldConfigs());
    List<RequestObjectParamView> optionalParams = generateRequestObjectParams(context, context.getMethodConfig().getOptionalFieldConfigs());
    List<RequestObjectParamView> optionalParamsNoPageToken = generateRequestObjectParams(context, filteredFieldConfigs);
    apiMethod.requiredRequestObjectParams(requiredParams);
    apiMethod.optionalRequestObjectParams(optionalParams);
    apiMethod.optionalRequestObjectParamsNoPageToken(optionalParamsNoPageToken);
    apiMethod.hasRequestParameters(!requiredParams.isEmpty() || !optionalParamsNoPageToken.isEmpty());
    GrpcStreamingType grpcStreamingType = context.getMethodConfig().getGrpcStreamingType();
    apiMethod.grpcStreamingType(grpcStreamingType);
    apiMethod.isSingularRequestMethod(grpcStreamingType.equals(GrpcStreamingType.NonStreaming) || grpcStreamingType.equals(GrpcStreamingType.ServerStreaming));
    apiMethod.packageName(namer.getPackageName());
    apiMethod.apiVersion(namer.getApiWrapperModuleVersion());
    apiMethod.oneofParams(context.getMethodConfig().getOneofNames(namer));
    apiMethod.headerRequestParams(headerRequestParamTransformer.generateHeaderRequestParams(context));
    InitCodeOutputType initCodeOutputType = context.getMethodModel().getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
    if (sampleContext == null) {
        sampleTransformer.generateSamples(apiMethod, context, initContext, context.getMethodConfig().getRequiredFieldConfigs(), initCodeOutputType, callingForms);
    } else {
        MethodSampleView sampleView = sampleTransformer.generateSample(context, sampleContext);
        apiMethod.samples(ImmutableList.of(sampleView));
    }
}
Also used : MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) FieldConfig(com.google.api.codegen.config.FieldConfig) GrpcStreamingType(com.google.api.codegen.config.GrpcStreamingConfig.GrpcStreamingType) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)

Aggregations

MethodSampleView (com.google.api.codegen.viewmodel.MethodSampleView)9 ArrayList (java.util.ArrayList)7 MethodModel (com.google.api.codegen.config.MethodModel)5 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)4 InterfaceContext (com.google.api.codegen.config.InterfaceContext)4 SampleContext (com.google.api.codegen.config.SampleContext)4 RequestObjectParamView (com.google.api.codegen.viewmodel.RequestObjectParamView)4 ViewModel (com.google.api.codegen.viewmodel.ViewModel)4 SampleConfig (com.google.api.codegen.config.SampleConfig)3 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)3 CallingForm (com.google.api.codegen.viewmodel.CallingForm)3 ImmutableList (com.google.common.collect.ImmutableList)3 FieldConfig (com.google.api.codegen.config.FieldConfig)2 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)2 MethodConfig (com.google.api.codegen.config.MethodConfig)2 MethodContext (com.google.api.codegen.config.MethodContext)2 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)2 SampleSpec (com.google.api.codegen.config.SampleSpec)2 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)2 OptionalArrayMethodView (com.google.api.codegen.viewmodel.OptionalArrayMethodView)2