use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class JavaDiscoGapicSchemaToViewTransformer method transform.
@Override
public List<ViewModel> transform(DiscoApiModel model, GapicProductConfig productConfig) {
List<ViewModel> surfaceSchemas = new ArrayList<>();
String packageName = productConfig.getPackageName();
JavaSurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
DiscoGapicInterfaceContext context = DiscoGapicInterfaceContext.createWithoutInterface(model, productConfig, createTypeTable(productConfig.getPackageName(), surfaceNamer), surfaceNamer, JavaFeatureConfig.newBuilder().enableStringFormatFunctions(true).build());
for (Schema schema : context.getDocument().schemas().values()) {
Map<SchemaTransformationContext, StaticLangApiMessageView> contextViews = new TreeMap<>(SchemaTransformationContext.comparator);
generateSchemaClasses(contextViews, context, schema);
for (Map.Entry<SchemaTransformationContext, StaticLangApiMessageView> contextView : contextViews.entrySet()) {
surfaceSchemas.add(generateSchemaFile(contextView.getKey(), contextView.getValue()));
}
}
Collections.sort(surfaceSchemas, new Comparator<ViewModel>() {
@Override
public int compare(ViewModel o1, ViewModel o2) {
return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
}
});
return surfaceSchemas;
}
use of com.google.api.codegen.discovery.Schema 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.discovery.Schema in project toolkit by googleapis.
the class SchemaTypeTable method getSnippetZeroValueAndSaveNicknameFor.
@Override
public String getSnippetZeroValueAndSaveNicknameFor(TypeModel type) {
Schema schema = null;
if (!type.isEmptyType()) {
schema = ((DiscoveryField) type).getDiscoveryField();
}
TypedValue typedValue = typeNameConverter.getSnippetZeroValue(schema);
return typedValue.getValueAndSaveTypeNicknameIn(typeTable);
}
use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class ResourceNameMessageConfigs method createMessageResourceTypesConfig.
static ResourceNameMessageConfigs createMessageResourceTypesConfig(DiscoApiModel model, ConfigProto configProto, String defaultPackage, SurfaceNamer languageNamer) {
ImmutableMap.Builder<String, ResourceNameMessageConfig> builder = ImmutableMap.builder();
for (ResourceNameMessageConfigProto messageResourceTypesProto : configProto.getResourceNameGenerationList()) {
ResourceNameMessageConfig messageResourceTypeConfig = ResourceNameMessageConfig.createResourceNameMessageConfig(model.getDiagCollector(), messageResourceTypesProto, defaultPackage);
builder.put(messageResourceTypeConfig.messageName(), messageResourceTypeConfig);
}
ImmutableMap<String, ResourceNameMessageConfig> messageResourceTypeConfigMap = builder.build();
ListMultimap<String, FieldModel> fieldsByMessage = ArrayListMultimap.create();
DiscoGapicNamer discoGapicNamer = new DiscoGapicNamer();
for (Method method : model.getDocument().methods()) {
String fullName = discoGapicNamer.getRequestTypeName(method, languageNamer).getFullName();
ResourceNameMessageConfig messageConfig = messageResourceTypeConfigMap.get(fullName);
if (messageConfig == null) {
continue;
}
for (Schema property : method.parameters().values()) {
if (messageConfig.getEntityNameForField(property.getIdentifier()) != null) {
fieldsByMessage.put(fullName, DiscoveryField.create(property, model));
}
}
}
return new AutoValue_ResourceNameMessageConfigs(messageResourceTypeConfigMap, fieldsByMessage);
}
use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class DiscoveryField method getField.
@Override
public DiscoveryField getField(String key) {
for (DiscoveryField field : getFields()) {
if (field.getNameAsParameter().equals(key)) {
return field;
}
}
Schema parentTypeSchema = getDiscoveryField();
List<Schema> pathToKeySchema = parentTypeSchema.findChild(key);
return DiscoveryField.create(pathToKeySchema.get(pathToKeySchema.size() - 1), apiModel);
}
Aggregations