Skip to content

Commit

Permalink
Implement ToChars in the engine (#1391)
Browse files Browse the repository at this point in the history
Implement ToChars
  • Loading branch information
antvaset authored Jul 30, 2024
1 parent f601a42 commit 846450e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ public Invocation resolveSystemFunction(FunctionRef fun) {
case "ToTime":
case "ToQuantity":
case "ToRatio":
case "ToConcept": {
case "ToConcept":
case "ToChars": {
return resolveUnary(fun);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@
<output>false</output>
</test>
</group>
<group name="ToChars">
<test name="StringToChars">
<expression>ToChars('abc123')</expression>
<output>{ 'a', 'b', 'c', '1', '2', '3' }</output>
</test>
<test name="EmptyStringToChars">
<expression>ToChars('')</expression>
<output>{}</output>
</test>
<test name="NullToChars">
<expression>ToChars(null)</expression>
<output>null</output>
</test>
<test name="ToCharsMalformed">
<expression invalid="true">ToChars(123)</expression>
</test>
</group>
<group name="ToConcept">
<test name="CodeToConcept1">
<expression>ToConcept(Code { code: '8480-6' })</expression>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.opencds.cqf.cql.engine.elm.executing;

import org.opencds.cqf.cql.engine.exception.InvalidOperatorArgument;

import java.util.ArrayList;
import java.util.List;

/*
ToChars(argument String) List<String>
The ToChars operator takes a string and returns a list with one string for each character in the input, in the order in which they appear in the string.
If the argument is null, the result is null.
*/

public class ToCharsEvaluator {

public static List<String> toChars(Object operand) {
if (operand == null) {
return null;
}

if (operand instanceof String) {
List<String> result = new ArrayList<>();
for (char c : ((String) operand).toCharArray()) {
result.add(String.valueOf(c));
}
return result;
}

throw new InvalidOperatorArgument(
"ToChars(String)",
String.format("ToInteger(%s)", operand.getClass().getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ public Object visitToConcept(ToConcept elm, State state) {
return ToConceptEvaluator.toConcept(operand);
}

@Override
public Object visitToChars(ToChars elm, State state) {
Object operand = visitExpression(elm.getOperand(), state);
return ToCharsEvaluator.toChars(operand);
}

@Override
public Object visitToDate(ToDate elm, State state) {
Object operand = visitExpression(elm.getOperand(), state);
Expand Down

0 comments on commit 846450e

Please sign in to comment.