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

software.amazon.smithy.build.model.ProjectionConfig 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.51.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.model;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import software.amazon.smithy.build.SmithyBuildException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;

/**
 * ProjectionConfig stored in a {@link SmithyBuildConfig}.
 */
public final class ProjectionConfig implements ToSmithyBuilder {
    private final boolean isAbstract;
    private final List imports;
    private final List transforms;
    private final Map plugins;

    private ProjectionConfig(Builder builder) {
        this.imports = builder.imports.copy();
        this.transforms = builder.transforms.copy();
        this.isAbstract = builder.isAbstract;
        this.plugins = builder.plugins.copy();

        if (isAbstract && (!plugins.isEmpty() || !imports.isEmpty())) {
            throw new SmithyBuildException("Abstract projections must not define plugins or imports");
        }
    }

    public static Builder builder() {
        return new Builder();
    }

    @Override
    public Builder toBuilder() {
        return builder()
                .imports(imports)
                .plugins(plugins)
                .transforms(transforms)
                .setAbstract(isAbstract);
    }

    public static ProjectionConfig fromNode(Node node) {
        return fromNode(node, SmithyBuildUtils.getBasePathFromSourceLocation(node));
    }

    static ProjectionConfig fromNode(Node node, Path basePath) {
        Builder builder = ProjectionConfig.builder();
        node.expectObjectNode()
                .getBooleanMember("abstract", builder::setAbstract)
                .getArrayMember("imports", s -> SmithyBuildUtils.resolveImportPath(basePath, s),
                                builder::imports)
                .getArrayMember("transforms", TransformConfig::fromNode, builder::transforms)
                .getObjectMember("plugins", plugins -> {
                    for (Map.Entry entry : plugins.getStringMap().entrySet()) {
                        builder.plugins.get().put(entry.getKey(), entry.getValue().expectObjectNode());
                    }
                });
        return builder.build();
    }

    /**
     * @return Gets the immutable transforms in the projection.
     */
    public List getTransforms() {
        return transforms;
    }

    /**
     * @return Gets the immutable plugins of the projection.
     */
    public Map getPlugins() {
        return plugins;
    }

    /**
     * @return Returns true if the projection is abstract.
     */
    public boolean isAbstract() {
        return isAbstract;
    }

    /**
     * Gets the imports configured for the projection.
     *
     * @return Returns the projection-specific imports.
     */
    public List getImports() {
        return imports;
    }

    /**
     * Builds a {@link ProjectionConfig}.
     */
    public static final class Builder implements SmithyBuilder {
        private boolean isAbstract;
        private final BuilderRef> imports = BuilderRef.forList();
        private final BuilderRef> transforms = BuilderRef.forList();
        private final BuilderRef> plugins = BuilderRef.forOrderedMap();

        private Builder() {}

        /**
         * Builds the projection.
         *
         * @return Returns the created projection.
         */
        public ProjectionConfig build() {
            return new ProjectionConfig(this);
        }

        /**
         * Sets the {@code abstract} property of the projection.
         *
         * 

Abstract projections do not directly create any artifacts. * * @param isAbstract Set to true to mark as abstract. * @return Returns the builder. */ public Builder setAbstract(boolean isAbstract) { this.isAbstract = isAbstract; return this; } /** * Replaces the imports of the projection. * * @param imports Imports to set. * @return Returns the builder. */ public Builder imports(Collection imports) { this.imports.clear(); this.imports.get().addAll(imports); return this; } /** * Replaces the transforms of the projection. * * @param transforms Transform to set. * @return Returns the builder. */ public Builder transforms(Collection transforms) { this.transforms.clear(); this.transforms.get().addAll(transforms); return this; } /** * Replaces the plugins of the projection. * * @param plugins Map of plugin name to plugin settings. * @return Returns the builder. */ public Builder plugins(Map plugins) { this.plugins.clear(); this.plugins.get().putAll(plugins); return this; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy