Search in sources :

Example 1 with JavaSurfaceNamer

use of com.google.api.codegen.transformer.java.JavaSurfaceNamer in project toolkit by googleapis.

the class JavaDiscoGapicResourceNameToViewTransformer method transform.

@Override
public List<ViewModel> transform(DiscoApiModel apiModel, GapicProductConfig productConfig) {
    List<ViewModel> surfaceRequests = new ArrayList<>();
    String packageName = productConfig.getPackageName();
    SurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
    DiscoGapicInterfaceContext context = DiscoGapicInterfaceContext.createWithoutInterface(apiModel, productConfig, createTypeTable(productConfig.getPackageName(), surfaceNamer), surfaceNamer, JavaFeatureConfig.newBuilder().enableStringFormatFunctions(true).build());
    // Keep track of which name patterns have been generated to avoid duplicate classes.
    Set<String> namePatterns = new HashSet<>();
    for (String interfaceName : productConfig.getInterfaceConfigMap().keySet()) {
        SchemaTransformationContext requestContext = SchemaTransformationContext.create(interfaceName, context.getSchemaTypeTable(), context);
        // Maps a canonical resource name pattern to any method that uses that pattern.
        Map<String, Method> namePatternsToMethod = new HashMap<>();
        for (MethodConfig methodConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getMethodConfigs()) {
            Method method = ((DiscoveryMethodModel) methodConfig.getMethodModel()).getDiscoMethod();
            namePatternsToMethod.put(DiscoGapicParser.getCanonicalPath(method), method);
        }
        for (SingleResourceNameConfig nameConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getSingleResourceNameConfigs()) {
            String namePattern = nameConfig.getNamePattern();
            if (namePatterns.contains(namePattern)) {
                continue;
            }
            Method method = namePatternsToMethod.get(namePattern);
            StaticLangApiResourceNameView resourceNameView = generateResourceNameClass(requestContext, method, nameConfig);
            surfaceRequests.add(generateResourceNameFile(requestContext, resourceNameView));
            namePatterns.add(nameConfig.getNamePattern());
        }
    }
    Collections.sort(surfaceRequests, new Comparator<ViewModel>() {

        @Override
        public int compare(ViewModel o1, ViewModel o2) {
            return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
        }
    });
    return surfaceRequests;
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) StaticLangApiResourceNameView(com.google.api.codegen.viewmodel.StaticLangApiResourceNameView) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) MethodConfig(com.google.api.codegen.config.MethodConfig) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext) HashSet(java.util.HashSet)

Example 2 with JavaSurfaceNamer

use of com.google.api.codegen.transformer.java.JavaSurfaceNamer 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;
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) Schema(com.google.api.codegen.discovery.Schema) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) TreeMap(java.util.TreeMap) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) Map(java.util.Map) TreeMap(java.util.TreeMap) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext)

Example 3 with JavaSurfaceNamer

use of com.google.api.codegen.transformer.java.JavaSurfaceNamer in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method createTypeTable.

private SchemaTypeTable createTypeTable(String implicitPackageName) {
    JavaTypeTable typeTable = new JavaTypeTable(implicitPackageName, IGNORE_JAVA_LANG_CLASH);
    SchemaTypeNameConverter typeNameConverter = new JavaSchemaTypeNameConverter(implicitPackageName, nameFormatter);
    return new SchemaTypeTable(typeTable, typeNameConverter, new JavaSurfaceNamer(implicitPackageName, implicitPackageName));
}
Also used : SchemaTypeNameConverter(com.google.api.codegen.transformer.SchemaTypeNameConverter) JavaSchemaTypeNameConverter(com.google.api.codegen.transformer.java.JavaSchemaTypeNameConverter) JavaSchemaTypeNameConverter(com.google.api.codegen.transformer.java.JavaSchemaTypeNameConverter) JavaTypeTable(com.google.api.codegen.util.java.JavaTypeTable) SchemaTypeTable(com.google.api.codegen.transformer.SchemaTypeTable) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer)

Example 4 with JavaSurfaceNamer

use of com.google.api.codegen.transformer.java.JavaSurfaceNamer in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method transform.

@Override
public List<ViewModel> transform(DiscoApiModel model, GapicProductConfig productConfig) {
    List<ViewModel> surfaceRequests = new ArrayList<>();
    String packageName = productConfig.getPackageName();
    SurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
    for (InterfaceModel apiInterface : model.getInterfaces()) {
        boolean enableStringFormatFunctions = productConfig.getResourceNameMessageConfigs().isEmpty();
        DiscoGapicInterfaceContext context = JavaDiscoGapicSurfaceTransformer.newInterfaceContext(apiInterface, productConfig, surfaceNamer, createTypeTable(productConfig.getPackageName()), enableStringFormatFunctions);
        for (MethodModel method : context.getSupportedMethods()) {
            RequestObjectParamView params = getRequestObjectParams(context, method);
            SchemaTransformationContext requestContext = SchemaTransformationContext.create(method.getFullName(), context.getSchemaTypeTable(), context);
            StaticLangApiMessageView requestView = generateRequestClass(requestContext, method, params);
            surfaceRequests.add(generateRequestFile(requestContext, requestView));
        }
    }
    Collections.sort(surfaceRequests, new Comparator<ViewModel>() {

        @Override
        public int compare(ViewModel o1, ViewModel o2) {
            return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
        }
    });
    return surfaceRequests;
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) MethodModel(com.google.api.codegen.config.MethodModel) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) InterfaceModel(com.google.api.codegen.config.InterfaceModel) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext)

Example 5 with JavaSurfaceNamer

use of com.google.api.codegen.transformer.java.JavaSurfaceNamer in project toolkit by googleapis.

the class DiscoGapicGeneratorApi method getProviders.

/**
 * From config file paths, constructs the DiscoGapicProviders to run.
 */
@VisibleForTesting
static List<GapicProvider> getProviders(String discoveryDocPath, List<String> configFileNames, String packageConfigFile, List<String> enabledArtifacts) throws IOException {
    if (!new File(discoveryDocPath).exists()) {
        throw new IOException("File not found: " + discoveryDocPath);
    }
    Reader reader = new InputStreamReader(new FileInputStream(new File(discoveryDocPath)));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(reader);
    // Read the YAML config and convert it to proto.
    if (configFileNames.size() == 0) {
        throw new IOException(String.format("--%s must be provided", GENERATOR_CONFIG_FILES.name()));
    }
    ConfigProto configProto = loadConfigFromFiles(configFileNames);
    if (configProto == null) {
        throw new IOException("Failed to load config proto.");
    }
    PackageMetadataConfig packageConfig = null;
    if (!Strings.isNullOrEmpty(packageConfigFile)) {
        String contents = new String(Files.readAllBytes(Paths.get(packageConfigFile)), StandardCharsets.UTF_8);
        packageConfig = PackageMetadataConfig.createFromString(contents);
    }
    GeneratorProto generator = configProto.getGenerator();
    String language = configProto.getLanguage();
    String defaultPackageName = configProto.getLanguageSettingsMap().get(language).getPackageName();
    SurfaceNamer surfaceNamer = null;
    if (language.equals(JAVA)) {
        surfaceNamer = new JavaSurfaceNamer(defaultPackageName, defaultPackageName, new JavaNameFormatter());
    }
    if (surfaceNamer == null) {
        throw new UnsupportedOperationException("DiscoGapicGeneratorApi: language \"" + language + "\" not yet supported");
    }
    DiscoApiModel model = new DiscoApiModel(Document.from(new DiscoveryNode(root)), defaultPackageName);
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, surfaceNamer);
    String factory = generator.getFactory();
    String id = generator.getId();
    DiscoGapicProviderFactory providerFactory = createProviderFactory(factory);
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(id).enabledArtifacts(enabledArtifacts).build();
    return providerFactory.create(model, productConfig, generatorConfig, packageConfig);
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) ConfigYamlReader(com.google.api.codegen.configgen.ConfigYamlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) FileInputStream(java.io.FileInputStream) JavaNameFormatter(com.google.api.codegen.util.java.JavaNameFormatter) DiscoGapicProviderFactory(com.google.api.codegen.discogapic.DiscoGapicProviderFactory) DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)5 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)3 DiscoGapicInterfaceContext (com.google.api.codegen.transformer.DiscoGapicInterfaceContext)3 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)3 ViewModel (com.google.api.codegen.viewmodel.ViewModel)3 ArrayList (java.util.ArrayList)3 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 MethodModel (com.google.api.codegen.config.MethodModel)1 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)1 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)1 ConfigYamlReader (com.google.api.codegen.configgen.ConfigYamlReader)1 DiscoGapicProviderFactory (com.google.api.codegen.discogapic.DiscoGapicProviderFactory)1 DiscoveryNode (com.google.api.codegen.discovery.DiscoveryNode)1