use of com.google.api.codegen.transformer.SurfaceNamer in project toolkit by googleapis.
the class CSharpGapicClientTransformer method generateResourceProtos.
private List<ResourceProtoView> generateResourceProtos(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
ResourceNameMessageConfigs resourceConfigs = context.getProductConfig().getResourceNameMessageConfigs();
String csharpDefaultNamespace = context.getProductConfig().getPackageName();
ListMultimap<String, FieldModel> fieldsByMessage = resourceConfigs.getFieldsWithResourceNamesByMessage();
Map<String, FieldConfig> fieldConfigMap = context.getProductConfig().getDefaultResourceNameFieldConfigMap();
List<ResourceProtoView> protos = new ArrayList<>();
// Get the default proto package name for this interface.
String defaultPackageName = context.getInterfaceModel().getParentFullName();
for (Entry<String, Collection<FieldModel>> entry : fieldsByMessage.asMap().entrySet()) {
String msgName = entry.getKey();
if (!msgName.startsWith(defaultPackageName)) {
// If the proto is not in this package, don't generate a partial class for it.
continue;
}
// msgName is the "<proto package name>.<C# proto class name>".
// Remove the proto package name, and prepend the C# namespace
msgName = csharpDefaultNamespace + msgName.substring(msgName.lastIndexOf('.'));
Collection<FieldModel> fields = new ArrayList<>(entry.getValue());
ResourceProtoView.Builder protoBuilder = ResourceProtoView.newBuilder();
protoBuilder.protoClassName(namer.getTypeNameConverter().getTypeName(msgName).getNickname());
List<ResourceProtoFieldView> fieldViews = new ArrayList<>();
for (FieldModel field : fields) {
FieldConfig fieldConfig = fieldConfigMap.get(field.getFullName());
ResourceNameConfig resourceNameConfig = fieldConfig.getResourceNameConfig();
String fieldTypeSimpleName = namer.getResourceTypeName(resourceNameConfig);
boolean isAny = false;
if (fieldTypeSimpleName.equals("IResourceName")) {
fieldTypeSimpleName = CSharpTypeTable.ALIAS_GAX + "::IResourceName";
isAny = true;
}
String fieldTypeName = context.getImportTypeTable().getAndSaveNicknameForTypedResourceName(fieldConfig, fieldTypeSimpleName);
if (field.isRepeated()) {
fieldTypeName = fieldTypeName.replaceFirst(CSharpTypeTable.ALIAS_SYSTEM_COLLECTIONS_GENERIC + "::IEnumerable", CSharpTypeTable.ALIAS_GAX + "::ResourceNameList");
} else if (resourceNameConfig.getCommonResourceName() == null && !isAny) {
// Needs to be fully qualifed because the 'fieldTypeName' class name will be
// the same as a property name on this proto message.
fieldTypeName = namer.getPackageName() + "." + fieldTypeName;
}
String fieldDocTypeName = fieldTypeName.replace('<', '{').replace('>', '}');
String fieldElementTypeName = context.getImportTypeTable().getAndSaveNicknameForResourceNameElementType(fieldConfig, fieldTypeSimpleName);
ResourceProtoFieldView fieldView = ResourceProtoFieldView.newBuilder().typeName(fieldTypeName).parseMethodTypeName(fieldTypeName).docTypeName(fieldDocTypeName).elementTypeName(fieldElementTypeName).isAny(fieldConfig.getResourceNameType() == ResourceNameType.ANY).isRepeated(field.isRepeated()).isOneof(fieldConfig.getResourceNameType() == ResourceNameType.ONEOF).propertyName(namer.getResourceNameFieldGetFunctionName(fieldConfig)).underlyingPropertyName(namer.publicMethodName(Name.from(field.getSimpleName()))).build();
fieldViews.add(fieldView);
}
protoBuilder.fields(fieldViews);
protos.add(protoBuilder.build());
}
// Sort alphabetically, to make generated code deterministic.
Collections.sort(protos, new Comparator<ResourceProtoView>() {
@Override
public int compare(ResourceProtoView a, ResourceProtoView b) {
return a.protoClassName().compareTo(b.protoClassName());
}
});
return protos;
}
use of com.google.api.codegen.transformer.SurfaceNamer in project toolkit by googleapis.
the class CSharpGapicClientTransformer method generateModifyMethods.
private List<ModifyMethodView> generateModifyMethods(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
List<ModifyMethodView> modifyMethods = new ArrayList<>();
Set<String> modifyTypeNames = new HashSet<>();
for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
MethodContext methodContext = context.asRequestMethodContext(method);
String inputTypeFullName = methodContext.getMethodModel().getInputFullName();
if (modifyTypeNames.contains(inputTypeFullName)) {
continue;
}
modifyTypeNames.add(inputTypeFullName);
MethodConfig methodConfig = methodContext.getMethodConfig();
ModifyMethodView.Builder builder = ModifyMethodView.builder();
builder.name(namer.getModifyMethodName(methodContext));
builder.requestTypeName(method.getAndSaveRequestTypeName(context.getImportTypeTable(), context.getNamer()));
builder.grpcStreamingType(methodConfig.getGrpcStreamingType());
modifyMethods.add(builder.build());
}
return modifyMethods;
}
use of com.google.api.codegen.transformer.SurfaceNamer in project toolkit by googleapis.
the class CSharpGapicClientTransformer method generateSettingsDoc.
public SettingsDocView generateSettingsDoc(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
SettingsDocView.Builder settingsDoc = SettingsDocView.newBuilder();
settingsDoc.serviceHostname(productServiceConfig.getServiceHostname(context.getServiceAddress()));
settingsDoc.servicePort(productServiceConfig.getServicePort(context.getServiceAddress()));
// Unused in C#
settingsDoc.exampleApiMethodName("");
// Unused in C#
settingsDoc.exampleApiMethodSettingsGetter("");
// Unused in C#
settingsDoc.exampleApiMethodSettingsHasRetrySettings(true);
settingsDoc.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
settingsDoc.settingsVarName(namer.getApiSettingsVariableName(context.getInterfaceConfig()));
settingsDoc.settingsClassName(namer.getApiSettingsClassName(context.getInterfaceConfig()));
settingsDoc.settingsBuilderVarName(namer.getApiSettingsBuilderVarName(context.getInterfaceConfig()));
settingsDoc.hasDefaultInstance(context.getInterfaceConfig().hasDefaultInstance());
return settingsDoc.build();
}
use of com.google.api.codegen.transformer.SurfaceNamer in project toolkit by googleapis.
the class CSharpGapicClientTransformer method generateApiClass.
private StaticLangApiView generateApiClass(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
StaticLangApiView.Builder apiClass = StaticLangApiView.newBuilder();
List<StaticLangApiMethodView> methods = apiMethodTransformer.generateApiMethods(context);
apiClass.doc(serviceTransformer.generateServiceDoc(context, null, context.getProductConfig()));
apiClass.name(namer.getApiWrapperClassName(context.getInterfaceConfig()));
apiClass.implName(namer.getApiWrapperClassImplName(context.getInterfaceConfig()));
apiClass.grpcServiceName(namer.getGrpcContainerTypeName(context.getInterfaceModel()));
String grpcTypeName = namer.getGrpcServiceClassName(context.getInterfaceModel());
int dotIndex = grpcTypeName.indexOf('.');
apiClass.grpcTypeNameOuter(grpcTypeName.substring(0, dotIndex));
apiClass.grpcTypeNameInner(grpcTypeName.substring(dotIndex + 1, grpcTypeName.length()));
apiClass.settingsClassName(context.getNamer().getApiSettingsClassName(context.getInterfaceConfig()));
List<ApiCallableView> callables = new ArrayList<>();
for (ApiCallableView call : apiCallableTransformer.generateStaticLangApiCallables(context)) {
if (call.type() == ApiCallableImplType.SimpleApiCallable || call.type() == ApiCallableImplType.BatchingApiCallable || call.type() == ApiCallableImplType.BidiStreamingApiCallable || call.type() == ApiCallableImplType.ServerStreamingApiCallable) {
callables.add(call);
}
}
apiClass.apiCallableMembers(callables);
apiClass.pathTemplates(pathTemplateTransformer.generatePathTemplates(context));
apiClass.formatResourceFunctions(pathTemplateTransformer.generateFormatResourceFunctions(context));
apiClass.parseResourceFunctions(pathTemplateTransformer.generateParseResourceFunctions(context));
apiClass.apiMethods(methods);
List<StaticLangApiMethodView> methodsImpl = new ArrayList<>();
for (StaticLangApiMethodView method : methods) {
if (methodTypeHasImpl(method.type())) {
methodsImpl.add(method);
}
}
apiClass.apiMethodsImpl(methodsImpl);
apiClass.hasDefaultInstance(context.getInterfaceConfig().hasDefaultInstance());
apiClass.hasLongRunningOperations(context.getInterfaceConfig().hasLongRunningOperations());
apiClass.reroutedGrpcClients(csharpCommonTransformer.generateReroutedGrpcView(context));
apiClass.modifyMethods(generateModifyMethods(context));
apiClass.apiHasUnaryMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.NonStreaming));
apiClass.apiHasServerStreamingMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.ServerStreaming));
apiClass.apiHasClientStreamingMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.ClientStreaming));
apiClass.apiHasBidiStreamingMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.BidiStreaming));
return apiClass.build();
}
use of com.google.api.codegen.transformer.SurfaceNamer in project toolkit by googleapis.
the class CSharpGapicSnippetsTransformer method generatePagedRequestAsyncMethod.
private StaticLangApiMethodSnippetView generatePagedRequestAsyncMethod(MethodContext methodContext) {
StaticLangApiMethodView method = generateInitCode(apiMethodTransformer.generatePagedRequestObjectAsyncMethod(methodContext, csharpCommonTransformer.pagedMethodAdditionalParams(), null), methodContext, methodContext.getMethodConfig().getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, CallingForm.RequestAsyncPaged);
SurfaceNamer namer = methodContext.getNamer();
PageStreamingConfig pageStreaming = methodContext.getMethodConfig().getPageStreaming();
FieldConfig resourceFieldConfig = pageStreaming.getResourcesFieldConfig();
String callerResponseTypeName = namer.getAndSaveCallerAsyncPagedResponseTypeName(methodContext, resourceFieldConfig);
return StaticLangApiMethodSnippetView.newBuilder().method(method).snippetMethodName(method.name() + "_RequestObject").callerResponseTypeName(callerResponseTypeName).apiClassName(namer.getApiWrapperClassName(methodContext.getInterfaceConfig())).apiVariableName(method.apiVariableName()).build();
}
Aggregations