This is an automated email from the ASF dual-hosted git repository.

olabusayo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-daffodil.git


The following commit(s) were added to refs/heads/master by this push:
     new 7be19a1  Refactor to Reduce Duplication
7be19a1 is described below

commit 7be19a16eaf6a50cb22dd03f75ac16eee208b663
Author: olabusayoT <[email protected]>
AuthorDate: Fri Apr 17 12:47:59 2020 -0400

    Refactor to Reduce Duplication
    
    -- both repeating seq parser/unparser shared some of the logic for their
    end of array checks, so I moved that logic into a trait that they could
    both share
    
    DAFFODIL-2275
---
 .../unparsers/SequenceChildUnparsers.scala         | 27 ++-------
 .../processors/parsers/SequenceChildBases.scala    | 65 +++++++++++++---------
 2 files changed, 43 insertions(+), 49 deletions(-)

diff --git 
a/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/SequenceChildUnparsers.scala
 
b/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/SequenceChildUnparsers.scala
index 0cbb032..c98f694 100644
--- 
a/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/SequenceChildUnparsers.scala
+++ 
b/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/SequenceChildUnparsers.scala
@@ -24,7 +24,7 @@ import org.apache.daffodil.api.ValidationMode
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.DIArray
 import org.apache.daffodil.equality._
-import org.apache.daffodil.processors.parsers.MinMaxRepeatsMixin
+import org.apache.daffodil.processors.parsers.{EndArrayChecksMixin, 
MinMaxRepeatsMixin}
 import org.apache.daffodil.schema.annotation.props.gen.OccursCountKind
 import org.apache.daffodil.processors.ModelGroupRuntimeData
 
@@ -58,7 +58,8 @@ abstract class RepeatingChildUnparser(
   override val srd: SequenceRuntimeData,
   val erd: ElementRuntimeData)
   extends SequenceChildUnparser(childUnparser, srd, erd)
-  with MinMaxRepeatsMixin {
+  with MinMaxRepeatsMixin
+  with EndArrayChecksMixin {
 
   /**
    * Unparse exactly one occurrence of an array/optional element.
@@ -112,27 +113,7 @@ abstract class RepeatingChildUnparser(
 
     Assert.invariant(state.processorStatus eq Success)
 
-    val shouldValidate =
-      state.dataProc.isDefined && state.dataProc.value.validationMode != 
ValidationMode.Off
-
-    if (shouldValidate) {
-      val minO = erd.minOccurs
-      val maxO = erd.maxOccurs
-      val isUnbounded = maxO == -1
-      val occurrence = actualOccurs - 1
-
-      if (isUnbounded && occurrence < minO)
-        state.validationError("%s occurred '%s' times when it was expected to 
be a " +
-          "minimum of '%s' and a maximum of 'UNBOUNDED' times.", 
erd.diagnosticDebugName,
-          occurrence, minO)
-      else if (!isUnbounded && (occurrence < minO || occurrence > maxO))
-        state.validationError("%s occurred '%s' times when it was expected to 
be a " +
-          "minimum of '%s' and a maximum of '%s' times.", 
erd.diagnosticDebugName,
-          occurrence, minO, maxO)
-      else {
-        //ok
-      }
-    }
+    endArray(state, actualOccurs)
   }
 
   /**
diff --git 
a/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/SequenceChildBases.scala
 
b/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/SequenceChildBases.scala
index 4ad50a8..f25d77a 100644
--- 
a/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/SequenceChildBases.scala
+++ 
b/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/SequenceChildBases.scala
@@ -282,7 +282,8 @@ abstract class RepeatingChildParser(
   val erd: ElementRuntimeData,
   baseName: String)
   extends SequenceChildParser(childParser, srd, erd)
-  with MinMaxRepeatsMixin {
+  with MinMaxRepeatsMixin
+  with EndArrayChecksMixin {
 
   final def maybeStaticRequiredOptionalStatus: Maybe[RequiredOptionalStatus] = 
Maybe.Nope
 
@@ -351,31 +352,7 @@ abstract class RepeatingChildParser(
    */
   def endArray(state: PState): Unit = {
     val actualOccurs = state.mpstate.arrayIndexStack.pop()
-
-    if (state.processorStatus eq Success) {
-
-      val shouldValidate =
-        state.dataProc.isDefined && state.dataProc.value.validationMode != 
ValidationMode.Off
-
-      if (shouldValidate) {
-        val minO = erd.minOccurs
-        val maxO = erd.maxOccurs
-        val isUnbounded = maxO == -1
-        val occurrence = actualOccurs - 1
-
-        if (isUnbounded && occurrence < minO)
-          state.validationError("%s occurred '%s' times when it was expected 
to be a " +
-            "minimum of '%s' and a maximum of 'UNBOUNDED' times.", 
erd.diagnosticDebugName,
-            occurrence, minO)
-        else if (!isUnbounded && (occurrence < minO || occurrence > maxO))
-          state.validationError("%s occurred '%s' times when it was expected 
to be a " +
-            "minimum of '%s' and a maximum of '%s' times.", 
erd.diagnosticDebugName,
-            occurrence, minO, maxO)
-        else {
-          //ok
-        }
-      }
-    }
+    super.endArray(state, actualOccurs)
   }
 
 }
@@ -537,6 +514,42 @@ trait MinMaxRepeatsMixin {
 }
 
 /**
+ * Trait shared by both repeating sequence child parser and unparsers
+ * for things that must be done at the end of an array
+ */
+trait EndArrayChecksMixin {
+
+  def erd: ElementRuntimeData
+
+  def endArray(state: ParseOrUnparseState, actualOccurs: Long): Unit = {
+    if (state.processorStatus eq Success) {
+
+      val shouldValidate =
+        state.dataProc.isDefined && state.dataProc.value.validationMode != 
ValidationMode.Off
+
+      if (shouldValidate) {
+        val minO = erd.minOccurs
+        val maxO = erd.maxOccurs
+        val isUnbounded = maxO == -1
+        val occurrence = actualOccurs - 1
+
+        if (isUnbounded && occurrence < minO)
+          state.validationError("%s occurred '%s' times when it was expected 
to be a " +
+            "minimum of '%s' and a maximum of 'UNBOUNDED' times.", 
erd.diagnosticDebugName,
+            occurrence, minO)
+        else if (!isUnbounded && (occurrence < minO || occurrence > maxO))
+          state.validationError("%s occurred '%s' times when it was expected 
to be a " +
+            "minimum of '%s' and a maximum of '%s' times.", 
erd.diagnosticDebugName,
+            occurrence, minO, maxO)
+        else {
+          //ok
+        }
+      }
+    }
+  }
+}
+
+/**
  * Base class for parsers for terms which are parsed speculatively.
  *
  * These may respect min/maxOccurs, or may not depending on the 
occursCountKind.

Reply via email to