processSetValue method
Performs a <setvalue> (port of SetValueAction.processAction).
Implementation
TreeReference? processSetValue(
SetValueAction action,
TreeReference? context,
) {
final target = action.target;
var targetRef = context == null ? target : target.contextualize(context)!;
final ec = EvaluationContext.withContext(evaluationContext, targetRef);
final failMessage =
'Target of TreeReference ${target.toString(includePredicates: true)} '
'could not be resolved!';
final refs = ec.expandReference(targetRef)!;
if (refs.isEmpty) {
if (mainInstance.hasTemplatePath(target)) return null;
throw StateError(failMessage);
}
if (refs.length > 1) {
throw XPathTypeMismatchException(
'You are trying to target a repeated field. Currently you may only '
'target a field in a specific repeat instance.\n\nXPath nodeset has '
'more than one node [" + references + "].',
);
}
targetRef = refs.single;
final node = ec.resolveReference(targetRef);
if (node == null) {
if (mainInstance.hasTemplatePath(target)) return null;
throw StateError(failMessage);
}
final result = unpack(action.value.eval(mainInstance, ec));
final wrapped = wrapData(result, node.dataType);
setValue(
wrapped == null ? null : castToDataType(wrapped.uncast(), node.dataType),
targetRef,
);
return targetRef;
}