forked from hadrienk/java-vtl
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://git-adm.ssb.no/scm/core/java-vtl in…
…to feature/is-null-operator
- Loading branch information
Showing
7 changed files
with
207 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
java-vtl-parser/src/test/java/no/ssb/vtl/parser/ConditionalParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package no.ssb.vtl.parser; | ||
|
||
|
||
import org.junit.Test; | ||
|
||
public class ConditionalParserTest extends GrammarTest { | ||
|
||
@Test | ||
public void testNvlWithNumber() throws Exception { | ||
parse("nvl(m1 , 0)", "conditionalExpression"); | ||
} | ||
|
||
@Test | ||
public void testNvlComponentRefWithString() throws Exception { | ||
parse("nvl(ds1.m2, \"constant\")", "conditionalExpression"); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
java-vtl-script/src/main/java/no/ssb/vtl/script/visitors/ConditionalExpressionVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package no.ssb.vtl.script.visitors; | ||
|
||
import no.ssb.vtl.model.Component; | ||
import no.ssb.vtl.model.DataStructure; | ||
import no.ssb.vtl.model.VTLExpression; | ||
import no.ssb.vtl.model.VTLObject; | ||
import no.ssb.vtl.parser.VTLBaseVisitor; | ||
import no.ssb.vtl.parser.VTLParser; | ||
import org.antlr.v4.runtime.misc.ParseCancellationException; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static java.lang.String.*; | ||
|
||
public class ConditionalExpressionVisitor extends VTLBaseVisitor<VTLExpression> { | ||
|
||
private final ReferenceVisitor referenceVisitor; | ||
private final DataStructure dataStructure; | ||
|
||
public ConditionalExpressionVisitor(ReferenceVisitor referenceVisitor, DataStructure dataStructure) { | ||
this.referenceVisitor = referenceVisitor; | ||
this.dataStructure = dataStructure; | ||
} | ||
|
||
@Override | ||
public VTLExpression visitNvlExpression(VTLParser.NvlExpressionContext ctx) { | ||
ParamVisitor paramVisitor = new ParamVisitor(referenceVisitor); | ||
Component input = (Component) paramVisitor.visit(ctx.componentRef()); | ||
Object repValue = paramVisitor.visit(ctx.nvlRepValue); | ||
|
||
if (input.getType() != repValue.getClass()) { | ||
throw new ParseCancellationException("The value to replace null must be of type " + input.getType()); | ||
} | ||
|
||
return new VTLExpression.Builder(input.getType(), dataPoint -> { | ||
Map<Component, VTLObject> map = dataStructure.asMap(dataPoint); | ||
Optional<VTLObject> vtlObject = Optional.ofNullable(map.get(input)); | ||
if (!vtlObject.isPresent() || vtlObject.get().get() == null) { | ||
return VTLObject.of(repValue); | ||
} else { | ||
return vtlObject.get(); | ||
} | ||
|
||
}).description(format("nvl(%s, %s)", input, repValue)).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters