Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new typed properties for trait code generation #2254

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public Builder toBuilder() {
return builder.namespace(namespace, namespaceDelimiter)
.name(name)
.properties(getProperties())
.typedProperties(getTypedProperties())
.definitionFile(definitionFile)
.declarationFile(declarationFile)
.references(references)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ public Builder toBuilder() {
.dependencyType(dependencyType)
.packageName(packageName)
.version(version)
.properties(getProperties());
.properties(getProperties())
.typedProperties(getTypedProperties());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public Builder toBuilder() {
.symbol(symbol)
.options(options)
.properties(getProperties())
.typedProperties(getTypedProperties())
.alias(alias);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package software.amazon.smithy.codegen.core;

import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -45,8 +44,8 @@ public Map<String, Object> getProperties() {
*
* @return Returns a map of additional typed properties.
*/
public Iterator<Property<?>> getTypedProperties() {
return typedProperties.keySet().iterator();
public Map<Property<?>, Object> getTypedProperties() {
return typedProperties;
}

/**
Expand Down Expand Up @@ -128,7 +127,7 @@ public <T> T expectProperty(String name, Class<T> type) {
* @param <T> value type of the property.
* @throws IllegalArgumentException if the property isn't found.
*/
<T> T expectProperty(Property<T> property) {
public <T> T expectProperty(Property<T> property) {
return getProperty(property).orElseThrow(() -> new IllegalArgumentException(String.format(
"Property `%s` expected but not found on %s", property, this)));
}
Expand Down Expand Up @@ -218,5 +217,18 @@ public T properties(Map<String, Object> properties) {
this.properties.get().putAll(properties);
return (T) this;
}

/**
* Replaces all the custom typed properties.
*
* @param properties Custom typed properties to replace with.
* @return Returns the builder.
*/
@SuppressWarnings("unchecked")
public T typedProperties(Map<Property<?>, Object> properties) {
this.typedProperties.clear();
this.typedProperties.get().putAll(properties);
return (T) this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package software.amazon.smithy.traitcodegen;

import software.amazon.smithy.codegen.core.Property;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.utils.SmithyInternalApi;

@SmithyInternalApi
Expand All @@ -14,7 +16,7 @@ public final class SymbolProperties {
*
* <p>This should always be included on collection shapes such as Maps and Lists.
*/
public static final String BUILDER_REF_INITIALIZER = "builder-ref-initializer";
public static final Property<String> BUILDER_REF_INITIALIZER = Property.named("builder-ref-initializer");

/**
* The "base" symbol for a trait.
Expand All @@ -23,7 +25,7 @@ public final class SymbolProperties {
* to if it were not marked with `@trait`. This property is expected on all
* trait symbols.
*/
public static final String BASE_SYMBOL = "base-symbol";
public static final Property<Symbol> BASE_SYMBOL = Property.named("base-symbol");

/**
* The unboxed or primitive version of a Symbol.
Expand All @@ -32,14 +34,14 @@ public final class SymbolProperties {
* have the boxed version {@code Integer} and an unboxed (primitive) version
* {@code integer}.
*/
public static final String UNBOXED_SYMBOL = "unboxed-symbol";
public static final Property<Symbol> UNBOXED_SYMBOL = Property.named("unboxed-symbol");

/**
* Indicates that the given symbol is a primitive type.
*
* <p>This property is checked for existence only and should have no meaningful value.
*/
public static final String IS_PRIMITIVE = "primitive";
public static final Property<Boolean> IS_PRIMITIVE = Property.named("primitive");

private SymbolProperties() {
// No constructor for constants class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
Expand All @@ -37,6 +38,7 @@
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.UniqueItemsTrait;
import software.amazon.smithy.utils.CaseUtils;

/**
Expand Down Expand Up @@ -126,10 +128,16 @@ public Symbol bigDecimalShape(BigDecimalShape shape) {

@Override
public Symbol listShape(ListShape shape) {
Symbol.Builder builder = TraitCodegenUtils.fromClass(List.class).toBuilder()
if (shape.hasTrait(UniqueItemsTrait.class)) {
return TraitCodegenUtils.fromClass(Set.class).toBuilder()
.addReference(toSymbol(shape.getMember()))
.putProperty(SymbolProperties.BUILDER_REF_INITIALIZER, "forOrderedSet()")
.build();
}
return TraitCodegenUtils.fromClass(List.class).toBuilder()
.addReference(toSymbol(shape.getMember()))
.putProperty(SymbolProperties.BUILDER_REF_INITIALIZER, "forList()");
return builder.build();
.putProperty(SymbolProperties.BUILDER_REF_INITIALIZER, "forList()")
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public static String getDefaultTraitName(Shape shape) {
* @return Returns true if the symbol maps to a Java String.
*/
public static boolean isJavaString(Symbol symbol) {
Symbol baseSymbol = symbol.getProperty(SymbolProperties.BASE_SYMBOL, Symbol.class)
.orElse(symbol);
Symbol baseSymbol = symbol.getProperty(SymbolProperties.BASE_SYMBOL).orElse(symbol);
return JAVA_STRING_SYMBOL.getName().equals(baseSymbol.getName())
&& JAVA_STRING_SYMBOL.getNamespace().equals(baseSymbol.getNamespace());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public Void structureShape(StructureShape shape) {

private void writeProperty(MemberShape shape) {
Optional<String> builderRefOptional =
symbolProvider.toSymbol(shape).getProperty(SymbolProperties.BUILDER_REF_INITIALIZER, String.class);
symbolProvider.toSymbol(shape).getProperty(SymbolProperties.BUILDER_REF_INITIALIZER);
if (builderRefOptional.isPresent()) {
writer.write("private final $1T<$2B> $3L = $1T.$4L;", BuilderRef.class,
symbolProvider.toSymbol(shape),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private Symbol getTraitSymbol(TraitCodegenSettings settings, Shape shape, Symbol
// If the base symbol has an unboxed version, use that as the base symbol
// instead of the Boxed version.
if (baseSymbol.getProperty(SymbolProperties.UNBOXED_SYMBOL).isPresent()) {
baseSymbol = baseSymbol.expectProperty(SymbolProperties.UNBOXED_SYMBOL, Symbol.class);
baseSymbol = baseSymbol.expectProperty(SymbolProperties.UNBOXED_SYMBOL);
}

// Maintain all existing properties, but change the namespace and name of the shape
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.traitcodegen.sections;

import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.utils.CodeSection;

/**
* Contains a Java class defining a trait or nested shape.
*/
public final class ClassSectionIsh implements CodeSection {
hpmellema marked this conversation as resolved.
Show resolved Hide resolved
private final Shape shape;

public ClassSectionIsh(Shape shape) {
this.shape = shape;
}

/**
* {@link Shape} that this Java class represents.
*/
public Shape shape() {
return shape;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public String apply(Object type, String indent) {
+ type + "`.");
}
Symbol symbol = (Symbol) type;
Optional<Symbol> baseSymbolOptional = symbol.getProperty(SymbolProperties.BASE_SYMBOL, Symbol.class);
Optional<Symbol> baseSymbolOptional = symbol.getProperty(SymbolProperties.BASE_SYMBOL);
if (baseSymbolOptional.isPresent()) {
return javaTypeFormatter.apply(baseSymbolOptional.get(), indent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
software.amazon.smithy.traitcodegen.integrations.core.CoreIntegration
software.amazon.smithy.traitcodegen.integrations.javadoc.JavaDocIntegration
software.amazon.smithy.traitcodegen.integrations.idref.IdRefDecoratorIntegration
software.amazon.smithy.traitcodegen.integrations.uniqueitems.UniqueItemDecoratorIntegration
Loading