diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-06-20 10:32:54 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-06-20 10:32:54 +0100 |
| commit | 60a89e424f6ad477ecc89760407d6999c5ee8d48 (patch) | |
| tree | 17354ea6155f156d9609b9a4d0bcd95aeaf88f57 | |
| parent | 60389e043ecfcbbb8592ccee639ec282657c1a72 (diff) | |
Handle subnamespaces when mapping namespaces to Java packages
3 files changed, 43 insertions, 8 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/java/JavaGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/java/JavaGenerator.java index 617d438..e68acaa 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/java/JavaGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/java/JavaGenerator.java @@ -34,14 +34,22 @@ public class JavaGenerator { } public List<String> namespaceToJavaPackageParts(NamespaceName namespaceName) { - // TODO: handle subpackages - return this.config.namespaceConfig(namespaceName) - .map(namespaceConfig -> namespaceConfig.packageName()) - .orElseGet(() -> { - var packageParts = new ArrayList<>(this.config.packageName()); - packageParts.addAll(namespaceName.parts()); + var ancestorNamespaceName = namespaceName; + var remainingNamespaceNameParts = new ArrayList<String>(); + while (!ancestorNamespaceName.isRoot()) { + var namespaceConfig = this.config.namespaceConfig(ancestorNamespaceName); + if (namespaceConfig.isPresent()) { + var packageParts = new ArrayList<>(namespaceConfig.get().packageName()); + packageParts.addAll(remainingNamespaceNameParts.reversed()); return packageParts; - }); + } + remainingNamespaceNameParts.add(ancestorNamespaceName.parts().getLast()); + ancestorNamespaceName = ancestorNamespaceName.parent(); + } + + var packageParts = new ArrayList<>(this.config.packageName()); + packageParts.addAll(namespaceName.parts()); + return packageParts; } public JavaTypeRef generateTypeRef(Type type) { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/NamespaceName.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/NamespaceName.java index e0fb2d3..2cb3497 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/types/NamespaceName.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/NamespaceName.java @@ -8,6 +8,14 @@ public record NamespaceName(List<String> parts) { return new NamespaceName(Arrays.asList(parts)); } + public NamespaceName parent() { + return new NamespaceName(parts.subList(0, parts.size() - 1)); + } + + public boolean isRoot() { + return parts.isEmpty(); + } + @Override public String toString() { return String.join("/", parts); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaGeneratorTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaGeneratorTests.java index 9cbd5d9..d7c3eeb 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaGeneratorTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaGeneratorTests.java @@ -28,7 +28,7 @@ public class JavaGeneratorTests { } @Test - public void explicitJavaPackageIsUsedForNamespaceWhenAvailable() { + public void explicitJavaPackageIsUsedForNamespaceWhenExactNamespaceHasMapping() { var config = new JavaGeneratorConfig( List.of("com", "example"), Map.ofEntries( @@ -45,4 +45,23 @@ public class JavaGeneratorTests { assertThat(result, equalTo(List.of("com", "other"))); } + + @Test + public void explicitJavaPackageIsUsedForNamespaceWhenAncestorNamespaceHasMapping() { + var config = new JavaGeneratorConfig( + List.of("com", "example"), + Map.ofEntries( + Map.entry( + NamespaceName.of("a", "b"), + new JavaNamespaceConfig(List.of("com", "other")) + ) + ), + Map.of() + ); + var generator = new JavaGenerator(config); + + var result = generator.namespaceToJavaPackageParts(NamespaceName.of("a", "b", "c", "d")); + + assertThat(result, equalTo(List.of("com", "other", "c", "d"))); + } } |
