use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class PageStreamingConfig method createPageStreaming.
/**
* Creates an instance of PageStreamingConfig based on Discovery Doc, linking it up with the
* provided method. On errors, null will be returned, and diagnostics are reported to the diag
* collector.
*
* @param method Method descriptor for the method to create config for.
*/
@Nullable
static // TODO(andrealin): Merge this function into the createPageStreaming(... Method protoMethod) function.
PageStreamingConfig createPageStreaming(DiscoApiModel apiModel, com.google.api.codegen.discovery.Method method) {
Schema requestTokenField = method.parameters().get(PARAMETER_PAGE_TOKEN);
DiagCollector diagCollector = apiModel.getDiagCollector();
if (requestTokenField == null) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Request field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_PAGE_TOKEN));
}
Schema pageSizeField = method.parameters().get(PARAMETER_MAX_RESULTS);
if (pageSizeField != null) {
pageSizeField = method.parameters().get(PARAMETER_MAX_RESULTS);
if (pageSizeField == null) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Request field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_MAX_RESULTS));
}
}
Schema responseTokenField = null;
Schema responseSchema = method.response().dereference();
if (responseSchema.hasProperty(PARAMETER_NEXT_PAGE_TOKEN)) {
responseTokenField = responseSchema.properties().get(PARAMETER_NEXT_PAGE_TOKEN);
}
if (responseTokenField == null) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Response field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_NEXT_PAGE_TOKEN));
}
Schema responseField = method.response().dereference();
ImmutableList<FieldModel> resourcesFieldPath = ImmutableList.copyOf(getResourcesGetterPath(responseField, apiModel));
FieldConfig resourcesFieldConfig;
if (resourcesFieldPath.isEmpty()) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Resources field missing for page streaming: method = %s, message type = %s, response field = %s", method.id(), method.id(), method.response() == null ? "null" : method.response().toString()));
resourcesFieldConfig = null;
} else {
FieldModel resourcesField = resourcesFieldPath.get(resourcesFieldPath.size() - 1);
resourcesFieldConfig = FieldConfig.createFieldConfig(resourcesField, ImmutableList.copyOf(resourcesFieldPath));
}
if (requestTokenField == null || responseTokenField == null || resourcesFieldConfig == null) {
return null;
}
return new AutoValue_PageStreamingConfig(DiscoveryField.create(requestTokenField, apiModel), DiscoveryField.create(pageSizeField, apiModel), DiscoveryField.create(responseTokenField, apiModel), resourcesFieldConfig);
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class DiscoConfigBaselineTestCase method test.
/**
* Run a test for the given file base name(s). Collects all .proto and .yaml files with the given
* base name (i.e. baseName.proto or baseName.yaml), constructs model, and calls {@link #run()}.
* Post that, prints diags and the result of the run to the baseline.
*/
protected void test() throws Exception {
DiagCollector diagCollector = new BoundedDiagCollector();
// Run test specific logic.
Object result = run();
if (!diagCollector.hasErrors() && result != null) {
// Output the result depending on its type.
if (result instanceof Map) {
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) result;
for (Map.Entry<String, Object> entry : map.entrySet()) {
testOutput().printf("============== file: %s ==============%n", entry.getKey());
testOutput().println(displayValue(entry.getValue()));
}
} else {
testOutput().println(displayValue(result));
}
}
}
use of com.google.api.tools.framework.model.DiagCollector 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, TargetLanguage language, MethodConfigProto methodConfigProto, Method method, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, RetryCodesConfig retryCodesConfig, 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.createPageStreamingFromGapicConfig(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
if (pageStreaming == null) {
error = true;
}
}
ImmutableList<FlatteningConfig> flattening = null;
if (!FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
flattening = FlatteningConfig.createFlatteningConfigs(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 = retryCodesConfig.getMethodRetryNames().get(methodConfigProto.getName());
String retryParamsName = RetryDefinitionsTransformer.getRetryParamsName(methodConfigProto, diagCollector, retryParamsConfigNames);
error |= (retryParamsName == null);
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;
}
ImmutableListMultimap<String, String> fieldNamePatterns = ImmutableListMultimap.copyOf(methodConfigProto.getFieldNamePatterns().entrySet());
ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
defaultResourceNameTreatment = ResourceNameTreatment.NONE;
}
List<String> requiredFieldsList = Lists.newArrayList(methodConfigProto.getRequiredFieldsList());
if (methodModel.hasExtraFieldMask()) {
requiredFieldsList.add(DiscoveryMethodTransformer.FIELDMASK_STRING);
}
ImmutableList<FieldConfig> requiredFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getRequiredFields(diagCollector, methodModel, requiredFieldsList));
ImmutableList<FieldConfig> optionalFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getOptionalFields(methodModel, requiredFieldsList));
List<String> sampleCodeInitFields = new ArrayList<>();
sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());
SampleSpec sampleSpec = new SampleSpec(methodConfigProto);
VisibilityConfig visibility = VisibilityConfig.PUBLIC;
ReleaseLevel releaseLevel = ReleaseLevel.ALPHA;
for (SurfaceTreatmentProto treatment : methodConfigProto.getSurfaceTreatmentsList()) {
if (!treatment.getIncludeLanguagesList().contains(language.toString().toLowerCase())) {
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(pageStreaming, flattening, retryCodesName, retryParamsName, timeout, requiredFieldConfigs, optionalFieldConfigs, batching, fieldNamePatterns, sampleCodeInitFields, sampleSpec, visibility, releaseLevel, longRunningConfig, methodModel);
}
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class LongRunningConfigTest method testCreateLROWithAnnotationsOverridingGapicConfig.
@Test
public void testCreateLROWithAnnotationsOverridingGapicConfig() {
DiagCollector diagCollector = new BoundedDiagCollector();
Mockito.when(protoParser.isProtoAnnotationsEnabled()).thenReturn(true);
// lroAnnotatedMethod contains different settings than that in lroConfigProtoWithPollSettings.
LongRunningConfig longRunningConfig = LongRunningConfig.createLongRunningConfig(lroAnnotatedMethod, diagCollector, lroConfigProtoWithPollSettings, protoParser);
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
assertThat(longRunningConfig).isNotNull();
// Assert that proto annotations settings take precendence over gapic config for
// return and metadata types.
ProtoTypeRef metadataTypeModel = (ProtoTypeRef) longRunningConfig.getMetadataType();
assertThat(metadataTypeModel.getProtoType()).isEqualTo(annotationsMetadataType);
ProtoTypeRef returnTypeModel = (ProtoTypeRef) longRunningConfig.getReturnType();
assertThat(returnTypeModel.getProtoType()).isEqualTo(annotationsReturnType);
// Assert that GAPIC config timeout values are used.
assertThat(longRunningConfig.getInitialPollDelay().toMillis()).isEqualTo(TEST_INITIAL_POLL_DELAY);
assertThat(longRunningConfig.getMaxPollDelay().toMillis()).isEqualTo(TEST_MAX_POLL_DELAY);
assertThat(longRunningConfig.getPollDelayMultiplier()).isEqualTo(TEST_POLL_DELAY_MULTIPLIER);
assertThat(longRunningConfig.getTotalPollTimeout().toMillis()).isEqualTo(TEST_TOTAL_POLL_TIMEOUT);
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class LongRunningConfigTest method testCreateLROWithoutGapicConfig.
@Test
public void testCreateLROWithoutGapicConfig() {
Mockito.when(protoParser.isProtoAnnotationsEnabled()).thenReturn(true);
DiagCollector diagCollector = new BoundedDiagCollector();
LongRunningConfig longRunningConfig = LongRunningConfig.createLongRunningConfig(lroAnnotatedMethod, diagCollector, LongRunningConfigProto.getDefaultInstance(), protoParser);
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
assertThat(longRunningConfig).isNotNull();
ProtoTypeRef metadataTypeModel = (ProtoTypeRef) longRunningConfig.getMetadataType();
assertThat(metadataTypeModel.getProtoType()).isEqualTo(annotationsMetadataType);
ProtoTypeRef returnTypeModel = (ProtoTypeRef) longRunningConfig.getReturnType();
assertThat(returnTypeModel.getProtoType()).isEqualTo(annotationsReturnType);
assertThat(longRunningConfig.getInitialPollDelay()).isEqualTo(LongRunningConfig.LRO_INITIAL_POLL_DELAY_MILLIS);
assertThat(longRunningConfig.getMaxPollDelay()).isEqualTo(LongRunningConfig.LRO_MAX_POLL_DELAY_MILLIS);
assertThat(longRunningConfig.getPollDelayMultiplier()).isEqualTo(LongRunningConfig.LRO_POLL_DELAY_MULTIPLIER);
assertThat(longRunningConfig.getTotalPollTimeout()).isEqualTo(LongRunningConfig.LRO_TOTAL_POLL_TIMEOUT_MILLS);
}
Aggregations