use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class JavaDiscoGapicSchemaToViewTransformer method generateSchemaClasses.
private StaticLangApiMessageView generateSchemaClasses(Map<SchemaTransformationContext, StaticLangApiMessageView> messageViewAccumulator, DiscoGapicInterfaceContext documentContext, Schema schema) {
FieldModel schemaModel = DiscoveryField.create(schema, documentContext.getApiModel());
SchemaTypeTable schemaTypeTable = documentContext.getSchemaTypeTable().cloneEmpty();
SchemaTransformationContext context = SchemaTransformationContext.create(schema.getIdentifier(), schemaTypeTable, documentContext);
StaticLangApiMessageView.Builder schemaView = StaticLangApiMessageView.newBuilder();
boolean hasRequiredProperties = false;
// Child schemas cannot have the same symbols as parent schemas, but sibling schemas can have
// the same symbols.
SymbolTable symbolTableCopy = SymbolTable.fromSeed(reservedKeywords);
String schemaId = Name.anyCamel(schema.getIdentifier()).toLowerCamel();
String schemaName = nameFormatter.privateFieldName(Name.anyCamel(symbolTableCopy.getNewSymbol(schemaId)));
schemaView.name(schemaName);
schemaView.defaultValue(schema.defaultValue());
schemaView.description(schema.description());
schemaView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(schemaModel));
schemaView.fieldSetFunction(context.getNamer().getFieldSetFunctionName(schemaModel));
schemaView.fieldAddFunction(context.getNamer().getFieldAddFunctionName(schemaModel));
String schemaTypeName = schemaTypeTable.getAndSaveNicknameFor(schema);
schemaView.typeName(schemaTypeName);
if (schema.repeated() || schema.type() == Type.ARRAY) {
schemaView.innerTypeName(schemaTypeTable.getInnerTypeNameFor(schema));
} else {
schemaView.innerTypeName(schemaTypeName);
}
// Generate a Schema view from each property.
List<StaticLangApiMessageView> viewProperties = new LinkedList<>();
List<Schema> schemaProperties = new LinkedList<>();
schemaProperties.addAll(schema.properties().values());
if (schema.items() != null) {
schemaProperties.addAll(schema.items().properties().values());
}
for (Schema property : schemaProperties) {
viewProperties.add(generateSchemaClasses(messageViewAccumulator, documentContext, property));
if (!property.properties().isEmpty() || (property.items() != null)) {
// Add non-primitive-type property to imports.
schemaTypeTable.getAndSaveNicknameFor(property);
}
if (property.required()) {
hasRequiredProperties = true;
}
}
Collections.sort(viewProperties);
schemaView.properties(viewProperties);
schemaView.canRepeat(schema.repeated() || schema.type().equals(Type.ARRAY));
schemaView.isRequired(schema.required());
schemaView.isRequestMessage(false);
schemaView.hasRequiredProperties(hasRequiredProperties);
if (!schema.properties().isEmpty() || (schema.items() != null && !schema.items().properties().isEmpty())) {
// This is a top-level Schema, so add it to list of file ViewModels for rendering.
messageViewAccumulator.put(context, schemaView.build());
}
return schemaView.build();
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class InitCodeNode method buildSubTrees.
private static List<InitCodeNode> buildSubTrees(InitCodeContext context) {
List<InitCodeNode> subTrees = new ArrayList<>();
if (context.initFieldConfigStrings() != null) {
for (String initFieldConfigString : context.initFieldConfigStrings()) {
subTrees.add(FieldStructureParser.parse(initFieldConfigString, context.initValueConfigMap()));
}
}
if (context.initFields() != null) {
// Add items in fieldSet to newSubTrees in case they were not included in
// sampleCodeInitFields, and to ensure the order is determined by initFields
List<InitCodeNode> newSubTrees = new ArrayList<>();
for (FieldModel field : context.initFields()) {
String nameString = field.getNameAsParameter();
InitValueConfig initValueConfig = context.initValueConfigMap().get(nameString);
if (initValueConfig == null) {
newSubTrees.add(InitCodeNode.createWithName(nameString, field.getNameAsParameter()));
} else {
newSubTrees.add(InitCodeNode.createWithValue(nameString, initValueConfig));
}
}
newSubTrees.addAll(subTrees);
subTrees = newSubTrees;
} else if (context.outputType() == InitCodeOutputType.FieldList) {
throw new IllegalArgumentException("Init field array is not set for flattened method.");
}
if (context.additionalInitCodeNodes() != null) {
subTrees.addAll(Lists.newArrayList(context.additionalInitCodeNodes()));
}
return subTrees;
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class ApiCallableTransformer method generateApiCallableSettings.
public ApiCallSettingsView generateApiCallableSettings(MethodContext context, ApiCallableImplType callableImplType) {
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
MethodModel method = context.getMethodModel();
MethodConfig methodConfig = context.getMethodConfig();
Map<String, RetryCodesDefinitionView> retryCodesByKey = new HashMap<>();
for (RetryCodesDefinitionView retryCodes : retryDefinitionsTransformer.generateRetryCodesDefinitions(context.getSurfaceInterfaceContext())) {
retryCodesByKey.put(retryCodes.key(), retryCodes);
}
Map<String, RetryParamsDefinitionView> retryParamsByKey = new HashMap<>();
for (RetryParamsDefinitionView retryParams : retryDefinitionsTransformer.generateRetryParamsDefinitions(context.getSurfaceInterfaceContext())) {
retryParamsByKey.put(retryParams.key(), retryParams);
}
ApiCallSettingsView.Builder settings = ApiCallSettingsView.newBuilder();
settings.methodName(namer.getApiMethodName(method, VisibilityConfig.PUBLIC));
settings.asyncMethodName(namer.getAsyncApiMethodName(method, VisibilityConfig.PUBLIC));
settings.requestTypeName(method.getAndSaveRequestTypeName(context.getTypeTable(), context.getNamer()));
settings.responseTypeName(method.getAndSaveResponseTypeName(context.getTypeTable(), context.getNamer()));
settings.grpcTypeName(typeTable.getAndSaveNicknameFor(context.getGrpcContainerTypeName()));
settings.grpcMethodConstant(namer.getGrpcMethodConstant(method));
settings.retryCodesName(methodConfig.getRetryCodesConfigName());
settings.retryCodesView(retryCodesByKey.get(methodConfig.getRetryCodesConfigName()));
settings.retryParamsName(methodConfig.getRetrySettingsConfigName());
settings.retryParamsView(retryParamsByKey.get(methodConfig.getRetrySettingsConfigName()));
settings.visibility(namer.getVisiblityKeyword(methodConfig.getVisibility()));
String notImplementedPrefix = "ApiCallableTransformer.generateApiCallableSettings - ";
settings.resourceTypeName(namer.getNotImplementedString(notImplementedPrefix + "resourceTypeName"));
settings.pagedListResponseTypeName(namer.getNotImplementedString(notImplementedPrefix + "pagedListResponseTypeName"));
settings.pageStreamingDescriptorName(namer.getNotImplementedString(notImplementedPrefix + "pageStreamingDescriptorName"));
settings.pagedListResponseFactoryName(namer.getNotImplementedString(notImplementedPrefix + "pagedListResponseFactoryName"));
settings.batchingDescriptorName(namer.getNotImplementedString(notImplementedPrefix + "batchingDescriptorName"));
settings.memberName(namer.getSettingsMemberName(method));
settings.settingsGetFunction(namer.getSettingsFunctionName(method));
settings.type(callableImplType);
switch(callableImplType) {
case BidiStreamingApiCallable:
case ClientStreamingApiCallable:
case ServerStreamingApiCallable:
settings.type(ApiCallableImplType.of(methodConfig.getGrpcStreamingType()));
if (methodConfig.getGrpcStreaming().hasResourceField()) {
FieldModel resourceType = methodConfig.getGrpcStreaming().getResourcesField();
settings.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceType));
}
settings.grpcStreamingType(methodConfig.getGrpcStreaming().getType());
break;
case PagedApiCallable:
settings.type(ApiCallableImplType.PagedApiCallable);
FieldModel resourceField = methodConfig.getPageStreaming().getResourcesField();
settings.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceField));
settings.pagedListResponseTypeName(namer.getAndSavePagedResponseTypeName(context, methodConfig.getPageStreaming().getResourcesFieldConfig()));
settings.pageStreamingDescriptorName(namer.getPageStreamingDescriptorConstName(method));
settings.pagedListResponseFactoryName(namer.getPagedListResponseFactoryConstName(method));
break;
case BatchingApiCallable:
settings.type(ApiCallableImplType.BatchingApiCallable);
settings.batchingDescriptorName(namer.getBatchingDescriptorConstName(method));
settings.batchingConfig(batchingTransformer.generateBatchingConfig(context));
break;
case OperationApiCallable:
settings.type(ApiCallableImplType.OperationApiCallable);
settings.operationMethod(lroTransformer.generateDetailView(context));
settings.memberName(namer.getOperationSettingsMemberName(method));
settings.settingsGetFunction(namer.getOperationSettingsFunctionName(method));
break;
case SimpleApiCallable:
break;
default:
throw new IllegalStateException("Unhandled ApiCallableImplType enum value: " + callableImplType);
}
return settings.build();
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class BatchingTransformer method generateDiscriminatorFieldCopies.
private List<FieldCopyView> generateDiscriminatorFieldCopies(MethodContext context) {
List<FieldCopyView> fieldCopies = new ArrayList<>();
BatchingConfig batching = context.getMethodConfig().getBatching();
for (GenericFieldSelector fieldSelector : batching.getDiscriminatorFields()) {
FieldModel selectedType = fieldSelector.getLastField();
FieldCopyView fieldCopy = FieldCopyView.newBuilder().fieldGetFunction(context.getNamer().getFieldGetFunctionName(selectedType)).fieldSetFunction(context.getNamer().getFieldSetFunctionName(selectedType)).build();
fieldCopies.add(fieldCopy);
}
return fieldCopies;
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class BatchingTransformer method generatePartitionKeys.
private List<BatchingPartitionKeyView> generatePartitionKeys(MethodContext context) {
List<BatchingPartitionKeyView> keys = new ArrayList<>();
BatchingConfig batching = context.getMethodConfig().getBatching();
for (GenericFieldSelector fieldSelector : batching.getDiscriminatorFields()) {
FieldModel selectedType = fieldSelector.getLastField();
BatchingPartitionKeyView key = BatchingPartitionKeyView.newBuilder().fieldGetFunction(context.getNamer().getFieldGetFunctionName(selectedType)).build();
keys.add(key);
}
return keys;
}
Aggregations