toItem static method

XPathItem toItem(
  1. Object obj
)

Converts an arbitrary object into a valid XPathItem.

Implementation

static XPathItem toItem(Object obj) => switch (obj) {
  final XPathItem item => item,
  final XmlNode node => XPathNode(node),
  final XmlName name => XPathQName(name),
  final bool b => XPathBoolean(b),
  final double d when !d.isFinite => XPathDouble(d),
  final int i => XPathInteger.fromInt(i),
  final BigInt bi => XPathInteger(bi),
  final double d => XPathDouble(d),
  final String s => XPathString(s),
  final DateTime dt => XPathDateTime.fromDateTime(dt, 0),
  final Duration dur => XPathDuration.fromDuration(dur),
  final Uint8List bytes => XPathBinary(bytes),
  final Map<XPathAtomic, XPathSequence> m => XPathMap(m),
  final Map<Object, Object?> m => XPathMap({
    for (final entry in m.entries)
      toItem(entry.key) as XPathAtomic: fromObject(entry.value),
  }),
  final List<XPathSequence> l => XPathArray(l),
  final List<Object?> l => XPathArray([for (final e in l) fromObject(e)]),
  _ => throw XPathEvaluationException(
    XPathErrorCode.XPTY0004,
    'Cannot convert ${obj.runtimeType} to XPathItem',
  ),
};