use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class SdkTestingExecutor method execute.
public Result execute() {
TestingSdkWorkflowBuilder builder = new TestingSdkWorkflowBuilder(fixedInputMap(), fixedInputTypeMap());
workflow().expand(builder);
WorkflowTemplate workflowTemplate = builder.toIdlTemplate();
for (Node node : workflowTemplate.nodes()) {
TaskNode taskNode = node.taskNode();
if (taskNode != null) {
String taskName = taskNode.referenceId().name();
checkArgument(fixedTaskMap().containsKey(taskName), "Can't execute remote task [%s], " + "use SdkTestingExecutor#withTaskOutput or SdkTestingExecutor#withTask", taskName);
}
}
Map<String, Literal> outputLiteralMap = LocalEngine.compileAndExecute(workflowTemplate, unmodifiableMap(fixedTaskMap()), emptyMap(), fixedInputMap());
Map<String, LiteralType> outputLiteralTypeMap = workflowTemplate.interface_().outputs().entrySet().stream().collect(toMap(Map.Entry::getKey, x -> x.getValue().literalType()));
return Result.create(outputLiteralMap, outputLiteralTypeMap);
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class JacksonSdkType method toLiteralMap.
@Override
public Map<String, Literal> toLiteralMap(T value) {
try {
JsonNode tree = OBJECT_MAPPER.valueToTree(value);
Map<String, LiteralType> literalTypeMap = getVariableMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, x -> x.getValue().literalType()));
// The previous trick with JavaType and withValueHandler did't work because
// Jackson caches serializers, without considering valueHandler as significant part
// of the caching key.
JsonParser tokens = OBJECT_MAPPER.treeAsTokens(tree);
tokens.nextToken();
LiteralMapDeserializer deserializer = new LiteralMapDeserializer(literalTypeMap);
// this is how OBJECT_MAPPER creates deserialization context, otherwise, nested deserializers
// don't work
DefaultDeserializationContext cctx = ((DefaultDeserializationContext) OBJECT_MAPPER.getDeserializationContext()).createInstance(OBJECT_MAPPER.getDeserializationConfig(), tokens, OBJECT_MAPPER.getInjectableValues());
JacksonLiteralMap jacksonLiteralMap = deserializer.deserialize(tokens, cctx);
return jacksonLiteralMap.getLiteralMap();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class LiteralSerializer method serialize.
@Override
public void serialize(Literal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
switch(value.kind()) {
case SCALAR:
serialize(value.scalar(), gen, serializers);
return;
case MAP:
gen.writeStartObject();
for (Map.Entry<String, Literal> entry : value.map().entrySet()) {
gen.writeFieldName(entry.getKey());
serialize(entry.getValue(), gen, serializers);
}
gen.writeEndObject();
return;
case COLLECTION:
gen.writeStartArray();
for (Literal element : value.collection()) {
serialize(element, gen, serializers);
}
gen.writeEndArray();
return;
}
throw new AssertionError("Unexpected Literal.Kind: [" + value.kind() + "]");
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class JacksonSdkTypeTest method testFromLiteralMap.
@Test
void testFromLiteralMap() {
Instant datetime = Instant.ofEpochSecond(12, 34);
Duration duration = Duration.ofSeconds(56, 78);
Blob blob = Blob.builder().metadata(BlobMetadata.builder().type(BLOB_TYPE).build()).uri("file://test").build();
Map<String, Literal> literalMap = new HashMap<>();
literalMap.put("i", literalOf(Primitive.ofIntegerValue(123L)));
literalMap.put("f", literalOf(Primitive.ofFloatValue(123.0)));
literalMap.put("s", literalOf(Primitive.ofStringValue("123")));
literalMap.put("b", literalOf(Primitive.ofBooleanValue(true)));
literalMap.put("t", literalOf(Primitive.ofDatetime(datetime)));
literalMap.put("d", literalOf(Primitive.ofDuration(duration)));
literalMap.put("blob", literalOf(blob));
literalMap.put("l", Literal.ofCollection(singletonList(literalOf(Primitive.ofStringValue("123")))));
literalMap.put("m", Literal.ofMap(singletonMap("marco", literalOf(Primitive.ofStringValue("polo")))));
AutoValueInput input = JacksonSdkType.of(AutoValueInput.class).fromLiteralMap(literalMap);
assertThat(input, equalTo(AutoValueInput.create(/* i= */
123L, /* f= */
123.0, /* s= */
"123", /* b= */
true, /* t= */
datetime, /* d= */
duration, /* blob= */
blob, /* l= */
singletonList("123"), /* m= */
singletonMap("marco", "polo"))));
}
use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.
the class JacksonSdkTypeTest method testToLiteralMap.
@Test
void testToLiteralMap() {
Blob blob = Blob.builder().metadata(BlobMetadata.builder().type(BLOB_TYPE).build()).uri("file://test").build();
Map<String, Literal> literalMap = JacksonSdkType.of(AutoValueInput.class).toLiteralMap(AutoValueInput.create(/* i= */
42L, /* f= */
42.0d, /* s= */
"42", /* b= */
false, /* t= */
Instant.ofEpochSecond(42, 1), /* d= */
Duration.ofSeconds(1, 42), /* blob= */
blob, /* l= */
singletonList("foo"), /* m= */
singletonMap("marco", "polo")));
Map<String, Literal> expected = new HashMap<>();
expected.put("i", literalOf(Primitive.ofIntegerValue(42L)));
expected.put("f", literalOf(Primitive.ofFloatValue(42.0d)));
expected.put("s", literalOf(Primitive.ofStringValue("42")));
expected.put("b", literalOf(Primitive.ofBooleanValue(false)));
expected.put("t", literalOf(Primitive.ofDatetime(Instant.ofEpochSecond(42, 1))));
expected.put("d", literalOf(Primitive.ofDuration(Duration.ofSeconds(1, 42))));
expected.put("l", Literal.ofCollection(singletonList(literalOf(Primitive.ofStringValue("foo")))));
expected.put("m", Literal.ofMap(singletonMap("marco", literalOf(Primitive.ofStringValue("polo")))));
expected.put("blob", literalOf(blob));
assertThat(literalMap, equalTo(expected));
}
Aggregations