software.amazon.smithy.build.model.ProjectionConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smithy-build Show documentation
Show all versions of smithy-build Show documentation
This module is a library used to validate Smithy models, create filtered projections of a model, and generate build artifacts.
/*
* 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