All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.smithy.build.ProjectionTransformer Maven / Gradle / Ivy

Go to download

This module is a library used to validate Smithy models, create filtered projections of a model, and generate build artifacts.

There is a newer version: 1.54.0
Show newest version
/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.smithy.build;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Function;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.utils.ListUtils;

/**
 * Creates a model transformer by name.
 */
public interface ProjectionTransformer {
    /**
     * Gets the name of the transformer.
     *
     * @return Returns the name (e.g., "traits").
     */
    String getName();

    /**
     * Transforms the given model using the provided {@link TransformContext}.
     *
     * @param context Transformation context.
     * @return Returns the created transformer.
     * @throws IllegalArgumentException if the arguments are invalid.
     */
    Model transform(TransformContext context);

    /**
     * Allows the composition of projections by returning additional
     * projections to run after the current one.
     *
     * @param context Transformation context.
     * @return a collection of named projections to run.
     * @throws IllegalArgumentException if the arguments are invalid.
     */
    default List getAdditionalProjections(TransformContext context) {
        return ListUtils.of();
    }

    /**
     * Creates a {@code ProjectionTransformer} factory function using SPI
     * and the current thread's context class loader.
     *
     * @return Returns the created factory function.
     * @see Thread#getContextClassLoader()
     */
    static Function> createServiceFactory() {
        return createServiceFactory(ServiceLoader.load(ProjectionTransformer.class));
    }

    /**
     * Creates a {@code ProjectionTransformer} factory function using
     * the given transformers.
     *
     * @param transformers Transformers used to build a factory function.
     * @return Returns the created factory.
     */
    static Function> createServiceFactory(
            Iterable transformers
    ) {
        Map map = new HashMap<>();
        for (ProjectionTransformer transformer : transformers) {
            map.put(transformer.getName(), transformer);
        }
        return name -> Optional.ofNullable(map.get(name));
    }

    /**
     * Creates a {@code ProjectionTransformer} factory function using SPI.
     *
     * @param classLoader Class loader to use to discover classes.
     * @return Returns the created factory function.
     */
    static Function> createServiceFactory(ClassLoader classLoader) {
        return createServiceFactory(ServiceLoader.load(ProjectionTransformer.class, classLoader));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy