use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class PageStreamingTransformer method generateDescriptorClass.
private PageStreamingDescriptorClassView generateDescriptorClass(MethodContext context) {
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
MethodModel method = context.getMethodModel();
PageStreamingConfig pageStreaming = context.getMethodConfig().getPageStreaming();
PageStreamingDescriptorClassView.Builder desc = PageStreamingDescriptorClassView.newBuilder();
FieldModel resourceField = pageStreaming.getResourcesField();
FieldConfig resourceFieldConfig = pageStreaming.getResourcesFieldConfig();
desc.name(namer.getPageStreamingDescriptorConstName(method));
desc.typeName(namer.getAndSavePagedResponseTypeName(context, resourceFieldConfig));
desc.requestTypeName(method.getAndSaveRequestTypeName(typeTable, namer));
desc.responseTypeName(method.getAndSaveResponseTypeName(typeTable, namer));
desc.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceField));
desc.tokenTypeName(typeTable.getAndSaveNicknameFor(pageStreaming.getResponseTokenField()));
desc.defaultTokenValue(typeTable.getSnippetZeroValueAndSaveNicknameFor(pageStreaming.getResponseTokenField()));
desc.requestTokenSetFunction(namer.getFieldSetFunctionName(pageStreaming.getRequestTokenField()));
if (pageStreaming.hasPageSizeField()) {
desc.requestPageSizeSetFunction(namer.getFieldSetFunctionName(pageStreaming.getPageSizeField()));
desc.requestPageSizeGetFunction(namer.getFieldGetFunctionName(pageStreaming.getPageSizeField()));
}
desc.responseTokenGetFunction(namer.getFieldGetFunctionName(pageStreaming.getResponseTokenField()));
ImmutableList.Builder<String> resourcesFieldGetFunctionList = new ImmutableList.Builder<>();
for (FieldModel field : resourceFieldConfig.getFieldPath()) {
resourcesFieldGetFunctionList.add(namer.getFieldGetFunctionName(field));
}
desc.resourcesFieldGetFunctions(resourcesFieldGetFunctionList.build());
return desc.build();
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method getMethodParamDocs.
private List<ParamDocView> getMethodParamDocs(MethodContext context, Iterable<FieldConfig> fieldConfigs, List<ParamWithSimpleDoc> additionalParamDocs) {
MethodModel method = context.getMethodModel();
List<ParamDocView> allDocs = new ArrayList<>();
if (method.getRequestStreaming()) {
allDocs.addAll(ParamWithSimpleDoc.asParamDocViews(additionalParamDocs));
return allDocs;
}
for (FieldConfig fieldConfig : fieldConfigs) {
FieldModel field = fieldConfig.getField();
SimpleParamDocView.Builder paramDoc = SimpleParamDocView.newBuilder();
paramDoc.paramName(context.getNamer().getVariableName(field));
paramDoc.typeName(context.getTypeTable().getAndSaveNicknameFor(field));
List<String> docLines = null;
MethodConfig methodConfig = context.getMethodConfig();
if (methodConfig.isPageStreaming() && methodConfig.getPageStreaming().hasPageSizeField() && field.equals(methodConfig.getPageStreaming().getPageSizeField())) {
docLines = Arrays.asList(new String[] { "The maximum number of resources contained in the underlying API", "response. The API may return fewer values in a page, even if", "there are additional values to be retrieved." });
} else if (methodConfig.isPageStreaming() && field.equals(methodConfig.getPageStreaming().getRequestTokenField())) {
docLines = Arrays.asList(new String[] { "A page token is used to specify a page of values to be returned.", "If no page token is specified (the default), the first page", "of values will be returned. Any page token used here must have", "been generated by a previous call to the API." });
} else {
docLines = context.getNamer().getDocLines(field);
}
paramDoc.lines(docLines);
allDocs.add(paramDoc.build());
}
allDocs.addAll(ParamWithSimpleDoc.asParamDocViews(additionalParamDocs));
return allDocs;
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class MethodMerger method generateField.
private ConfigNode generateField(ConfigNode prevNode, MethodModel method) {
List<String> parameterList = new ArrayList<>();
for (FieldModel field : method.getInputFields()) {
String fieldName = field.getSimpleName();
if (field.getOneof() == null && !methodTransformer.isIgnoredParameter(fieldName)) {
parameterList.add(fieldName);
}
}
if (parameterList.size() > 0 && parameterList.size() <= FLATTENING_THRESHOLD) {
prevNode = generateFlatteningNode(prevNode, parameterList);
}
FieldConfigNode requiredFieldsNode = new FieldConfigNode(NodeFinder.getNextLine(prevNode), "required_fields");
requiredFieldsNode.setComment(new FixmeComment("Configure which fields are required."));
ConfigNode requiredFieldsValueNode = ListTransformer.generateStringList(parameterList, requiredFieldsNode);
if (requiredFieldsValueNode.isPresent()) {
prevNode.insertNext(requiredFieldsNode);
prevNode = requiredFieldsNode;
}
// use all fields for the following check; if there are ignored fields for flattening
// purposes, the caller still needs a way to set them (by using the request object method).
int fieldCount = Iterables.size(method.getInputFields());
boolean requestObjectMethod = (fieldCount > REQUEST_OBJECT_METHOD_THRESHOLD || fieldCount != parameterList.size()) && !method.getRequestStreaming();
ConfigNode requestObjectMethodNode = FieldConfigNode.createStringPair(NodeFinder.getNextLine(prevNode), "request_object_method", String.valueOf(requestObjectMethod));
prevNode.insertNext(requestObjectMethodNode);
return requestObjectMethodNode;
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class SurfaceNamer method getResourceNameFieldGetFunctionName.
/**
* The function name to get a field that is a resource name class.
*/
public String getResourceNameFieldGetFunctionName(FieldConfig fieldConfig) {
FieldModel type = fieldConfig.getField();
Name identifier = fieldConfig.getField().getNameAsParameterName();
Name resourceName = getResourceTypeNameObject(fieldConfig.getResourceNameConfig());
if (type.isMap()) {
return getNotImplementedString("SurfaceNamer.getResourceNameFieldGetFunctionName:map-type");
} else if (type.isRepeated()) {
return publicMethodName(Name.from("get").join(identifier).join("list_as").join(resourceName).join("list"));
} else {
return publicMethodName(Name.from("get").join(identifier).join("as").join(resourceName));
}
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class TestCaseTransformer method createMockResponseAdditionalSubTrees.
private Iterable<InitCodeNode> createMockResponseAdditionalSubTrees(MethodContext context) {
List<InitCodeNode> additionalSubTrees = new ArrayList<>();
if (context.getMethodConfig().isPageStreaming()) {
// Initialize one resource element if it is page-streaming.
PageStreamingConfig config = context.getMethodConfig().getPageStreaming();
if (config.getResourcesFieldConfig().getFieldPath().size() == 1) {
String resourceFieldName = config.getResourcesFieldName();
additionalSubTrees.add(InitCodeNode.createSingletonList(resourceFieldName));
} else {
// Initialize all the objects between the response type and the resource element.
List<FieldModel> fieldGetters = Lists.reverse(config.getResourcesFieldConfig().getFieldPath());
InitCodeNode initCodeNode = null;
for (FieldModel field : fieldGetters) {
if (config.getResourcesField().isRepeated()) {
initCodeNode = InitCodeNode.createSingletonList(config.getResourcesFieldName());
} else {
initCodeNode = InitCodeNode.create(config.getResourcesFieldName());
}
InitCodeLineType lineType = field.isRepeated() ? ListInitLine : StructureInitLine;
initCodeNode = InitCodeNode.createWithChildren(field.getSimpleName(), lineType, initCodeNode);
}
additionalSubTrees.add(initCodeNode);
}
// Set the initial value of the page token to empty, in order to indicate that no more pages
// are available
String responseTokenName = config.getResponseTokenField().getSimpleName();
additionalSubTrees.add(InitCodeNode.createWithValue(responseTokenName, InitValueConfig.createWithValue(InitValue.createLiteral(""))));
}
if (context.getMethodConfig().isBatching()) {
// Initialize one batching element if it is batching.
BatchingConfig config = context.getMethodConfig().getBatching();
if (config.getSubresponseField() != null) {
String subResponseFieldName = config.getSubresponseField().getSimpleName();
additionalSubTrees.add(InitCodeNode.createSingletonList(subResponseFieldName));
}
}
if (context.getMethodConfig().isGrpcStreaming()) {
GrpcStreamingConfig streamingConfig = context.getMethodConfig().getGrpcStreaming();
if (streamingConfig.hasResourceField()) {
String resourceFieldName = streamingConfig.getResourcesField().getSimpleName();
additionalSubTrees.add(InitCodeNode.createSingletonList(resourceFieldName));
}
}
return additionalSubTrees;
}
Aggregations