use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class InitCodeTransformer method generateSimpleInitCodeLine.
private InitCodeLineView generateSimpleInitCodeLine(MethodContext context, InitCodeNode item, boolean isFirstItem) {
SimpleInitCodeLineView.Builder surfaceLine = SimpleInitCodeLineView.newBuilder();
FieldConfig fieldConfig = item.getFieldConfig();
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
surfaceLine.lineType(InitCodeLineType.SimpleInitLine);
if (context.getFeatureConfig().useResourceNameFormatOptionInSample(fieldConfig)) {
if (!context.isFlattenedMethodContext()) {
// In a non-flattened context, we always use the resource name type set on the message
// instead of set on the flattened method
fieldConfig = fieldConfig.getMessageFieldConfig();
}
if (item.getType().isRepeated()) {
surfaceLine.typeName(namer.getAndSaveResourceTypeName(typeTable, fieldConfig));
} else {
surfaceLine.typeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig));
}
} else {
surfaceLine.typeName(typeTable.getAndSaveNicknameFor(item.getType()));
}
surfaceLine.identifier(getVariableName(context, item));
setInitValueAndComments(surfaceLine, context, item, isFirstItem);
return surfaceLine.build();
}
use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class InitCodeTransformer method generateListInitCodeLine.
private InitCodeLineView generateListInitCodeLine(MethodContext context, InitCodeNode item) {
ListInitCodeLineView.Builder surfaceLine = ListInitCodeLineView.newBuilder();
FieldConfig fieldConfig = item.getFieldConfig();
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
surfaceLine.lineType(InitCodeLineType.ListInitLine);
surfaceLine.identifier(namer.localVarName(item.getIdentifier()));
if (context.getFeatureConfig().useResourceNameFormatOptionInSample(fieldConfig)) {
surfaceLine.elementTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig));
} else {
surfaceLine.elementTypeName(typeTable.getAndSaveNicknameForElementType(item.getType().makeOptional()));
}
List<String> entries = new ArrayList<>();
List<InitCodeLineView> elements = new ArrayList<>();
for (InitCodeNode child : item.getChildren().values()) {
entries.add(namer.localVarName(child.getIdentifier()));
elements.add(generateSurfaceInitCodeLine(context, child, elements.isEmpty()));
}
surfaceLine.elementIdentifiers(entries);
surfaceLine.elements(elements);
return surfaceLine.build();
}
use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class InitCodeTransformer method setInitValueAndComments.
private void setInitValueAndComments(SimpleInitCodeLineView.Builder surfaceLine, MethodContext context, InitCodeNode item, boolean isFirstItem) {
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
InitValueConfig initValueConfig = item.getInitValueConfig();
FieldConfig fieldConfig = item.getFieldConfig();
// Output variables
InitValueView initValue;
String comment = "";
if (context.getFeatureConfig().useResourceNameFormatOptionInSample(fieldConfig)) {
if (!context.isFlattenedMethodContext()) {
ResourceNameConfig messageResNameConfig = fieldConfig.getMessageResourceNameConfig();
if (messageResNameConfig == null || messageResNameConfig.getResourceNameType() != ResourceNameType.ANY) {
// In a non-flattened context, we always use the resource name type set on the message
// instead of set on the flattened method, unless the resource name type on message
// is ANY.
fieldConfig = fieldConfig.getMessageFieldConfig();
}
}
if (item.getType().isRepeated()) {
initValue = RepeatedResourceNameInitValueView.newBuilder().resourceTypeName(namer.getAndSaveElementResourceTypeName(context.getTypeTable(), fieldConfig)).build();
} else {
SingleResourceNameConfig singleResourceNameConfig;
switch(fieldConfig.getResourceNameType()) {
case ANY:
// TODO(michaelbausor): handle case where there are no other resource names at all...
singleResourceNameConfig = Iterables.get(context.getProductConfig().getSingleResourceNameConfigs(), 0);
FieldConfig anyResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
initValue = createResourceNameInitValueView(context, anyResourceNameFieldConfig, item).build();
break;
case FIXED:
throw new UnsupportedOperationException("entity name invalid");
case ONEOF:
ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
singleResourceNameConfig = Iterables.get(oneofConfig.getSingleResourceNameConfigs(), 0);
FieldConfig singleResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
ResourceNameInitValueView initView = createResourceNameInitValueView(context, singleResourceNameFieldConfig, item).build();
initValue = ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).specificResourceNameView(initView).build();
break;
case SINGLE:
initValue = createResourceNameInitValueView(context, fieldConfig, item).build();
break;
case NONE:
// Fall-through
default:
throw new UnsupportedOperationException("unexpected entity name type '" + fieldConfig.getResourceNameType() + "'");
}
}
} else if (initValueConfig.hasFormattingConfig() && !item.getType().isRepeated()) {
if (context.getFeatureConfig().enableStringFormatFunctions() || fieldConfig.getResourceNameConfig() == null) {
FormattedInitValueView.Builder formattedInitValue = FormattedInitValueView.newBuilder();
formattedInitValue.apiWrapperName(context.getNamer().getApiWrapperClassName(context.getInterfaceConfig()));
formattedInitValue.fullyQualifiedApiWrapperName(context.getNamer().getFullyQualifiedApiWrapperClassName(context.getInterfaceConfig()));
formattedInitValue.formatFunctionName(context.getNamer().getFormatFunctionName(context.getInterfaceConfig(), initValueConfig.getSingleResourceNameConfig()));
PathTemplate template = initValueConfig.getSingleResourceNameConfig().getNameTemplate();
String[] encodeArgs = new String[template.vars().size()];
Arrays.fill(encodeArgs, FORMAT_SPEC_PLACEHOLDER);
// Format spec usually contains reserved character, escaped by path template.
// So we first encode using FORMAT_SPEC_PLACEHOLDER, then do straight string replace.
formattedInitValue.formatSpec(template.withoutVars().encode(encodeArgs).replace(FORMAT_SPEC_PLACEHOLDER, context.getNamer().formatSpec()));
List<String> varList = Lists.newArrayList(initValueConfig.getSingleResourceNameConfig().getNameTemplate().vars());
formattedInitValue.formatArgs(getFormatFunctionArgs(context, varList, initValueConfig));
initValue = formattedInitValue.build();
} else {
initValue = createResourceNameInitValueView(context, fieldConfig, item).convertToString(true).build();
}
} else {
SimpleInitValueView.Builder simpleInitValue = SimpleInitValueView.newBuilder();
if (initValueConfig.hasSimpleInitialValue()) {
String value = initValueConfig.getInitialValue().getValue();
switch(initValueConfig.getInitialValue().getType()) {
case Literal:
if (item.getType().isEnum()) {
value = context.getTypeTable().getEnumValue(item.getType(), value);
} else {
value = context.getTypeTable().renderPrimitiveValue(item.getType(), value);
}
break;
case Random:
value = context.getNamer().injectRandomStringGeneratorCode(value);
break;
case Variable:
value = context.getNamer().localVarReference(Name.anyLower(value));
break;
default:
throw new IllegalArgumentException("Unhandled init value type");
}
simpleInitValue.initialValue(value);
} else {
simpleInitValue.initialValue(context.getTypeTable().getSnippetZeroValueAndSaveNicknameFor(item.getType()));
simpleInitValue.isRepeated(item.getType().isRepeated());
if (isRequired(item.getFieldConfig(), context)) {
String name = getVariableName(context, item);
comment = String.format(UNINITIALIZED_REQUIRED_FIELD_COMMENT, name);
}
}
initValue = simpleInitValue.build();
}
surfaceLine.initValue(initValue);
surfaceLine.needsLeadingNewline(!isFirstItem);
if (generateUserFacingComments) {
surfaceLine.doc(context.getNamer().getDocLines(comment));
} else {
surfaceLine.doc(ImmutableList.of());
}
}
use of com.google.api.codegen.config.FieldConfig 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.FieldConfig 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;
}
Aggregations