Squash empty lists/maps
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Apr 2020 13:11:41 +0000 (15:11 +0200)
committerAnil Belur <abelur@linuxfoundation.org>
Wed, 19 Jun 2024 00:41:32 +0000 (10:41 +1000)
Dealing with lists requires us to interfere with what the user
is giving us in builders, effectively creating new mechanics.

This patch adds the model metadata to carry these around, exposing
just how badly broken some binding.model.api construcs are misused.
We do not fix the misuse, but make it slightly worse, but also mark
the mess for future cleanup.

JIRA: MDSAL-451
Change-Id: I43b3ddccff69bec9a4e98b3c8e73aac4679c1d1f
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/AbstractBuilderTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGenerator.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderImplTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java

index 010450485b20ce354bd291c6aabd318d09b5d82f..11e8629f95cd05a78fbddea37921cc4708b891ba 100644 (file)
@@ -152,14 +152,10 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
         return generateDeprecatedAnnotation(found)
     }
 
-    def protected final CharSequence generateCopyNonKeys(Collection<GeneratedProperty> props) '''
-        «FOR field : props»
-            this.«field.fieldName» = base.«field.getterMethodName»();
-        «ENDFOR»
-    '''
-
     def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
 
+    def protected abstract CharSequence generateCopyNonKeys(Collection<GeneratedProperty> props)
+
     def protected abstract CharSequence generateCopyAugmentation(Type implType)
 
     def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
index a632394a7569f3fa8a7f782dacd6f2efaeb9ca3d..b7ac63f7b2450432f5c7f040bb441eb07e075234 100644 (file)
@@ -29,6 +29,7 @@ import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
 import org.opendaylight.mdsal.binding.model.util.Types;
@@ -215,7 +216,14 @@ public final class BuilderGenerator implements CodeGenerator {
 
         final String fieldName = StringExtensions.toFirstLower(method.getName().substring(prefix.length()));
         final GeneratedTOBuilder tmpGenTO = new CodegenGeneratedTOBuilder(JavaTypeName.create("foo", "foo"));
-        tmpGenTO.addProperty(fieldName).setReturnType(method.getReturnType());
+        final GeneratedPropertyBuilder builder = tmpGenTO.addProperty(fieldName).setReturnType(method.getReturnType());
+        switch (method.getMechanics()) {
+            case NULLIFY_EMPTY:
+                builder.setNullifyEmpty(true);
+                break;
+            default:
+                break;
+        }
         return tmpGenTO.build().getProperties().get(0);
     }
 }
index 5a37e75809aafbc86c4da39b0ad3ec0a4a9d1af6..53bffa4f18f504f946880282d73b985f1fe3f9d6 100644 (file)
@@ -147,6 +147,16 @@ class BuilderImplTemplate extends AbstractBuilderTemplate {
         «ENDFOR»
     '''
 
+    override protected  CharSequence generateCopyNonKeys(Collection<GeneratedProperty> props) '''
+        «FOR field : props»
+            «IF field.nullifyEmpty»
+                this.«field.fieldName» = «CODEHELPERS.importedName».emptyToNull(base.«field.getterMethodName»());
+            «ELSE»
+                this.«field.fieldName» = base.«field.getterMethodName»();
+            «ENDIF»
+        «ENDFOR»
+    '''
+
     override protected generateCopyAugmentation(Type implType) '''
         super(base.«AUGMENTATION_FIELD»);
     '''
index d095d726be2b9dca6061c7272ad2f656e5e69847..e4e093ae06554308394910dd888e6973647429fb 100644 (file)
@@ -432,6 +432,13 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «generateCopyNonKeys(keyProps)»
     '''
 
+
+    override protected  CharSequence generateCopyNonKeys(Collection<GeneratedProperty> props) '''
+        «FOR field : props»
+            this.«field.fieldName» = base.«field.getterMethodName»();
+        «ENDFOR»
+    '''
+
     override protected generateCopyAugmentation(Type implType) {
         val augmentationHolderRef = AugmentationHolder.importedName
         val typeRef = targetType.importedName
index bdcda6be6ab7a44cef778c76609e98a15878464a..6602747408725f7ca202481991e8738b57b170cb 100644 (file)
@@ -18,6 +18,7 @@ import org.junit.Test;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
+import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
 import org.opendaylight.mdsal.binding.model.api.Type;
 
 public class BuilderGeneratorTest {
@@ -148,6 +149,7 @@ public class BuilderGeneratorTest {
         final Type methType = mock(Type.class);
         doReturn(TYPE_NAME).when(methType).getIdentifier();
         doReturn(methType).when(methSign).getReturnType();
+        doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();
         return methSign;
     }
 }