Skip to content

Commit 34630b5

Browse files
committed
Assign Type to Overridden UDF Function
- specify types for overridden getUserDefinedFunctionClasses method - remove empty namespace from xml literals - specify longs for digits otherwise the assert fails on comparing an int and a long - During NonEmptyString initialization, it requires TypeNode(String), which requires NodeInfo to be initialized, which tries to initialize the members of allTypes of which NonEmptyString is one. This results in NonEmptyString returning null. So we update the code to not force initialization of allType members during NodeInfo initialization. Instead, the members are initialized on an as needed basis - Also we make parents call by name so they can also reference their children - update scalafmt to format import selectors one per line as per example below. We already do this in code, but this is just to ensure newer code matches the format ``` import a.b.{ c, d } ``` DAFFODIL-2975
1 parent 52f41c6 commit 34630b5

File tree

13 files changed

+49
-77
lines changed

13 files changed

+49
-77
lines changed

.scalafmt.conf

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ docstrings.style = keep
2020
indent.defnSite = 2
2121
indent.extendSite = 2
2222
maxColumn = 96
23+
importSelectors = noBinPack
2324
rewrite.imports.groups = [
2425
["scala\\..*", "java\\..*", "javax\\..*"],
2526
["org\\.apache\\.daffodil\\..*"],

daffodil-io/src/test/scala/org/apache/daffodil/io/TestInputSourceDataInputStream.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class TestInputSourceDataInputStream {
332332
@Test def testUnsignedBigInt1(): Unit = {
333333
val dis = InputSourceDataInputStream(List(0xff).map { _.toByte }.toArray)
334334
val ml = dis.getUnsignedBigInt(2, finfo)
335-
assertEqualsTyped(2, dis.bitPos0b)
335+
assertEqualsTyped(2L, dis.bitPos0b)
336336
val expected = JBigInt.valueOf(3)
337337
assertEqualsTyped[JBigInt](expected, ml)
338338
}
@@ -342,7 +342,7 @@ class TestInputSourceDataInputStream {
342342
_.toByte
343343
}.toArray)
344344
val ml = dis.getUnsignedBigInt(40, finfo)
345-
assertEqualsTyped(40, dis.bitPos0b)
345+
assertEqualsTyped(40L, dis.bitPos0b)
346346
val expected = JBigInt.valueOf(0xc1c2c3c4c5L)
347347
assertEqualsTyped[JBigInt](expected, ml)
348348
}

daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/dpath/NodeInfo.scala

+33-62
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import java.net.URISyntaxException
3434
import org.apache.daffodil.lib.calendar.DFDLDateConversion
3535
import org.apache.daffodil.lib.calendar.DFDLDateTimeConversion
3636
import org.apache.daffodil.lib.calendar.DFDLTimeConversion
37-
import org.apache.daffodil.lib.exceptions.Assert
3837
import org.apache.daffodil.lib.util.Delay
3938
import org.apache.daffodil.lib.util.Enum
4039
import org.apache.daffodil.lib.util.MaybeInt
@@ -84,33 +83,15 @@ sealed abstract class TypeNode private (
8483
childrenDelay: Delay[Seq[NodeInfo.Kind]]
8584
) extends Serializable
8685
with NodeInfo.Kind {
87-
8886
def this(parents: => Seq[NodeInfo.Kind], children: => Seq[NodeInfo.Kind]) =
8987
this(Delay("TypeNode", parents), Delay("TypeNode", children))
9088

91-
def this(parentArg: NodeInfo.Kind, childrenArg: => Seq[NodeInfo.Kind]) =
92-
this(Delay("TypeNode", Seq(parentArg)), Delay("TypeNode", childrenArg))
93-
9489
def this(parentArg: => NodeInfo.Kind) =
9590
this(
9691
Delay("TypeNode", Seq(parentArg)),
9792
Delay("TypeNode", Seq[NodeInfo.Kind](NodeInfo.Nothing))
9893
)
9994

100-
/**
101-
* Cyclic structures require an initialization
102-
*/
103-
lazy val initialize: Unit = {
104-
parents
105-
children // demand their value
106-
parents.foreach { p =>
107-
// if this fails, it is because cyclic graph construction of the types
108-
// has failed. For some reason, this doesn't cause a stack overflow, but
109-
// you just get null as the value of one of the case object type nodes.
110-
Assert.invariant(p ne null)
111-
}
112-
}
113-
11495
final override lazy val parents = parentsDelay.value
11596
final override lazy val children = childrenDelay.value
11697
}
@@ -122,12 +103,11 @@ sealed abstract class TypeNode private (
122103
*/
123104
sealed abstract class PrimTypeNode(
124105
val dfdlType: DFDLPrimType,
125-
parent: NodeInfo.Kind,
106+
parent: => NodeInfo.Kind,
126107
childrenArg: => Seq[NodeInfo.Kind]
127-
) extends TypeNode(parent, childrenArg)
108+
) extends TypeNode(Seq(parent), childrenArg)
128109
with NodeInfo.PrimType {
129-
130-
def this(javaType: DFDLPrimType, parent: NodeInfo.Kind) =
110+
def this(javaType: DFDLPrimType, parent: => NodeInfo.Kind) =
131111
this(javaType, parent, Seq(NodeInfo.Nothing))
132112

133113
}
@@ -173,16 +153,6 @@ class InvalidPrimitiveDataException(msg: String, cause: Throwable = null)
173153
*/
174154
object NodeInfo extends Enum {
175155

176-
/**
177-
* Cyclic structures require initialization
178-
*/
179-
private lazy val initialize: Boolean = {
180-
allTypes.foreach {
181-
_.initialize
182-
}
183-
true
184-
}
185-
186156
// Primitives are not "global" because they don't appear in any schema document
187157

188158
sealed trait PrimType extends PrimTypeKind {
@@ -256,6 +226,7 @@ object NodeInfo extends Enum {
256226
case _ => QName.createGlobal(name, NoNamespace, scala.xml.TopScope)
257227
}
258228
}
229+
259230
val ClassString = classOf[java.lang.String]
260231
val ClassIntBoxed = classOf[java.lang.Integer]
261232
val ClassIntPrim = classOf[scala.Int]
@@ -392,14 +363,16 @@ object NodeInfo extends Enum {
392363
protected sealed trait AnySimpleTypeKind extends AnyType.Kind {
393364
final def primType: PrimType = optPrimType.get
394365
}
395-
case object AnySimpleType extends TypeNode(AnyType, Seq(AnyAtomic)) with AnySimpleTypeKind {
366+
case object AnySimpleType
367+
extends TypeNode(Seq(AnyType), Seq(AnyAtomic))
368+
with AnySimpleTypeKind {
396369
type Kind = AnySimpleTypeKind
397370
}
398371

399372
protected sealed trait AnyAtomicKind extends AnySimpleType.Kind
400373
case object AnyAtomic
401374
extends TypeNode(
402-
AnySimpleType,
375+
Seq(AnySimpleType),
403376
Seq(String, Numeric, Boolean, Opaque, AnyDateTime, AnyURI)
404377
)
405378
with AnyAtomicKind {
@@ -408,27 +381,27 @@ object NodeInfo extends Enum {
408381

409382
protected sealed trait NumericKind extends AnyAtomic.Kind
410383
case object Numeric
411-
extends TypeNode(AnyAtomic, Seq(SignedNumeric, UnsignedNumeric))
384+
extends TypeNode(Seq(AnyAtomic), Seq(SignedNumeric, UnsignedNumeric))
412385
with NumericKind {
413386
type Kind = NumericKind
414387
}
415388

416389
protected sealed trait SignedNumericKind extends Numeric.Kind
417390
case object SignedNumeric
418-
extends TypeNode(Numeric, Seq(Float, Double, Decimal))
391+
extends TypeNode(Seq(Numeric), Seq(Float, Double, Decimal))
419392
with SignedNumericKind {
420393
type Kind = SignedNumericKind
421394
}
422395

423396
protected sealed trait UnsignedNumericKind extends Numeric.Kind
424397
case object UnsignedNumeric
425-
extends TypeNode(Numeric, Seq(NonNegativeInteger))
398+
extends TypeNode(Seq(Numeric), Seq(NonNegativeInteger))
426399
with UnsignedNumericKind {
427400
type Kind = UnsignedNumericKind
428401
}
429402

430403
protected sealed trait OpaqueKind extends AnyAtomic.Kind
431-
case object Opaque extends TypeNode(AnyAtomic, Seq(HexBinary)) with OpaqueKind {
404+
case object Opaque extends TypeNode(Seq(AnyAtomic), Seq(HexBinary)) with OpaqueKind {
432405
type Kind = OpaqueKind
433406
}
434407

@@ -452,33 +425,33 @@ object NodeInfo extends Enum {
452425

453426
protected sealed trait AnyDateTimeKind extends AnyAtomicKind
454427
case object AnyDateTime
455-
extends TypeNode(AnyAtomic, Seq(Date, Time, DateTime))
428+
extends TypeNode(Seq(AnyAtomic), Seq(Date, Time, DateTime))
456429
with AnyDateTimeKind {
457430
type Kind = AnyDateTimeKind
458431
}
459432

460433
// One might think these can be def, but scala insists on "stable identifier"
461434
// where these are used in case matching.
462-
val String = PrimType.String
463-
val Int = PrimType.Int
464-
val Byte = PrimType.Byte
465-
val Short = PrimType.Short
466-
val Long = PrimType.Long
467-
val Integer = PrimType.Integer
468-
val Decimal = PrimType.Decimal
469-
val UnsignedInt = PrimType.UnsignedInt
470-
val UnsignedByte = PrimType.UnsignedByte
471-
val UnsignedShort = PrimType.UnsignedShort
472-
val UnsignedLong = PrimType.UnsignedLong
473-
val NonNegativeInteger = PrimType.NonNegativeInteger
474-
val Double = PrimType.Double
475-
val Float = PrimType.Float
476-
val HexBinary = PrimType.HexBinary
477-
val AnyURI = PrimType.AnyURI
478-
val Boolean = PrimType.Boolean
479-
val DateTime = PrimType.DateTime
480-
val Date = PrimType.Date
481-
val Time = PrimType.Time
435+
lazy val String = PrimType.String
436+
lazy val Int = PrimType.Int
437+
lazy val Byte = PrimType.Byte
438+
lazy val Short = PrimType.Short
439+
lazy val Long = PrimType.Long
440+
lazy val Integer = PrimType.Integer
441+
lazy val Decimal = PrimType.Decimal
442+
lazy val UnsignedInt = PrimType.UnsignedInt
443+
lazy val UnsignedByte = PrimType.UnsignedByte
444+
lazy val UnsignedShort = PrimType.UnsignedShort
445+
lazy val UnsignedLong = PrimType.UnsignedLong
446+
lazy val NonNegativeInteger = PrimType.NonNegativeInteger
447+
lazy val Double = PrimType.Double
448+
lazy val Float = PrimType.Float
449+
lazy val HexBinary = PrimType.HexBinary
450+
lazy val AnyURI = PrimType.AnyURI
451+
lazy val Boolean = PrimType.Boolean
452+
lazy val DateTime = PrimType.DateTime
453+
lazy val Date = PrimType.Date
454+
lazy val Time = PrimType.Time
482455

483456
protected sealed trait PrimTypeKind extends AnyAtomic.Kind
484457

@@ -1048,6 +1021,4 @@ object NodeInfo extends Enum {
10481021
NonEmptyString,
10491022
Nothing
10501023
) ++ allAbstractTypes
1051-
1052-
initialize // initialize self - creates all cyclic structures
10531024
}

daffodil-test/src/test/scala/org/apache/daffodil/runtime1/layers/TestCheckDigit.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class TestCheckDigit extends TdmlTests {
114114
}
115115

116116
val okInfoset: Elem =
117-
<ex:e1 xmlns="" xmlns:ex={example}>
117+
<ex:e1 xmlns:ex={example}>
118118
<value>2021-09-25</value>
119119
<checkDigit>1</checkDigit>
120120
<computedCheckDigit>1</computedCheckDigit>
@@ -200,7 +200,7 @@ class TestCheckDigit extends TdmlTests {
200200
val sch = cdSchema(10, "ascii")
201201
val data = "123:6"
202202
val infoset =
203-
<ex:e1 xmlns="" xmlns:ex={example}>
203+
<ex:e1 xmlns:ex={example}>
204204
<value>123</value>
205205
<checkDigit>0</checkDigit>
206206
<computedCheckDigit>0</computedCheckDigit>
@@ -212,7 +212,7 @@ class TestCheckDigit extends TdmlTests {
212212
val sch = cdSchema(5, "ascii")
213213
val data = "1234567890:1"
214214
val infoset =
215-
<ex:e1 xmlns="" xmlns:ex={example}>
215+
<ex:e1 xmlns:ex={example}>
216216
<value>1234567890</value>
217217
<checkDigit>0</checkDigit>
218218
<computedCheckDigit>0</computedCheckDigit>
@@ -228,7 +228,7 @@ class TestCheckDigit extends TdmlTests {
228228
val sch = cdSchema(10, "notACharsetName")
229229
val data = "1234567890:1"
230230
val infoset =
231-
<ex:e1 xmlns="" xmlns:ex={example}>
231+
<ex:e1 xmlns:ex={example}>
232232
<value>1234567890</value>
233233
<checkDigit>0</checkDigit>
234234
<computedCheckDigit>0</computedCheckDigit>

daffodil-udf/src/test/scala/org/sbadudfs/functionclasses/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import org.apache.daffodil.udf.UserDefinedFunctionProvider
2626
* Contains incorrect implementation of lookup function
2727
*/
2828
class StringFunctionsProvider extends UserDefinedFunctionProvider {
29-
override def getUserDefinedFunctionClasses = {
29+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
3030
Array(classOf[ReverseWords], classOf[Reverse])
3131
}
3232

daffodil-udf/src/test/scala/org/sbadudfs/functionclasses2/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class StringFunctionsProvider extends UserDefinedFunctionProvider {
3131
val nonSerializable = new SomeNonSerializableClass
3232
val serializable = new SomeSerializableClass
3333

34-
override def getUserDefinedFunctionClasses = {
34+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
3535
Array(classOf[GetNonSerializableState], classOf[GetSerializableState])
3636
}
3737

daffodil-udf/src/test/scala/org/sbadudfs/udfexceptions/evaluating/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.apache.daffodil.udf.exceptions.UserDefinedFunctionProcessingError
2828
*/
2929
class StringFunctionsProvider extends UserDefinedFunctionProvider {
3030

31-
override def getUserDefinedFunctionClasses = {
31+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
3232
Array(classOf[ReverseWords], classOf[Reverse])
3333
}
3434
}

daffodil-udf/src/test/scala/org/sbadudfs/udfexceptions2/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import org.apache.daffodil.udf.UserDefinedFunctionProvider
2727
*/
2828
class StringFunctionsProvider extends UserDefinedFunctionProvider {
2929

30-
override def getUserDefinedFunctionClasses = {
30+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
3131
Array(classOf[ReverseWords], classOf[Reverse])
3232
}
3333
}

daffodil-udf/src/test/scala/org/sbadudfs/udfpexceptions/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class StringFunctionsProvider extends UserDefinedFunctionProvider {
3131
private val cause: Throwable = None.orNull
3232
) extends Exception(message, cause)
3333

34-
override def getUserDefinedFunctionClasses = {
34+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
3535
throw new CustomException("UDFP Error!")
3636
Array(classOf[ReverseWords], classOf[Reverse])
3737
}

daffodil-udf/src/test/scala/org/sbadudfs/udfpexceptions2/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class StringFunctionsProvider extends UserDefinedFunctionProvider {
3232
) extends Exception(message, cause)
3333

3434
throw new CustomException("UDFP Error!")
35-
override def getUserDefinedFunctionClasses = {
35+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
3636
Array(classOf[ReverseWords], classOf[Reverse])
3737
}
3838
}

daffodil-udf/src/test/scala/org/sgoodudfs/example/IntegerFunctions/IntegerFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.apache.daffodil.udf.UserDefinedFunctionProvider
2525
*
2626
*/
2727
class IntegerFunctionsProvider extends UserDefinedFunctionProvider {
28-
override def getUserDefinedFunctionClasses = {
28+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
2929
Array(classOf[BoxedAddition], classOf[PrimitivesAddition])
3030
}
3131
}

daffodil-udf/src/test/scala/org/sgoodudfs/example/StringFunctions/StringFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.apache.daffodil.udf.UserDefinedFunctionProvider
2525
*
2626
*/
2727
class StringFunctionsProvider extends UserDefinedFunctionProvider {
28-
override def getUserDefinedFunctionClasses = {
28+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
2929
Array(classOf[ReverseWords], classOf[Reverse], classOf[SayHello])
3030
}
3131
}

daffodil-udf/src/test/scala/org/sgoodudfs/example/SupportedTypesFunctions/SupportedTypesFunctionsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.apache.daffodil.udf.UserDefinedFunctionProvider
2525
*
2626
*/
2727
class SupportedTypesFunctionsProvider extends UserDefinedFunctionProvider {
28-
override def getUserDefinedFunctionClasses = {
28+
override def getUserDefinedFunctionClasses: Array[Class[?]] = {
2929
Array(
3030
classOf[PrimByteFunc],
3131
classOf[BoxedByteFunc],

0 commit comments

Comments
 (0)