Rework inlined union generation
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Jul 2018 19:32:04 +0000 (21:32 +0200)
committerAnil Belur <abelur@linuxfoundation.org>
Wed, 19 Jun 2024 00:41:24 +0000 (10:41 +1000)
Unions internal to a leaf union end up being incompletely generated,
as they lack stringValue(), hashCode(), equals() and do not correctly
bind to its enclosing builder -- leading to a generated code not being
compilable.

There are multiple issues here, all of which are addressed in this patch:
- hashCode/equals properties are created in the wrong place
- property return type is set to the builder, not its product
- union builder type is not set as a union
- builders for nested types are not correctly emitted
- Class/InterfaceTemplate use different logic to emit the inner classes

JIRA: MDSAL-320
Change-Id: I626fb4ac42ae6528bc98b809bc33756e8daa08b9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/CompilationTest.java
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal320/mdsal320.yang [new file with mode: 0644]

index 403f5ebfefde246f64a92e18760d20cea5c360bd..d8d40a575a14faf2b04d807d8345ff941efadafb 100644 (file)
@@ -487,6 +487,17 @@ abstract class BaseTemplate extends JavaFileTemplate {
         return actualType.restrictions;
     }
 
+    def protected generateInnerClass(GeneratedType innerClass) '''
+        «IF innerClass instanceof GeneratedTransferObject»
+            «val innerJavaType = javaType.getEnclosedType(innerClass.identifier)»
+            «IF innerClass.unionType»
+                «new UnionTemplate(innerJavaType, innerClass).generateAsInnerClass»
+            «ELSE»
+                «new ClassTemplate(innerJavaType, innerClass).generateAsInnerClass»
+            «ENDIF»
+        «ENDIF»
+    '''
+
     def static Restrictions getRestrictions(Type type) {
         if (type instanceof ConcreteType) {
             return type.restrictions
index 7c713e101611609a97eef9c09b1c0c769e9c232c..e34952a04c36b23d4841a441a3fc226922f435a8 100644 (file)
@@ -184,9 +184,7 @@ class ClassTemplate extends BaseTemplate {
     def protected innerClassesDeclarations() '''
         «IF !type.enclosedTypes.empty»
             «FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
-                «IF (innerClass instanceof GeneratedTransferObject)»
-                    «new ClassTemplate(javaType.getEnclosedType(innerClass.identifier), innerClass).generateAsInnerClass»
-                «ENDIF»
+                «generateInnerClass(innerClass)»
             «ENDFOR»
         «ENDIF»
     '''
index 81bd8067577270ed14680aa7c5f486c88cd4a8f3..ba99afceaa2f367773d2b89597c94f90dba19499 100644 (file)
@@ -11,7 +11,6 @@ import java.util.List
 import org.opendaylight.mdsal.binding.model.api.AnnotationType
 import org.opendaylight.mdsal.binding.model.api.Constant
 import org.opendaylight.mdsal.binding.model.api.Enumeration
-import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.GeneratedType
 import org.opendaylight.mdsal.binding.model.api.MethodSignature
 import org.opendaylight.mdsal.binding.model.util.TypeConstants
@@ -122,17 +121,7 @@ class InterfaceTemplate extends BaseTemplate {
     def private generateInnerClasses() '''
         «IF !enclosedGeneratedTypes.empty»
             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
-                «IF (innerClass instanceof GeneratedTransferObject)»
-                    «val innerJavaType = javaType.getEnclosedType(innerClass.identifier)»
-                    «IF innerClass.unionType»
-                        «val unionTemplate = new UnionTemplate(innerJavaType, innerClass)»
-                        «unionTemplate.generateAsInnerClass»
-                    «ELSE»
-                        «val classTemplate = new ClassTemplate(innerJavaType, innerClass)»
-                        «classTemplate.generateAsInnerClass»
-                    «ENDIF»
-
-                «ENDIF»
+                «generateInnerClass(innerClass)»
             «ENDFOR»
         «ENDIF»
     '''
index 540397e12f399dd604907384da26e2b344cffe4a..344962e2e54064d649a063446c6375606e7dfbee 100644 (file)
@@ -656,6 +656,15 @@ public class CompilationTest extends BaseCompilationTest {
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    @Test
+    public void twoNestedUnionsTest() throws Exception {
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
+        generateTestSources("/compilation/mdsal320", sourcesOutputDir);
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
             final String returnTypeStr) throws NoSuchMethodException {
         Method method = clazz.getMethod(methodName);
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal320/mdsal320.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal320/mdsal320.yang
new file mode 100644 (file)
index 0000000..3c1f66c
--- /dev/null
@@ -0,0 +1,26 @@
+module yt320 {
+    namespace "urn:odl:yt320";
+    prefix yt320;
+
+    container foo {
+        leaf bar {
+            type union {
+                type enumeration {
+                    enum "foo";
+                }
+                type string {
+                    length 2;
+                }
+                type union {
+                    type enumeration {
+                        enum bar;
+                    }
+                    type string {
+                        length 1;
+                    }
+                }
+            }
+        }
+    }
+}
+