Permalink
Browse files

Merge pull request #63 from andrewcox/swift-namespace

Updates to generator namespace functionality
  • Loading branch information...
2 parents bca966a + 7b5ea79 commit fa60bd0246dae2f6f4d44b982de6cbd65afb9bf4 @l3fang l3fang committed Feb 22, 2013
@@ -56,7 +56,8 @@ public static void main(final String ... args) throws Exception
.overridePackage(cliConfig.overridePackage)
.defaultPackage(cliConfig.defaultPackage)
.generateIncludedCode(cliConfig.generateIncludedCode)
- .codeFlavor(cliConfig.generateBeans ? "java-regular" : "java-immutable");
+ .codeFlavor(cliConfig.generateBeans ? "java-regular" : "java-immutable")
+ .usePlainJavaNamespace(cliConfig.usePlainJavaNamespace);
for (SwiftGeneratorTweak tweak : cliConfig.tweaks) {
configBuilder.addTweak(tweak);
@@ -59,4 +59,10 @@
description = "Enable specific code generation tweaks"
)
public Set<SwiftGeneratorTweak> tweaks = Sets.newHashSet();
+
+ @Parameter(
+ names = "-use_java_namespace",
+ description = "Use 'java' namespace instead of 'java.swift' namespace"
+ )
+ public boolean usePlainJavaNamespace;
}
@@ -136,22 +136,24 @@ private void parseDocument(final URI thriftUri,
final Document document = context.getDocument();
final Header header = document.getHeader();
+ String effectiveJavaNamespace = "java.swift";
+ if (swiftGeneratorConfig.usePlainJavaNamespace()) {
+ effectiveJavaNamespace = "java";
+ }
+
// Override takes precedence
String javaPackage = swiftGeneratorConfig.getOverridePackage();
// Otherwise fallback on package specified in .thrift file
if (javaPackage == null) {
- javaPackage = header.getNamespace("swift");
- }
- if (javaPackage == null) {
- javaPackage = header.getNamespace("java");
+ javaPackage = header.getNamespace(effectiveJavaNamespace);
}
// Or the default if we don't have an override package or a package in the .thrift file
if (javaPackage == null) {
javaPackage = swiftGeneratorConfig.getDefaultPackage();
}
// If none of the above options get us a package to use, fail
- Preconditions.checkState(javaPackage != null, "thrift uri %s does not declare a swift namespace!", thriftUri);
+ Preconditions.checkState(javaPackage != null, "thrift uri %s does not declare a '%s' namespace!", thriftUri, effectiveJavaNamespace);
// Make a note that this document is a parent of all the documents included, directly or recursively
parentDocuments.push(thriftUri);
@@ -31,14 +31,17 @@
private final Set<SwiftGeneratorTweak> generatorTweaks;
private final boolean generateIncludedCode;
private final String codeFlavor;
-
- private SwiftGeneratorConfig(final URI inputBase,
- final File outputFolder,
- final String overridePackage,
- final String defaultPackage,
- final Set<SwiftGeneratorTweak> generatorTweaks,
- final boolean generateIncludedCode,
- final String codeFlavor)
+ private boolean usePlainJavaNamespace;
+
+ private SwiftGeneratorConfig(
+ final URI inputBase,
+ final File outputFolder,
+ final String overridePackage,
+ final String defaultPackage,
+ final Set<SwiftGeneratorTweak> generatorTweaks,
+ final boolean generateIncludedCode,
+ final String codeFlavor,
+ boolean usePlainJavaNamespace)
{
this.inputBase = inputBase;
this.outputFolder = outputFolder;
@@ -47,6 +50,7 @@ private SwiftGeneratorConfig(final URI inputBase,
this.generatorTweaks = generatorTweaks;
this.generateIncludedCode = generateIncludedCode;
this.codeFlavor = codeFlavor;
+ this.usePlainJavaNamespace = usePlainJavaNamespace;
}
public static Builder builder()
@@ -111,6 +115,14 @@ public String getCodeFlavor()
return codeFlavor;
}
+ /**
+ * Use namespace from 'namespace java ...' directive instead of from 'namespace java.swift ...'
+ */
+ public boolean usePlainJavaNamespace()
+ {
+ return usePlainJavaNamespace;
+ }
+
public static class Builder
{
private URI inputBase = null;
@@ -120,6 +132,7 @@ public String getCodeFlavor()
private Set<SwiftGeneratorTweak> generatorTweaks = EnumSet.noneOf(SwiftGeneratorTweak.class);
private boolean generateIncludedCode = false;
private String codeFlavor = null;
+ private boolean usePlainJavaNamespace = false;
private Builder()
{
@@ -139,7 +152,8 @@ public SwiftGeneratorConfig build()
defaultPackage,
generatorTweaks,
generateIncludedCode,
- codeFlavor);
+ codeFlavor,
+ usePlainJavaNamespace);
}
public Builder inputBase(final URI inputBase)
@@ -183,5 +197,11 @@ public Builder codeFlavor(final String codeFlavor)
this.codeFlavor = codeFlavor;
return this;
}
+
+ public Builder usePlainJavaNamespace(final boolean usePlainJavaNamespace)
+ {
+ this.usePlainJavaNamespace = usePlainJavaNamespace;
+ return this;
+ }
}
}
@@ -135,6 +135,13 @@ public URI apply(final File file) {
private String codeFlavor = "java-regular";
/**
+ * Use the 'java' namespace instead of the 'java.swift' namespace.
+ *
+ * @parameter default-value="false"
+ */
+ private boolean usePlainJavaNamespace = false;
+
+ /**
* @parameter expression="${project}"
* @required
* @readonly
@@ -163,7 +170,8 @@ public final void execute() throws MojoExecutionException, MojoFailureException
.overridePackage(overridePackage)
.defaultPackage(defaultPackage)
.generateIncludedCode(generateIncludedCode)
- .codeFlavor(codeFlavor);
+ .codeFlavor(codeFlavor)
+ .usePlainJavaNamespace(usePlainJavaNamespace);
if (addThriftExceptions) {
configBuilder.addTweak(SwiftGeneratorTweak.ADD_THRIFT_EXCEPTION);

0 comments on commit fa60bd0

Please sign in to comment.