Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix ConfigEvalEngine.evalObjectRecursive(). #1077

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ private ObjectNode evalObjectRecursive(ObjectNode local)
throws TemplateException
{
ObjectNode built = local.objectNode();
return evalObjectRecursive(built, built, local);
}

private ObjectNode evalObjectRecursive(ObjectNode built, ObjectNode reference, ObjectNode local)
throws TemplateException
{
for (Map.Entry<String, JsonNode> pair : ImmutableList.copyOf(local.fields())) {
JsonNode value = pair.getValue();
JsonNode evaluated;
Expand All @@ -153,15 +159,15 @@ private ObjectNode evalObjectRecursive(ObjectNode local)
evaluated = value;
}
else if (value.isObject()) {
evaluated = evalObjectRecursive((ObjectNode) value);
evaluated = evalObjectRecursive(local.objectNode(), built, (ObjectNode) value);
}
else if (value.isArray()) {
evaluated = evalArrayRecursive(built, (ArrayNode) value);
}
else if (value.isTextual()) {
// eval using template engine
String code = value.textValue();
evaluated = evalValue(built, code);
evaluated = evalValue(reference, code);
}
else {
evaluated = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public void testBasic()
is(loadYamlResource("/io/digdag/core/agent/eval/basic_expected.dig")));
}

@Test
public void testNestedVars()
throws Exception
{
assertThat(
engine.eval(loadYamlResource("/io/digdag/core/agent/eval/nested_vars.dig"), params()),
is(loadYamlResource("/io/digdag/core/agent/eval/nested_vars_expected.dig")));
}

@Test
public void testLiteral()
throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -15,7 +14,6 @@
import io.digdag.client.config.ConfigException;
import io.digdag.client.config.ConfigUtils;
import io.digdag.core.DigdagEmbed;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -25,11 +23,10 @@
import org.junit.rules.TemporaryFolder;
import static java.nio.charset.StandardCharsets.UTF_8;
import static io.digdag.client.config.ConfigUtils.newConfig;
import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;
import static io.digdag.core.workflow.WorkflowTestingUtils.loadYamlResource;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

public class WorkflowExecutorTest
{
Expand Down Expand Up @@ -181,10 +178,27 @@ public void ifOperatorDelayedEvalElseDo()

}


private void runWorkflow(String workflowName, Config config)
throws Exception
{
WorkflowTestingUtils.runWorkflow(embed, folder.getRoot().toPath(), workflowName, config);
}

// variables of sibling task should not be referable.
@Test
public void unscopedVariableReference()
throws Exception
{
runWorkflow("unscoped_variable_reference", loadYamlResource("/io/digdag/core/workflow/unscoped_variable_ref.dig"));
assertFalse(folder.getRoot().toPath().resolve("out").toFile().exists()); // workflow failed and no output file generated
}

// nested variables should be substituted and should be referable from correct scope
@Test
public void scopedNestedVariableReference()
throws Exception
{
runWorkflow("scoped_nested_variable_reference", loadYamlResource("/io/digdag/core/workflow/scoped_nested_variable_ref.dig"));
assertThat(new String(Files.readAllBytes(folder.getRoot().toPath().resolve("out")), UTF_8), is("1970-01-01")); //should be substituted correctly
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
timezone: UTC
session_time: 2016-03-13T10:00:00Z
default_format: ${moment(session_time).add(1, 'days').format()}
arg1: ${default_format}
arg2:
sub1: ${default_format}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
timezone: UTC
session_time: 2016-03-13T10:00:00Z
default_format: 2016-03-14T10:00:00+00:00
arg1: 2016-03-14T10:00:00+00:00
arg2:
sub1: 2016-03-14T10:00:00+00:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+task1:
_export:
date1: ${moment(0).utc().format('YYYY-MM-DD')}
nested:
date2: ${date1}
+task2:
echo>: ${nested.date2}
append_file: out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+task1:
_export:
date1: ${moment(0).utc().format('YYYY-MM-DD')}
nested:
date2: ${date1}
+task2:
echo>: ${nested.date2}
append_file: out