Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Add collection oneof (#689)
Browse files Browse the repository at this point in the history
- Add oneof support
- Move collection config to ApiConfig
- Change archived book template
- FieldConfig to hold the ResourceCollectionConfig
- Rename CollectionConfig to ResourceNameConfig
  • Loading branch information
michaelbausor authored Nov 2, 2016
1 parent 480f152 commit 8b1ef40
Show file tree
Hide file tree
Showing 67 changed files with 1,574 additions and 347 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/google/api/codegen/DocConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
package com.google.api.codegen;

import com.google.api.codegen.config.CollectionConfig;
import com.google.api.codegen.config.MethodConfig;
import com.google.api.codegen.config.SingleResourceNameConfig;
import com.google.api.codegen.metacode.FieldSetting;
import com.google.api.codegen.metacode.InitCode;
import com.google.api.codegen.metacode.InitCodeContext;
Expand Down Expand Up @@ -100,10 +100,10 @@ private static List<InitCodeLine> createInitCodeLines(

ImmutableMap.Builder<String, InitValueConfig> initValueConfigMap = ImmutableMap.builder();
for (Map.Entry<String, String> fieldNamePattern : fieldNamePatterns.entrySet()) {
CollectionConfig collectionConfig =
context.getCollectionConfig(service, fieldNamePattern.getValue());
SingleResourceNameConfig resourceNameConfig =
context.getSingleResourceNameConfig(fieldNamePattern.getValue());
InitValueConfig initValueConfig =
InitValueConfig.create(context.getApiWrapperName(service), collectionConfig);
InitValueConfig.create(context.getApiWrapperName(service), resourceNameConfig);
initValueConfigMap.put(fieldNamePattern.getKey(), initValueConfig);
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/google/api/codegen/GapicContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package com.google.api.codegen;

import com.google.api.codegen.config.ApiConfig;
import com.google.api.codegen.config.CollectionConfig;
import com.google.api.codegen.config.InterfaceConfig;
import com.google.api.codegen.config.MethodConfig;
import com.google.api.codegen.config.ServiceConfig;
import com.google.api.codegen.config.SingleResourceNameConfig;
import com.google.api.tools.framework.aspects.documentation.model.DocumentationUtil;
import com.google.api.tools.framework.model.Field;
import com.google.api.tools.framework.model.Interface;
Expand Down Expand Up @@ -78,9 +78,8 @@ public String getDescription(ProtoElement element) {
}

/** Get collection configuration for a method. */
public CollectionConfig getCollectionConfig(Interface service, String entityName) {
CollectionConfig result =
getApiConfig().getInterfaceConfig(service).getCollectionConfig(entityName);
public SingleResourceNameConfig getSingleResourceNameConfig(String entityName) {
SingleResourceNameConfig result = getApiConfig().getSingleResourceNameConfig(entityName);
if (result == null) {
throw new IllegalStateException(
"A collection config was not present for entity name " + entityName);
Expand Down
123 changes: 117 additions & 6 deletions src/main/java/com/google/api/codegen/config/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package com.google.api.codegen.config;

import com.google.api.codegen.CollectionConfigProto;
import com.google.api.codegen.CollectionOneofProto;
import com.google.api.codegen.ConfigProto;
import com.google.api.codegen.InterfaceConfigProto;
import com.google.api.codegen.LanguageSettingsProto;
Expand All @@ -30,10 +32,12 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import javax.annotation.Nullable;

/** ApiConfig represents the code-gen config for an API library. */
Expand All @@ -57,6 +61,9 @@ public abstract class ApiConfig {
/** Returns the lines from the configured license file. */
public abstract ImmutableList<String> getLicenseLines();

/** Returns a map from entity names to resource name configs. */
public abstract ImmutableMap<String, ResourceNameConfig> getResourceNameConfigs();

/**
* Creates an instance of ApiConfig based on ConfigProto, linking up API interface configurations
* with specified interfaces in interfaceConfigMap. On errors, null will be returned, and
Expand All @@ -67,9 +74,18 @@ public static ApiConfig createApiConfig(Model model, ConfigProto configProto) {
ResourceNameMessageConfigs messageConfigs =
ResourceNameMessageConfigs.createMessageResourceTypesConfig(
model.getDiagCollector(), configProto);

ImmutableMap<String, ResourceNameConfig> resourceNameConfigs =
createResourceNameConfigs(model.getDiagCollector(), configProto);

ImmutableMap<String, InterfaceConfig> interfaceConfigMap =
createInterfaceConfigMap(
model.getDiagCollector(), configProto, messageConfigs, model.getSymbolTable());
model.getDiagCollector(),
configProto,
messageConfigs,
resourceNameConfigs,
model.getSymbolTable());

LanguageSettingsProto settings =
configProto.getLanguageSettings().get(configProto.getLanguage());
if (settings == null) {
Expand Down Expand Up @@ -98,15 +114,15 @@ public static ApiConfig createApiConfig(Model model, ConfigProto configProto) {
settings.getDomainLayerLocation(),
messageConfigs,
copyrightLines,
licenseLines);
licenseLines,
resourceNameConfigs);
}
}

/** Creates an ApiConfig with no content. Exposed for testing. */
@VisibleForTesting
public static ApiConfig createDummyApiConfig() {
return createDummyApiConfig(
ImmutableMap.<String, InterfaceConfig>builder().build(), "", "", null);
return createDummyApiConfig(ImmutableMap.<String, InterfaceConfig>of(), "", "", null);
}

/** Creates an ApiConfig with fixed content. Exposed for testing. */
Expand All @@ -122,13 +138,15 @@ public static ApiConfig createDummyApiConfig(
domainLayerLocation,
messageConfigs,
ImmutableList.<String>of(),
ImmutableList.<String>of());
ImmutableList.<String>of(),
ImmutableMap.<String, ResourceNameConfig>of());
}

private static ImmutableMap<String, InterfaceConfig> createInterfaceConfigMap(
DiagCollector diagCollector,
ConfigProto configProto,
ResourceNameMessageConfigs messageConfigs,
ImmutableMap<String, ResourceNameConfig> resourceNameConfigs,
SymbolTable symbolTable) {
ImmutableMap.Builder<String, InterfaceConfig> interfaceConfigMap =
ImmutableMap.<String, InterfaceConfig>builder();
Expand All @@ -148,7 +166,8 @@ private static ImmutableMap<String, InterfaceConfig> createInterfaceConfigMap(
configProto.getLanguage(),
interfaceConfigProto,
iface,
messageConfigs);
messageConfigs,
resourceNameConfigs);
if (interfaceConfig == null) {
continue;
}
Expand Down Expand Up @@ -199,8 +218,100 @@ private static ImmutableList<String> getResourceLines(String resourceFileName)
return ImmutableList.copyOf(CharStreams.readLines(fileReader));
}

private static ImmutableMap<String, ResourceNameConfig> createResourceNameConfigs(
DiagCollector diagCollector, ConfigProto configProto) {
ImmutableMap<String, SingleResourceNameConfig> singleResourceNameConfigs =
createSingleResourceNameConfigs(diagCollector, configProto);
ImmutableMap<String, ResourceNameOneofConfig> collectionOneofConfigs =
createResourceNameOneofConfigs(
diagCollector, configProto.getCollectionOneofsList(), singleResourceNameConfigs);

ImmutableMap.Builder<String, ResourceNameConfig> resourceCollectionMap = ImmutableMap.builder();
resourceCollectionMap.putAll(singleResourceNameConfigs);
resourceCollectionMap.putAll(collectionOneofConfigs);
return resourceCollectionMap.build();
}

private static ImmutableMap<String, SingleResourceNameConfig> createSingleResourceNameConfigs(
DiagCollector diagCollector, ConfigProto configProto) {
LinkedHashMap<String, SingleResourceNameConfig> singleResourceNameConfigsMap =
new LinkedHashMap<>();
for (CollectionConfigProto collectionConfigProto : configProto.getCollectionsList()) {
createSingleResourceNameConfig(
diagCollector, collectionConfigProto, singleResourceNameConfigsMap);
}
for (InterfaceConfigProto interfaceConfigProto : configProto.getInterfacesList()) {
for (CollectionConfigProto collectionConfigProto :
interfaceConfigProto.getCollectionsList()) {
createSingleResourceNameConfig(
diagCollector, collectionConfigProto, singleResourceNameConfigsMap);
}
}

if (diagCollector.getErrorCount() > 0) {
return null;
} else {
return ImmutableMap.copyOf(singleResourceNameConfigsMap);
}
}

private static void createSingleResourceNameConfig(
DiagCollector diagCollector,
CollectionConfigProto collectionConfigProto,
LinkedHashMap<String, SingleResourceNameConfig> singleResourceNameConfigsMap) {
SingleResourceNameConfig singleResourceNameConfig =
SingleResourceNameConfig.createSingleResourceName(diagCollector, collectionConfigProto);
if (singleResourceNameConfig == null) {
return;
}
if (singleResourceNameConfigsMap.containsKey(singleResourceNameConfig.getEntityName())) {
SingleResourceNameConfig otherConfig =
singleResourceNameConfigsMap.get(singleResourceNameConfig.getEntityName());
if (!singleResourceNameConfig.getNamePattern().equals(otherConfig.getNamePattern())) {
diagCollector.addDiag(
Diag.error(
SimpleLocation.TOPLEVEL,
"Inconsistent collection configs across interfaces. Entity name: "
+ singleResourceNameConfig.getEntityName()));
}
} else {
singleResourceNameConfigsMap.put(
singleResourceNameConfig.getEntityName(), singleResourceNameConfig);
}
}

private static ImmutableMap<String, ResourceNameOneofConfig> createResourceNameOneofConfigs(
DiagCollector diagCollector,
Iterable<CollectionOneofProto> oneofConfigProtos,
ImmutableMap<String, SingleResourceNameConfig> singleResourceNameConfigs) {
ImmutableMap.Builder<String, ResourceNameOneofConfig> oneofConfigBuilder =
ImmutableMap.builder();
for (CollectionOneofProto oneofProto : oneofConfigProtos) {
ResourceNameOneofConfig oneofConfig =
ResourceNameOneofConfig.createResourceNameOneof(
diagCollector, oneofProto, singleResourceNameConfigs);
if (oneofConfig == null) {
continue;
}
oneofConfigBuilder.put(oneofConfig.getEntityName(), oneofConfig);
}
return oneofConfigBuilder.build();
}

/** Returns the InterfaceConfig for the given API interface. */
public InterfaceConfig getInterfaceConfig(Interface iface) {
return getInterfaceConfigMap().get(iface.getFullName());
}

public Iterable<SingleResourceNameConfig> getSingleResourceNameConfigs() {
return Iterables.filter(getResourceNameConfigs().values(), SingleResourceNameConfig.class);
}

public SingleResourceNameConfig getSingleResourceNameConfig(String entityName) {
ResourceNameConfig resourceNameConfig = getResourceNameConfigs().get(entityName);
if (resourceNameConfig != null && resourceNameConfig instanceof SingleResourceNameConfig) {
return (SingleResourceNameConfig) resourceNameConfig;
}
return null;
}
}
46 changes: 36 additions & 10 deletions src/main/java/com/google/api/codegen/config/FieldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,31 @@ public abstract class FieldConfig {
public abstract ResourceNameTreatment getResourceNameTreatment();

@Nullable
public abstract String getEntityName();
public abstract ResourceNameConfig getResourceNameConfig();

public String getEntityName() {
if (getResourceNameConfig() == null) {
return null;
}
return getResourceNameConfig().getEntityName();
}

public ResourceNameType getResourceNameType() {
if (getResourceNameConfig() == null) {
return null;
}
return getResourceNameConfig().getResourceNameType();
}

public static FieldConfig createFieldConfig(
Field field, ResourceNameTreatment resourceNameTreatment, String entityName) {
if (resourceNameTreatment != ResourceNameTreatment.NONE && entityName == null) {
Field field,
ResourceNameTreatment resourceNameTreatment,
ResourceNameConfig resourceNameConfig) {
if (resourceNameTreatment != ResourceNameTreatment.NONE && resourceNameConfig == null) {
throw new IllegalArgumentException(
"resourceName may only be null if resourceNameTreatment is NONE");
}
return new AutoValue_FieldConfig(field, resourceNameTreatment, entityName);
return new AutoValue_FieldConfig(field, resourceNameTreatment, resourceNameConfig);
}

/** Creates a FieldConfig for the given Field with ResourceNameTreatment set to None. */
Expand All @@ -58,6 +74,7 @@ public static FieldConfig createDefaultFieldConfig(Field field) {
public static FieldConfig createFlattenedFieldConfig(
DiagCollector diagCollector,
ResourceNameMessageConfigs messageConfigs,
ImmutableMap<String, ResourceNameConfig> resourceNameConfigs,
MethodConfigProto methodConfigProto,
Method method,
FlatteningGroupProto flatteningGroup,
Expand Down Expand Up @@ -94,13 +111,22 @@ public static FieldConfig createFlattenedFieldConfig(
}
}

if (treatment == ResourceNameTreatment.NONE) {
entityName = null;
ResourceNameConfig resourceNameConfig = null;
if (entityName != null) {
resourceNameConfig = resourceNameConfigs.get(entityName);
if (resourceNameConfig == null) {
diagCollector.addDiag(
Diag.error(
SimpleLocation.TOPLEVEL,
"No resourceNameConfig with entity_name \"%s\"",
entityName));
return null;
}
}

validate(messageConfigs, parameterField, treatment, entityName);
validate(messageConfigs, parameterField, treatment, resourceNameConfig);

return createFieldConfig(parameterField, treatment, entityName);
return createFieldConfig(parameterField, treatment, resourceNameConfig);
}

public static String getEntityName(
Expand Down Expand Up @@ -148,7 +174,7 @@ public static void validate(
ResourceNameMessageConfigs messageConfigs,
Field field,
ResourceNameTreatment treatment,
String entityName) {
ResourceNameConfig resourceNameConfig) {
switch (treatment) {
case NONE:
break;
Expand All @@ -161,7 +187,7 @@ public static void validate(
}
break;
case VALIDATE:
if (entityName == null) {
if (resourceNameConfig == null) {
throw new IllegalArgumentException(
"Field must have a resource type or field name pattern specified to support "
+ "VALIDATE resource name treatment. Field: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class FlatteningConfig {
public static FlatteningConfig createFlattening(
DiagCollector diagCollector,
ResourceNameMessageConfigs messageConfigs,
ImmutableMap<String, ResourceNameConfig> resourceNameConfigs,
MethodConfigProto methodConfigProto,
FlatteningGroupProto flatteningGroup,
Method method) {
Expand All @@ -47,7 +48,13 @@ public static FlatteningConfig createFlattening(
for (String parameter : flatteningGroup.getParametersList()) {
FieldConfig fieldConfig =
FieldConfig.createFlattenedFieldConfig(
diagCollector, messageConfigs, methodConfigProto, method, flatteningGroup, parameter);
diagCollector,
messageConfigs,
resourceNameConfigs,
methodConfigProto,
method,
flatteningGroup,
parameter);
if (fieldConfig == null) {
missing = true;
} else {
Expand Down
Loading

0 comments on commit 8b1ef40

Please sign in to comment.