Search in sources :

Example 1 with ResourceNameTreatment

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

the class GapicMethodConfig method createMethodConfig.

/**
 * Creates an instance of GapicMethodConfig based on MethodConfigProto, linking it up with the
 * provided method. On errors, null will be returned, and diagnostics are reported to the diag
 * collector.
 */
@Nullable
static GapicMethodConfig createMethodConfig(DiagCollector diagCollector, String language, MethodConfigProto methodConfigProto, Method method, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, ImmutableSet<String> retryCodesConfigNames, ImmutableSet<String> retryParamsConfigNames) {
    boolean error = false;
    ProtoMethodModel methodModel = new ProtoMethodModel(method);
    PageStreamingConfig pageStreaming = null;
    if (!PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getPageStreaming())) {
        pageStreaming = PageStreamingConfig.createPageStreaming(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
        if (pageStreaming == null) {
            error = true;
        }
    }
    GrpcStreamingConfig grpcStreaming = null;
    if (isGrpcStreamingMethod(methodModel)) {
        if (PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getGrpcStreaming())) {
            grpcStreaming = GrpcStreamingConfig.createGrpcStreaming(diagCollector, method);
        } else {
            grpcStreaming = GrpcStreamingConfig.createGrpcStreaming(diagCollector, methodConfigProto.getGrpcStreaming(), method);
            if (grpcStreaming == null) {
                error = true;
            }
        }
    }
    ImmutableList<FlatteningConfig> flattening = null;
    if (!FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
        flattening = createFlattening(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
        if (flattening == null) {
            error = true;
        }
    }
    BatchingConfig batching = null;
    if (!BatchingConfigProto.getDefaultInstance().equals(methodConfigProto.getBatching())) {
        batching = BatchingConfig.createBatching(diagCollector, methodConfigProto.getBatching(), methodModel);
        if (batching == null) {
            error = true;
        }
    }
    String retryCodesName = methodConfigProto.getRetryCodesName();
    if (!retryCodesName.isEmpty() && !retryCodesConfigNames.contains(retryCodesName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Retry codes config used but not defined: '%s' (in method %s)", retryCodesName, methodModel.getFullName()));
        error = true;
    }
    String retryParamsName = methodConfigProto.getRetryParamsName();
    if (!retryParamsConfigNames.isEmpty() && !retryParamsConfigNames.contains(retryParamsName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Retry parameters config used but not defined: %s (in method %s)", retryParamsName, methodModel.getFullName()));
        error = true;
    }
    Duration timeout = Duration.ofMillis(methodConfigProto.getTimeoutMillis());
    if (timeout.toMillis() <= 0) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Default timeout not found or has invalid value (in method %s)", methodModel.getFullName()));
        error = true;
    }
    boolean hasRequestObjectMethod = methodConfigProto.getRequestObjectMethod();
    if (hasRequestObjectMethod && method.getRequestStreaming()) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "request_object_method incompatible with streaming method %s", method.getFullName()));
        error = true;
    }
    ImmutableMap<String, String> fieldNamePatterns = ImmutableMap.copyOf(methodConfigProto.getFieldNamePatterns());
    ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
    if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
        defaultResourceNameTreatment = ResourceNameTreatment.NONE;
    }
    Iterable<FieldConfig> requiredFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getRequiredFields(diagCollector, methodModel, methodConfigProto.getRequiredFieldsList()));
    Iterable<FieldConfig> optionalFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getOptionalFields(methodModel, methodConfigProto.getRequiredFieldsList()));
    List<String> sampleCodeInitFields = new ArrayList<>();
    sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());
    String rerouteToGrpcInterface = Strings.emptyToNull(methodConfigProto.getRerouteToGrpcInterface());
    VisibilityConfig visibility = VisibilityConfig.PUBLIC;
    ReleaseLevel releaseLevel = ReleaseLevel.GA;
    for (SurfaceTreatmentProto treatment : methodConfigProto.getSurfaceTreatmentsList()) {
        if (!treatment.getIncludeLanguagesList().contains(language)) {
            continue;
        }
        if (treatment.getVisibility() != VisibilityProto.UNSET_VISIBILITY) {
            visibility = VisibilityConfig.fromProto(treatment.getVisibility());
        }
        if (treatment.getReleaseLevel() != ReleaseLevel.UNSET_RELEASE_LEVEL) {
            releaseLevel = treatment.getReleaseLevel();
        }
    }
    LongRunningConfig longRunningConfig = null;
    if (!LongRunningConfigProto.getDefaultInstance().equals(methodConfigProto.getLongRunning())) {
        longRunningConfig = LongRunningConfig.createLongRunningConfig(method.getModel(), diagCollector, methodConfigProto.getLongRunning());
        if (longRunningConfig == null) {
            error = true;
        }
    }
    List<String> headerRequestParams = ImmutableList.copyOf(methodConfigProto.getHeaderRequestParamsList());
    if (error) {
        return null;
    } else {
        return new AutoValue_GapicMethodConfig(methodModel, pageStreaming, grpcStreaming, flattening, retryCodesName, retryParamsName, timeout, requiredFieldConfigs, optionalFieldConfigs, defaultResourceNameTreatment, batching, hasRequestObjectMethod, fieldNamePatterns, sampleCodeInitFields, rerouteToGrpcInterface, visibility, releaseLevel, longRunningConfig, headerRequestParams);
    }
}
Also used : SurfaceTreatmentProto(com.google.api.codegen.SurfaceTreatmentProto) ArrayList(java.util.ArrayList) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) Duration(org.threeten.bp.Duration) ReleaseLevel(com.google.api.codegen.ReleaseLevel) Nullable(javax.annotation.Nullable)

Example 2 with ResourceNameTreatment

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

the class DiscoGapicMethodConfig method createDiscoGapicMethodConfig.

/**
 * Creates an instance of DiscoGapicMethodConfig based on MethodConfigProto, linking it up with
 * the provided method. On errors, null will be returned, and diagnostics are reported to the diag
 * collector.
 */
@Nullable
static DiscoGapicMethodConfig createDiscoGapicMethodConfig(DiscoApiModel apiModel, String language, MethodConfigProto methodConfigProto, Method method, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, ImmutableSet<String> retryCodesConfigNames, ImmutableSet<String> retryParamsConfigNames) {
    boolean error = false;
    DiscoveryMethodModel methodModel = new DiscoveryMethodModel(method, apiModel);
    DiagCollector diagCollector = apiModel.getDiagCollector();
    PageStreamingConfig pageStreaming = null;
    if (!PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getPageStreaming())) {
        pageStreaming = PageStreamingConfig.createPageStreaming(apiModel, method);
        if (pageStreaming == null) {
            error = true;
        }
    }
    ImmutableList<FlatteningConfig> flattening = null;
    if (!FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
        flattening = createFlattening(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
        if (flattening == null) {
            error = true;
        }
    }
    BatchingConfig batching = null;
    if (!BatchingConfigProto.getDefaultInstance().equals(methodConfigProto.getBatching())) {
        batching = BatchingConfig.createBatching(diagCollector, methodConfigProto.getBatching(), methodModel);
        if (batching == null) {
            error = true;
        }
    }
    String retryCodesName = methodConfigProto.getRetryCodesName();
    if (!retryCodesName.isEmpty() && !retryCodesConfigNames.contains(retryCodesName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Retry codes config used but not defined: '%s' (in method %s)", retryCodesName, methodModel.getFullName()));
        error = true;
    }
    String retryParamsName = methodConfigProto.getRetryParamsName();
    if (!retryParamsConfigNames.isEmpty() && !retryParamsConfigNames.contains(retryParamsName)) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Retry parameters config used but not defined: %s (in method %s)", retryParamsName, methodModel.getFullName()));
        error = true;
    }
    Duration timeout = Duration.ofMillis(methodConfigProto.getTimeoutMillis());
    if (timeout.toMillis() <= 0) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Default timeout not found or has invalid value (in method %s)", methodModel.getFullName()));
        error = true;
    }
    boolean hasRequestObjectMethod = methodConfigProto.getRequestObjectMethod();
    ImmutableMap<String, String> fieldNamePatterns = ImmutableMap.copyOf(methodConfigProto.getFieldNamePatterns());
    ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
    if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
        defaultResourceNameTreatment = ResourceNameTreatment.NONE;
    }
    Iterable<FieldConfig> requiredFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getRequiredFields(diagCollector, methodModel, methodConfigProto.getRequiredFieldsList()));
    Iterable<FieldConfig> optionalFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getOptionalFields(methodModel, methodConfigProto.getRequiredFieldsList()));
    List<String> sampleCodeInitFields = new ArrayList<>();
    sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());
    VisibilityConfig visibility = VisibilityConfig.PUBLIC;
    ReleaseLevel releaseLevel = ReleaseLevel.ALPHA;
    for (SurfaceTreatmentProto treatment : methodConfigProto.getSurfaceTreatmentsList()) {
        if (!treatment.getIncludeLanguagesList().contains(language)) {
            continue;
        }
        if (treatment.getVisibility() != VisibilityProto.UNSET_VISIBILITY) {
            visibility = VisibilityConfig.fromProto(treatment.getVisibility());
        }
        if (treatment.getReleaseLevel() != ReleaseLevel.UNSET_RELEASE_LEVEL) {
            releaseLevel = treatment.getReleaseLevel();
        }
    }
    LongRunningConfig longRunningConfig = null;
    if (error) {
        return null;
    } else {
        return new AutoValue_DiscoGapicMethodConfig(methodModel, pageStreaming, flattening, retryCodesName, retryParamsName, timeout, requiredFieldConfigs, optionalFieldConfigs, defaultResourceNameTreatment, batching, hasRequestObjectMethod, fieldNamePatterns, sampleCodeInitFields, visibility, releaseLevel, longRunningConfig);
    }
}
Also used : SurfaceTreatmentProto(com.google.api.codegen.SurfaceTreatmentProto) ArrayList(java.util.ArrayList) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) Duration(org.threeten.bp.Duration) ReleaseLevel(com.google.api.codegen.ReleaseLevel) DiagCollector(com.google.api.tools.framework.model.DiagCollector) Nullable(javax.annotation.Nullable)

Example 3 with ResourceNameTreatment

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

the class FlatteningConfig method createFlattening.

/**
 * Creates an instance of FlatteningConfig based on a FlatteningGroupProto, linking it up with the
 * provided method.
 */
@Nullable
static FlatteningConfig createFlattening(DiagCollector diagCollector, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, MethodConfigProto methodConfigProto, FlatteningGroupProto flatteningGroup, MethodModel method) {
    boolean missing = false;
    ImmutableMap.Builder<String, FieldConfig> flattenedFieldConfigBuilder = ImmutableMap.builder();
    Set<String> oneofNames = new HashSet<>();
    for (String parameter : flatteningGroup.getParametersList()) {
        FieldModel parameterField = method.getInputField(parameter);
        if (parameterField == null) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Field missing for flattening: method = %s, message type = %s, field = %s", method.getFullName(), method.getInputFullName(), parameter));
            return null;
        }
        Oneof oneof = parameterField.getOneof();
        if (oneof != null) {
            String oneofName = oneof.getName();
            if (oneofNames.contains(oneofName)) {
                diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Value from oneof already specifed for flattening:%n" + "method = %s, message type = %s, oneof = %s", method.getFullName(), method.getInputFullName(), oneofName));
                return null;
            }
            oneofNames.add(oneofName);
        }
        ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
        if (!parameterField.mayBeInResourceName()) {
            defaultResourceNameTreatment = ResourceNameTreatment.NONE;
        }
        if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
            defaultResourceNameTreatment = ResourceNameTreatment.VALIDATE;
        }
        FieldConfig fieldConfig = FieldConfig.createFieldConfig(diagCollector, messageConfigs, methodConfigProto.getFieldNamePatternsMap(), resourceNameConfigs, parameterField, flatteningGroup.getParameterResourceNameTreatmentMap().get(parameter), defaultResourceNameTreatment);
        if (fieldConfig == null) {
            missing = true;
        } else {
            flattenedFieldConfigBuilder.put(parameter, fieldConfig);
        }
    }
    if (missing) {
        return null;
    }
    return new AutoValue_FlatteningConfig(flattenedFieldConfigBuilder.build(), flatteningGroup.getFlatteningGroupName());
}
Also used : Oneof(com.google.api.tools.framework.model.Oneof) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Aggregations

ResourceNameTreatment (com.google.api.codegen.ResourceNameTreatment)3 Nullable (javax.annotation.Nullable)3 ReleaseLevel (com.google.api.codegen.ReleaseLevel)2 SurfaceTreatmentProto (com.google.api.codegen.SurfaceTreatmentProto)2 ArrayList (java.util.ArrayList)2 Duration (org.threeten.bp.Duration)2 DiagCollector (com.google.api.tools.framework.model.DiagCollector)1 Oneof (com.google.api.tools.framework.model.Oneof)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashSet (java.util.HashSet)1