use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method generateAsyncOperationRequestObjectMethod.
public StaticLangApiMethodView generateAsyncOperationRequestObjectMethod(MethodContext context, List<ParamWithSimpleDoc> additionalParams, boolean requiresOperationMethod) {
MethodModel method = context.getMethodModel();
SurfaceNamer namer = context.getNamer();
StaticLangApiMethodView.Builder methodViewBuilder = StaticLangApiMethodView.newBuilder();
setCommonFields(context, methodViewBuilder);
methodViewBuilder.name(namer.getAsyncApiMethodName(context.getMethodModel(), context.getMethodConfig().getVisibility()));
methodViewBuilder.exampleName(namer.getAsyncApiMethodExampleName(method));
setRequestObjectMethodFields(context, namer.getOperationCallableMethodName(method), Synchronicity.Async, additionalParams, methodViewBuilder);
if (requiresOperationMethod) {
// Only for protobuf-based APIs.
methodViewBuilder.operationMethod(lroTransformer.generateDetailView(context));
}
if (context.getMethodConfig().isLongRunningOperation()) {
// Only for protobuf-based APIs.
TypeModel returnType = context.getMethodConfig().getLongRunningConfig().getReturnType();
methodViewBuilder.responseTypeName(context.getTypeTable().getAndSaveNicknameFor(returnType));
methodViewBuilder.operationMethod(lroTransformer.generateDetailView(context));
} else {
throw new IllegalArgumentException("Discovery-based APIs do not have LongRunning operations.");
}
return methodViewBuilder.type(ClientMethodType.AsyncOperationRequestObjectMethod).build();
}
use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method generateOperationRequestObjectMethod.
public StaticLangApiMethodView generateOperationRequestObjectMethod(MethodContext context, List<ParamWithSimpleDoc> additionalParams) {
MethodModel method = context.getMethodModel();
SurfaceNamer namer = context.getNamer();
StaticLangApiMethodView.Builder methodViewBuilder = StaticLangApiMethodView.newBuilder();
setCommonFields(context, methodViewBuilder);
methodViewBuilder.name(namer.getApiMethodName(method, context.getMethodConfig().getVisibility()));
methodViewBuilder.exampleName(namer.getApiMethodExampleName(context.getInterfaceConfig(), method));
setRequestObjectMethodFields(context, namer.getCallableMethodName(method), Synchronicity.Sync, additionalParams, methodViewBuilder);
methodViewBuilder.operationMethod(lroTransformer.generateDetailView(context));
TypeModel returnType = context.getMethodConfig().getLongRunningConfig().getReturnType();
methodViewBuilder.responseTypeName(context.getTypeTable().getAndSaveNicknameFor(returnType));
return methodViewBuilder.type(ClientMethodType.OperationRequestObjectMethod).build();
}
use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method generateOperationFlattenedMethod.
public StaticLangApiMethodView generateOperationFlattenedMethod(MethodContext context, List<ParamWithSimpleDoc> additionalParams) {
MethodModel method = context.getMethodModel();
SurfaceNamer namer = context.getNamer();
StaticLangApiMethodView.Builder methodViewBuilder = StaticLangApiMethodView.newBuilder();
setCommonFields(context, methodViewBuilder);
methodViewBuilder.name(namer.getApiMethodName(method, context.getMethodConfig().getVisibility()));
methodViewBuilder.exampleName(namer.getApiMethodExampleName(context.getInterfaceConfig(), method));
methodViewBuilder.callableName(namer.getCallableName(method));
setFlattenedMethodFields(context, additionalParams, Synchronicity.Sync, methodViewBuilder);
methodViewBuilder.operationMethod(lroTransformer.generateDetailView(context));
TypeModel returnType = context.getMethodConfig().getLongRunningConfig().getReturnType();
methodViewBuilder.responseTypeName(context.getTypeTable().getAndSaveNicknameFor(returnType));
return methodViewBuilder.type(ClientMethodType.OperationFlattenedMethod).build();
}
use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.
the class CSharpSampleImportTransformer method addOutputImports.
@Override
protected void addOutputImports(MethodContext context, OutputContext outputContext) {
ImportTypeTable typeTable = context.getTypeTable();
OutputTransformer.ScopeTable scopeTable = outputContext.scopeTable();
for (TypeModel type : scopeTable.allTypes()) {
if (type == null) {
saveResourceTypeName(context);
} else {
typeTable.getAndSaveNicknameFor(type);
}
}
if (outputContext.hasMaps()) {
typeTable.getAndSaveNicknameFor("System.Collections.Generic.KeyValuePair");
}
if (outputContext.hasBytesFileOutput()) {
typeTable.getAndSaveNicknameFor("System.Linq.Enumerable");
typeTable.getAndSaveNicknameFor("System.IO.File");
}
if (outputContext.hasStringFileOutput()) {
typeTable.getAndSaveNicknameFor("System.IO.File");
}
if (outputContext.hasPrints()) {
typeTable.getAndSaveNicknameFor("System.Console");
}
}
use of com.google.api.codegen.config.TypeModel in project toolkit by googleapis.
the class OutputTransformer method accessorNewVariable.
/**
* Parses config and returns accessor the config describes.
*
* <p>The config is type-checked. For example, indexing into a scalar field is not allowed. If
* config refers to a local variable, the variable is looked up in {@code localVars}. If {@code
* newVar} is not null, it is registered into {@code localVars}. If {@code
* scalarTypeForCollection} is true, the config must resolve to a collection type, and the type of
* the elements is registered instead.
*
* <pre><code>
* Syntax:
* accessor:
* identifier
* accessor '[' number ']'
* accessor '.' identifier
* </code></pre>
*/
@VisibleForTesting
static OutputView.VariableView accessorNewVariable(Scanner config, MethodContext methodContext, SampleContext sampleContext, ScopeTable localVars, @Nullable String newVar, boolean scalarTypeForCollection) {
OutputView.VariableView.Builder view = OutputView.VariableView.newBuilder();
Preconditions.checkArgument(config.scan() == Scanner.IDENT, "%s:%s: expected identifier: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
String baseIdentifier = config.tokenStr();
TypeModel type = null;
String typeName = null;
if (baseIdentifier.equals(RESPONSE_PLACEHOLDER)) {
view.variable(methodContext.getNamer().getSampleResponseVarName(methodContext, sampleContext.callingForm()));
boolean pageStreaming = methodContext.getMethodConfig().getPageStreaming() != null;
boolean pageStreamingAndUseResourceName = pageStreaming && methodContext.getFeatureConfig().useResourceNameFormatOption(methodContext.getMethodConfig().getPageStreaming().getResourcesFieldConfig());
// Compute the resource name format of output type and store that in typeName
if (pageStreamingAndUseResourceName) {
typeName = methodContext.getNamer().getAndSaveElementResourceTypeName(methodContext.getTypeTable(), methodContext.getMethodConfig().getPageStreaming().getResourcesFieldConfig());
} else if (pageStreaming) {
type = methodContext.getMethodConfig().getPageStreaming().getResourcesFieldConfig().getField().getType().makeOptional();
} else if (methodContext.isLongRunningMethodContext()) {
type = methodContext.getLongRunningConfig().getReturnType();
} else {
type = methodContext.getMethodModel().getOutputType();
}
} else {
// Referencing the value of a local variable
view.variable(methodContext.getNamer().localVarName(Name.from(baseIdentifier)));
type = localVars.getTypeModel(baseIdentifier);
if (type == null) {
typeName = Preconditions.checkNotNull(localVars.getTypeName(baseIdentifier), "%s:%s: variable not defined: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), baseIdentifier);
}
}
int token;
// The accessors include not only the field names but also language-specific
// syntax. e.g., `->field()` in PHP and `.field()` in Java.
ImmutableList.Builder<String> accessors = ImmutableList.builder();
while ((token = config.scan()) != Scanner.EOF) {
if (token == '.') {
// TODO(hzyi): add support for accessing fields of resource name types
Preconditions.checkArgument(type != null, "%s:%s: accessing a field of a resource name is not currently supported", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id());
Preconditions.checkArgument(type.isMessage(), "%s:%s: %s is not a message", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
Preconditions.checkArgument(!type.isRepeated() && !type.isMap(), "%s:%s: %s is not scalar", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
Preconditions.checkArgument(config.scan() == Scanner.IDENT, "%s:%s: expected identifier: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
String fieldName = config.tokenStr();
FieldModel field = Preconditions.checkNotNull(type.getField(fieldName), "%s:%s: type %s does not have field %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), type, fieldName);
type = field.getType();
accessors.add(methodContext.getNamer().getFieldAccessorName(field));
} else if (token == '[') {
Preconditions.checkArgument(type.isRepeated() && !type.isMap(), "%s:%s: %s is not a repeated field", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
Preconditions.checkArgument(config.scan() == Scanner.INT, "%s:%s: expected int in index expression: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
type = type.makeOptional();
int index = Integer.parseInt(config.tokenStr());
accessors.add(methodContext.getNamer().getIndexAccessorName(index));
Preconditions.checkArgument(config.scan() == ']', "%s:%s: expected ']': %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
} else if (token == '{') {
// TODO: honor https://github.com/googleapis/gapic-generator/issues/2600
Preconditions.checkArgument(type.isMap(), "%s:%s: %s is not a map field", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
TypeModel keyType = type.getMapKeyType();
int keyToken = config.scan();
if (keyType.isStringType()) {
Preconditions.checkArgument(keyToken == Scanner.STRING, "%s:%s: expected string type for map key: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
} else if (keyType.isBooleanType()) {
// `true` and `false` are the only valid literals here
Preconditions.checkArgument(keyToken == Scanner.IDENT, "%s:%s: expected boolean type for map key: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
} else {
// Protobuf map keys can only be strings, booleans or integers
Preconditions.checkArgument(keyToken == Scanner.INT, "%s:%s: expected integral type for map key: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
}
keyType.validateValue(config.tokenStr());
accessors.add(methodContext.getNamer().getMapKeyAccessorName(keyType, config.tokenStr()));
type = type.getMapValueType();
Preconditions.checkArgument(config.scan() == '}', "%s:%s: expected '}': %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
} else {
throw new IllegalArgumentException(String.format("%s:%s: unexpected character: %c (%d)", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), token, token));
}
}
if (newVar != null) {
assertIdentifierNotUsed(newVar, methodContext, sampleContext);
if (scalarTypeForCollection) {
Preconditions.checkArgument(type != null, "%s:%s: a resource name can never be a repeated field", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id());
Preconditions.checkArgument(type.isRepeated() && !type.isMap(), "%s:%s: %s is not a repeated field", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), config.input());
// "optional" is how protobuf defines singular fields
type = type.makeOptional();
}
if (type == null && typeName == null) {
throw new IllegalStateException(String.format("%s:%s: type and typeName can't be null at the same time", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id()));
}
typeName = type == null ? typeName : methodContext.getTypeTable().getNicknameFor(type);
if (!localVars.put(newVar, type, typeName)) {
throw new IllegalStateException(String.format("%s:%s: duplicate variable declaration not allowed: %s", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id(), newVar));
}
}
return view.accessors(accessors.build()).type(type).build();
}
Aggregations