Search in sources :

Example 6 with Literal

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);
}
Also used : TaskNode(org.flyte.api.v1.TaskNode) SdkRunnableTask(org.flyte.flytekit.SdkRunnableTask) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Collectors.toMap(java.util.stream.Collectors.toMap) WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) SdkType(org.flyte.flytekit.SdkType) Duration(java.time.Duration) Map(java.util.Map) Preconditions.checkArgument(org.flyte.flytekit.testing.Preconditions.checkArgument) Collections.emptyMap(java.util.Collections.emptyMap) Node(org.flyte.api.v1.Node) LiteralType(org.flyte.api.v1.LiteralType) Literal(org.flyte.api.v1.Literal) ServiceLoader(java.util.ServiceLoader) Variable(org.flyte.api.v1.Variable) Instant(java.time.Instant) SdkWorkflow(org.flyte.flytekit.SdkWorkflow) Var(com.google.errorprone.annotations.Var) List(java.util.List) SdkRemoteTask(org.flyte.flytekit.SdkRemoteTask) AutoValue(com.google.auto.value.AutoValue) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) LocalEngine(org.flyte.localengine.LocalEngine) TaskNode(org.flyte.api.v1.TaskNode) WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) TaskNode(org.flyte.api.v1.TaskNode) Node(org.flyte.api.v1.Node) Literal(org.flyte.api.v1.Literal) LiteralType(org.flyte.api.v1.LiteralType) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 7 with Literal

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);
    }
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) LiteralType(org.flyte.api.v1.LiteralType) DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) Literal(org.flyte.api.v1.Literal) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Variable(org.flyte.api.v1.Variable) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) SdkType(org.flyte.flytekit.SdkType) Map(java.util.Map) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) LiteralType(org.flyte.api.v1.LiteralType) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Map(java.util.Map) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 8 with Literal

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() + "]");
}
Also used : Literal(org.flyte.api.v1.Literal) Map(java.util.Map)

Example 9 with Literal

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"))));
}
Also used : Blob(org.flyte.api.v1.Blob) HashMap(java.util.HashMap) Instant(java.time.Instant) Literal(org.flyte.api.v1.Literal) Duration(java.time.Duration) Test(org.junit.jupiter.api.Test)

Example 10 with Literal

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));
}
Also used : Blob(org.flyte.api.v1.Blob) HashMap(java.util.HashMap) Literal(org.flyte.api.v1.Literal) Test(org.junit.jupiter.api.Test)

Aggregations

Literal (org.flyte.api.v1.Literal)28 Test (org.junit.jupiter.api.Test)16 HashMap (java.util.HashMap)9 Instant (java.time.Instant)7 WorkflowTemplate (org.flyte.api.v1.WorkflowTemplate)7 LiteralType (org.flyte.api.v1.LiteralType)6 RunnableTask (org.flyte.api.v1.RunnableTask)6 Map (java.util.Map)5 Duration (java.time.Duration)4 List (java.util.List)4 Blob (org.flyte.api.v1.Blob)4 Variable (org.flyte.api.v1.Variable)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Literals (flyteidl.core.Literals)3 Struct (org.flyte.api.v1.Struct)3 Var (com.google.errorprone.annotations.Var)2 UncheckedIOException (java.io.UncheckedIOException)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Collections.emptyMap (java.util.Collections.emptyMap)2