Skip to content

Commit

Permalink
Merge pull request #32 from JordonPhillips/fix-namespacing
Browse files Browse the repository at this point in the history
Update namespace handling
  • Loading branch information
JordonPhillips authored May 11, 2020
2 parents 7c17f83 + 7c7bd77 commit fc5a89c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.logging.Logger;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.utils.StringUtils;

/**
Expand Down Expand Up @@ -97,6 +98,20 @@ public static String getDefaultPackageImportName(String packageName) {
return packageName.substring(packageName.lastIndexOf('/') + 1);
}

/**
* Gets the alias to use when referencing the given symbol outside of its namespace.
*
* <p>The default value is the last path component of the symbol's namespace.
*
* @param symbol The symbol whose whose namespace alias should be retrieved.
* @return The alias of the symbol's namespace.
*/
public static String getSymbolNamespaceAlias(Symbol symbol) {
return symbol.getProperty(SymbolUtils.NAMESPACE_ALIAS, String.class)
.filter(StringUtils::isNotBlank)
.orElse(CodegenUtils.getDefaultPackageImportName(symbol.getNamespace()));
}

/**
* Detects if an annotated mediatype indicates JSON contents.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ public enum GoDependency implements SymbolDependencyContainer {
// The version in the stdlib dependencies should reflect the minimum Go version.
// The values aren't currently used, but they could potentially used to dynamically
// set the minimum go version.
BIG("stdlib", "", "math/big", "1.14"),
TIME("stdlib", "", "time", "1.14"),
FMT("stdlib", "", "fmt", "1.14"),
CONTEXT("stdlib", "", "context", "1.14"),
BIG("stdlib", "", "math/big", null, "1.14"),
TIME("stdlib", "", "time", null, "1.14"),
FMT("stdlib", "", "fmt", null, "1.14"),
CONTEXT("stdlib", "", "context", null, "1.14"),

SMITHY("dependency", "github.com/awslabs/smithy-go", "github.com/awslabs/smithy-go", "v0.0.1"),
SMITHY("dependency", "github.com/awslabs/smithy-go", "github.com/awslabs/smithy-go",
"smithy", "v0.0.1"),
SMITHY_HTTP_TRANSPORT("dependency", "github.com/awslabs/smithy-go",
"github.com/awslabs/smithy-go/transport/http", "v0.0.1"),
"github.com/awslabs/smithy-go/transport/http", "smithyhttp", "v0.0.1"),
SMITHY_MIDDLEWARE("dependency", "github.com/awslabs/smithy-go",
"github.com/awslabs/smithy-go/middleware", "v0.0.1");
"github.com/awslabs/smithy-go/middleware", null, "v0.0.1");

public final String sourcePath;
public final String importPath;
public final String alias;
public final String version;
public final SymbolDependency dependency;

GoDependency(String type, String sourcePath, String importPath, String version) {
GoDependency(String type, String sourcePath, String importPath, String alias, String version) {
this.dependency = SymbolDependency.builder()
.dependencyType(type)
.packageName(sourcePath)
Expand All @@ -52,6 +54,7 @@ public enum GoDependency implements SymbolDependencyContainer {
this.sourcePath = sourcePath;
this.importPath = importPath;
this.version = version;
this.alias = alias;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,22 @@
package software.amazon.smithy.go.codegen;

import java.util.function.BiConsumer;

import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolReference;

/**
* Helper for generating stack step middleware.
*/
public final class GoStackStepMiddlewareGenerator {
private static final SymbolReference CONTEXT_TYPE = SymbolReference.builder()
.symbol(SymbolUtils.createValueSymbolBuilder("context.Context")
.addReference(SymbolUtils.createNamespaceReference(GoDependency.CONTEXT))
.build())
.build();
private static final SymbolReference METADATA_TYPE = SymbolReference.builder()
.symbol(SymbolUtils.createValueSymbolBuilder("middleware.Metadata")
.addReference(SymbolUtils.createNamespaceReference(GoDependency.SMITHY_MIDDLEWARE))
.build())
.build();
private static final Symbol CONTEXT_TYPE = SymbolUtils.createValueSymbolBuilder(
"Context", GoDependency.CONTEXT).build();
private static final Symbol METADATA_TYPE = SymbolUtils.createValueSymbolBuilder(
"Metadata", GoDependency.SMITHY_MIDDLEWARE).build();

private final Symbol middlewareSymbol;
private final String handleMethodName;
private final SymbolReference inputType;
private final SymbolReference outputType;
private final SymbolReference handlerType;
private final Symbol inputType;
private final Symbol outputType;
private final Symbol handlerType;

/**
* Creates a new middleware generator with the given builder definition.
Expand All @@ -63,9 +55,9 @@ public GoStackStepMiddlewareGenerator(Builder builder) {
public static GoStackStepMiddlewareGenerator createSerializeStepMiddleware(String identifier) {
return createMiddleware(identifier,
"HandleSerialize",
createSmithyMiddlewarePackageReference("middleware.SerializeInput"),
createSmithyMiddlewarePackageReference("middleware.SerializeOutput"),
createSmithyMiddlewarePackageReference("middleware.SerializeHandler"));
SymbolUtils.createValueSymbolBuilder("SerializeInput", GoDependency.SMITHY_MIDDLEWARE).build(),
SymbolUtils.createValueSymbolBuilder("SerializeOutput", GoDependency.SMITHY_MIDDLEWARE).build(),
SymbolUtils.createValueSymbolBuilder("SerializeHandler", GoDependency.SMITHY_MIDDLEWARE).build());
}

/**
Expand All @@ -77,9 +69,9 @@ public static GoStackStepMiddlewareGenerator createSerializeStepMiddleware(Strin
public static GoStackStepMiddlewareGenerator createDeserializeStepMiddleware(String identifier) {
return createMiddleware(identifier,
"HandleDeserialize",
createSmithyMiddlewarePackageReference("middleware.DeserializeInput"),
createSmithyMiddlewarePackageReference("middleware.DeserializeOutput"),
createSmithyMiddlewarePackageReference("middleware.DeserializeHandler"));
SymbolUtils.createValueSymbolBuilder("DeserializeInput", GoDependency.SMITHY_MIDDLEWARE).build(),
SymbolUtils.createValueSymbolBuilder("DeserializeOutput", GoDependency.SMITHY_MIDDLEWARE).build(),
SymbolUtils.createValueSymbolBuilder("DeserializeHandler", GoDependency.SMITHY_MIDDLEWARE).build());
}

/**
Expand All @@ -95,9 +87,9 @@ public static GoStackStepMiddlewareGenerator createDeserializeStepMiddleware(Str
public static GoStackStepMiddlewareGenerator createMiddleware(
String identifier,
String handlerMethodName,
SymbolReference inputType,
SymbolReference outputType,
SymbolReference handlerType
Symbol inputType,
Symbol outputType,
Symbol handlerType
) {
return builder()
.identifier(identifier)
Expand Down Expand Up @@ -211,7 +203,7 @@ public Symbol getMiddlewareSymbol() {
*
* @return the input type symbol reference.
*/
public SymbolReference getInputType() {
public Symbol getInputType() {
return inputType;
}

Expand All @@ -220,7 +212,7 @@ public SymbolReference getInputType() {
*
* @return the output type symbol reference.
*/
public SymbolReference getOutputType() {
public Symbol getOutputType() {
return outputType;
}

Expand All @@ -229,45 +221,37 @@ public SymbolReference getOutputType() {
*
* @return the handler type symbol reference.
*/
public SymbolReference getHandlerType() {
public Symbol getHandlerType() {
return handlerType;
}

/**
* Get the context type symbol reference.
* Get the context type symbol.
*
* @return the context type symbol reference.
* @return the context type symbol.
*/
public static SymbolReference getContextType() {
public static Symbol getContextType() {
return CONTEXT_TYPE;
}

/**
* Get the middleware metadata type symbol reference.
* Get the middleware metadata type symbol.
*
* @return the middleware metadata type symbol reference.
* @return the middleware metadata type symbol.
*/
public static SymbolReference getMiddlewareMetadataType() {
public static Symbol getMiddlewareMetadataType() {
return METADATA_TYPE;
}

private static SymbolReference createSmithyMiddlewarePackageReference(String typeName) {
return SymbolReference.builder()
.symbol(SymbolUtils.createValueSymbolBuilder(typeName)
.addReference(SymbolUtils.createNamespaceReference(GoDependency.SMITHY_MIDDLEWARE))
.build())
.build();
}

/**
* Builds a {@link GoStackStepMiddlewareGenerator}.
*/
public static class Builder {
private String identifier;
private String handleMethodName;
private SymbolReference inputType;
private SymbolReference outputType;
private SymbolReference handlerType;
private Symbol inputType;
private Symbol outputType;
private Symbol handlerType;

/**
* Builds the middleware generator.
Expand Down Expand Up @@ -306,7 +290,7 @@ public Builder identifier(String identifier) {
* @param inputType the symbol reference to the input type.
* @return the builder.
*/
public Builder inputType(SymbolReference inputType) {
public Builder inputType(Symbol inputType) {
this.inputType = inputType;
return this;
}
Expand All @@ -317,7 +301,7 @@ public Builder inputType(SymbolReference inputType) {
* @param outputType the symbol reference to the output type.
* @return the builder.
*/
public Builder outputType(SymbolReference outputType) {
public Builder outputType(Symbol outputType) {
this.outputType = outputType;
return this;
}
Expand All @@ -328,7 +312,7 @@ public Builder outputType(SymbolReference outputType) {
* @param handlerType the symbol reference to the handler type.
* @return the builder.
*/
public Builder handlerType(SymbolReference handlerType) {
public Builder handlerType(Symbol handlerType) {
this.handlerType = handlerType;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public GoWriter(String fullPackageName) {
public GoWriter addUseImports(SymbolContainer container) {
for (Symbol symbol : container.getSymbols()) {
addImport(symbol,
CodegenUtils.getDefaultPackageImportName(symbol.getNamespace()),
CodegenUtils.getSymbolNamespaceAlias(symbol),
SymbolReference.ContextOption.USE);
}
return this;
Expand All @@ -88,6 +88,17 @@ public GoWriter addUseImports(SymbolReference symbolReference) {
return addImport(symbolReference.getSymbol(), symbolReference.getAlias(), SymbolReference.ContextOption.USE);
}

/**
* Adds and imports the given dependency.
*
* @param goDependency The GoDependency to import.
* @return Returns the writer.
*/
public GoWriter addUseImports(GoDependency goDependency) {
dependencies.addAll(goDependency.getDependencies());
return addImport(goDependency.importPath, goDependency.alias);
}

/**
* Imports a symbol if necessary using a package alias and list of context options.
*
Expand All @@ -110,7 +121,7 @@ public GoWriter addImport(Symbol symbol, String packageAlias, SymbolReference.Co
// Always add dependencies.
dependencies.addAll(symbol.getDependencies());

if (!symbol.getNamespace().isEmpty() && !symbol.getNamespace().equals(fullPackageName)) {
if (isExternalNamespace(symbol.getNamespace())) {
addImport(symbol.getNamespace(), packageAlias);
}

Expand All @@ -121,6 +132,10 @@ public GoWriter addImport(Symbol symbol, String packageAlias, SymbolReference.Co
return this;
}

private boolean isExternalNamespace(String namespace) {
return !StringUtils.isBlank(namespace) && !namespace.equals(fullPackageName);
}

void addImportReferences(Symbol symbol, SymbolReference.ContextOption... options) {
for (SymbolReference reference : symbol.getReferences()) {
for (SymbolReference.ContextOption option : options) {
Expand Down Expand Up @@ -254,6 +269,9 @@ public String apply(Object type, String indent) {
if (type instanceof Symbol) {
Symbol typeSymbol = (Symbol) type;
addUseImports(typeSymbol);
if (isExternalNamespace(typeSymbol.getNamespace())) {
return formatWithNamespace(typeSymbol);
}
return typeSymbol.getName();
} else if (type instanceof SymbolReference) {
SymbolReference typeSymbol = (SymbolReference) type;
Expand All @@ -264,6 +282,13 @@ public String apply(Object type, String indent) {
"Invalid type provided to $T. Expected a Symbol, but found `" + type + "`");
}
}

private String formatWithNamespace(Symbol symbol) {
if (StringUtils.isEmpty(symbol.getNamespace())) {
return symbol.getName();
}
return String.format("%s.%s", CodegenUtils.getSymbolNamespaceAlias(symbol), symbol.getName());
}
}

/**
Expand All @@ -283,10 +308,10 @@ public String apply(Object type, String indent) {
private boolean isPointer(Object type) {
if (type instanceof Symbol) {
Symbol typeSymbol = (Symbol) type;
return typeSymbol.getProperty("pointable", Boolean.class).orElse(false);
return typeSymbol.getProperty(SymbolUtils.POINTABLE, Boolean.class).orElse(false);
} else if (type instanceof SymbolReference) {
SymbolReference typeSymbol = (SymbolReference) type;
return typeSymbol.getProperty("pointable", Boolean.class).orElse(false);
return typeSymbol.getProperty(SymbolUtils.POINTABLE, Boolean.class).orElse(false);
} else {
throw new CodegenException(
"Invalid type provided to $P. Expected a Symbol, but found `" + type + "`");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ private void renderErrorStructure() {
Symbol structureSymbol = symbolProvider.toSymbol(shape);
String interfaceName = structureSymbol.getName() + "Interface";

writer.addUseImports(GoDependency.SMITHY);
writer.addUseImports(GoDependency.FMT);

ErrorTrait errorTrait = shape.expectTrait(ErrorTrait.class);

// Write out the interface for the error
Expand Down
Loading

0 comments on commit fc5a89c

Please sign in to comment.