use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class LiteralMapDeserializer method deserialize.
@Override
public JacksonLiteralMap deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Map<String, Literal> literalMap = new HashMap<>();
verifyToken(p, JsonToken.START_OBJECT);
p.nextToken();
while (p.currentToken() != JsonToken.END_OBJECT) {
verifyToken(p, JsonToken.FIELD_NAME);
String fieldName = p.currentName();
LiteralType literalType = literalTypeMap.get(fieldName);
if (literalType == null) {
throw new IllegalStateException("Unexpected field [" + fieldName + "]");
}
p.nextToken();
literalMap.put(fieldName, deserialize(p, ctxt, literalType));
p.nextToken();
}
return new JacksonLiteralMap(unmodifiableMap(literalMap));
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class LiteralMapDeserializer method deserialize.
private static Literal deserialize(JsonParser p, DeserializationContext ctxt, LiteralType literalType) throws IOException {
switch(literalType.getKind()) {
case SIMPLE_TYPE:
return deserialize(p, literalType.simpleType());
case COLLECTION_TYPE:
List<Literal> collection = new ArrayList<>();
verifyToken(p, JsonToken.START_ARRAY);
while (p.nextToken() != JsonToken.END_ARRAY) {
collection.add(deserialize(p, ctxt, literalType.collectionType()));
}
return Literal.ofCollection(unmodifiableList(collection));
case MAP_VALUE_TYPE:
Map<String, Literal> map = new HashMap<>();
verifyToken(p, JsonToken.START_OBJECT);
p.nextToken();
while (p.currentToken() != JsonToken.END_OBJECT) {
verifyToken(p, JsonToken.FIELD_NAME);
String fieldName = p.currentName();
p.nextToken();
map.put(fieldName, deserialize(p, ctxt, literalType.mapValueType()));
p.nextToken();
}
return Literal.ofMap(unmodifiableMap(map));
case BLOB_TYPE:
JavaType type = ctxt.constructType(Blob.class);
Blob blob = (Blob) ctxt.findNonContextualValueDeserializer(type).deserialize(p, ctxt);
return Literal.ofScalar(Scalar.ofBlob(blob));
case SCHEMA_TYPE:
throw new IllegalArgumentException(String.format("Unsupported LiteralType.Kind: [%s]", literalType.getKind()));
}
throw new AssertionError(String.format("Unexpected LiteralType.Kind: [%s]", literalType.getKind()));
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class LocalEngineTest method testBindingMap.
@Test
public void testBindingMap() {
String workflowName = new MapWorkflow().getName();
Map<String, WorkflowTemplate> workflows = loadWorkflows();
Map<String, RunnableTask> tasks = loadTasks();
WorkflowTemplate workflow = workflows.get(workflowName);
Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), ImmutableMap.of());
// 3 = 1 + 2, 7 = 3 + 4
Literal i3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3)));
Literal i7 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(7)));
assertEquals(ImmutableMap.of("map", Literal.ofMap(ImmutableMap.of("e", i3, "f", i7))), outputs);
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class LocalEngineTest method testFibonacci.
@Test
void testFibonacci() {
String workflowName = new FibonacciWorkflow().getName();
Literal fib0 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(0L)));
Literal fib1 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1L)));
Literal fib2 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1L)));
Literal fib3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(2L)));
Literal fib4 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3L)));
Literal fib5 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(5L)));
Map<String, WorkflowTemplate> workflows = loadWorkflows();
Map<String, RunnableTask> tasks = loadTasks();
TestingListener listener = new TestingListener();
Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflows.get(workflowName), tasks, emptyMap(), ImmutableMap.of("fib0", fib0, "fib1", fib1), listener);
assertEquals(ImmutableMap.of("fib4", fib4, "fib5", fib5), outputs);
assertEquals(ImmutableList.<List<Object>>builder().add(ofPending("fib-2")).add(ofPending("fib-3")).add(ofPending("fib-4")).add(ofPending("fib-5")).add(ofStarting("fib-2", ImmutableMap.of("a", fib0, "b", fib1))).add(ofCompleted("fib-2", ImmutableMap.of("a", fib0, "b", fib1), ImmutableMap.of("c", fib2))).add(ofStarting("fib-3", ImmutableMap.of("a", fib1, "b", fib2))).add(ofCompleted("fib-3", ImmutableMap.of("a", fib1, "b", fib2), ImmutableMap.of("c", fib3))).add(ofStarting("fib-4", ImmutableMap.of("a", fib2, "b", fib3))).add(ofCompleted("fib-4", ImmutableMap.of("a", fib2, "b", fib3), ImmutableMap.of("c", fib4))).add(ofStarting("fib-5", ImmutableMap.of("a", fib3, "b", fib4))).add(ofCompleted("fib-5", ImmutableMap.of("a", fib3, "b", fib4), ImmutableMap.of("c", fib5))).build(), listener.actions);
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class LocalEngineTest method testBindingCollection.
@Test
public void testBindingCollection() {
String workflowName = new ListWorkflow().getName();
Map<String, WorkflowTemplate> workflows = loadWorkflows();
Map<String, RunnableTask> tasks = loadTasks();
WorkflowTemplate workflow = workflows.get(workflowName);
Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), ImmutableMap.of());
// 3 = 1 + 2, 7 = 3 + 4
Literal i3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3)));
Literal i7 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(7)));
assertEquals(ImmutableMap.of("list", Literal.ofCollection(ImmutableList.of(i3, i7))), outputs);
}
Aggregations