stevedlawrence commented on a change in pull request #207: Added support for
enumerations and TypeValueCalc
URL: https://github.com/apache/incubator-daffodil/pull/207#discussion_r278607505
##########
File path:
daffodil-core/src/main/scala/org/apache/daffodil/dsom/SimpleTypes.scala
##########
@@ -40,14 +69,70 @@ trait NonPrimTypeMixin {
}
}
-sealed trait SimpleTypeBase extends TypeBase {
- def primType: NodeInfo.PrimType = {
- optRestriction.map { _.primType }.getOrElse {
- optUnion.map { _.primType }.getOrElse {
- Assert.invariantFailed("must be either a restriction or union")
+sealed trait SimpleTypeBase extends TypeBase
+ with hasOptRepTypeMixin {
+
+ def primType: PrimType
+
+}
+
+/*
+ * For components which can define dfdl:repValues and dfdl:repValueRanges
+ * Construct the repValueSet using only the above mentioned attributes on the
element itself
+ * Applies to simpleType and enumeration
+ *
+ * In the case of simpleType, it is possible that optRepValueSetFromAttribute
will be none
+ * but the element will still have an optRepValueSet for another source (eg.
children elements)
+ */
+sealed trait HasRepValueAttributes extends AnnotatedSchemaComponent {
+ def optRepTypeFactory: Option[SimpleTypeFactory]
+ def optRepValueSet: Option[RepValueSet[AnyRef]]
+
+ lazy val (repValuesAttrCooked: Seq[AnyRef], repValueRangesAttrCooked:
Seq[(RangeBound[BigInt], RangeBound[BigInt])]) = optRepTypeFactory match {
+ case Some(repType) => {
+ val repValueSetRaw =
findPropertyOptionThisComponentOnly("repValues").toOption
+ .map(_.split("\\s+").toSeq).getOrElse(Seq())
+ val repValueRangesRaw =
findPropertyOptionThisComponentOnly("repValueRanges").toOption
+ .map(_.split("\\s+").toSeq).getOrElse(Seq())
+ repType.primType match {
+ case PrimType.String => {
+ if (repValueRangesRaw.size > 0) context.SDE("repValueRanges set when
using a string repType")
+ val repValueSetCooked =
repValueRangesRaw.map(RepValueCooker.convertConstant(_, context, false))
+ (repValueSetCooked, Seq())
+ }
+ case _: NodeInfo.Integer.Kind => {
+ if (repValueRangesRaw.size % 2 != 0) context.SDE("repValueRanges
must have an even number of elements")
+ val ans1 = repValueSetRaw.map(BigInt(_))
+ def cookRepValueRanges(xs: Seq[String]): Seq[(RangeBound[BigInt],
RangeBound[BigInt])] = {
+ xs match {
+ case Seq() => Seq()
+ case a +: b +: rest => {
+ val a2 = BigInt(a)
+ val b2 = BigInt(b)
+ if (a2.compare(b2) > 0) {
+ context.SDE("min value must not be greater than max value")
+ }
+ val a3 = new RangeBound(Maybe(a2), true)
+ val b3 = new RangeBound(Maybe(b2), true)
+ (a3, b3) +: cookRepValueRanges(rest)
+ }
+ }
+ }
+ val ans2 = cookRepValueRanges(repValueRangesRaw)
+ (ans1, ans2)
+ }
+ case x => context.SDE("repType must be either String or Integer type")
Review comment:
Do we need to catch NumberFormatException when converting strings to
BigInts? For example, if someone had ``dfdl:repValueRanges="five ten"`` that
should fail with a nice SDE, I don't think I see that being caught anywhere.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services