Search in sources :

Example 1 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class ProtoUtilTest method createLiteralsArguments.

static Stream<Arguments> createLiteralsArguments() {
    Literal apiLiteral = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(123)));
    Literals.Literal protoLiteral = Literals.Literal.newBuilder().setScalar(Literals.Scalar.newBuilder().setPrimitive(Literals.Primitive.newBuilder().setInteger(123).build()).build()).build();
    com.google.protobuf.Struct protoStruct = com.google.protobuf.Struct.newBuilder().putFields("stringValue", Value.newBuilder().setStringValue("string").build()).putFields("boolValue", Value.newBuilder().setBoolValue(true).build()).putFields("numberValue", Value.newBuilder().setNumberValue(42.0).build()).putFields("listValue", Value.newBuilder().setListValue(ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(1.0).build()).addValues(Value.newBuilder().setNumberValue(2.0).build()).addValues(Value.newBuilder().setNumberValue(3.0).build()).build()).build()).putFields("nullValue", Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()).putFields("structValue", Value.newBuilder().setStructValue(com.google.protobuf.Struct.newBuilder().putFields("stringValue", Value.newBuilder().setStringValue("string").build()).build()).build()).build();
    Struct struct = Struct.of(ImmutableMap.<String, Struct.Value>builder().put("stringValue", Struct.Value.ofStringValue("string")).put("boolValue", Struct.Value.ofBoolValue(true)).put("numberValue", Struct.Value.ofNumberValue(42.0)).put("listValue", Struct.Value.ofListValue(ImmutableList.of(Struct.Value.ofNumberValue(1.0), Struct.Value.ofNumberValue(2.0), Struct.Value.ofNumberValue(3.0)))).put("nullValue", Struct.Value.ofNullValue()).put("structValue", Struct.Value.ofStructValue(Struct.of(ImmutableMap.of("stringValue", Struct.Value.ofStringValue("string"))))).build());
    return Stream.of(Arguments.of(protoLiteral, apiLiteral), Arguments.of(Literals.Literal.newBuilder().setCollection(Literals.LiteralCollection.newBuilder().addLiterals(protoLiteral).build()).build(), Literal.ofCollection(singletonList(apiLiteral))), Arguments.of(Literals.Literal.newBuilder().setMap(Literals.LiteralMap.newBuilder().putLiterals("name", protoLiteral).build()).build(), Literal.ofMap(Collections.singletonMap("name", apiLiteral))), Arguments.of(Literals.Literal.newBuilder().setScalar(Literals.Scalar.newBuilder().setGeneric(protoStruct).build()).build(), Literal.ofScalar(Scalar.ofGeneric(struct))));
}
Also used : Literal(org.flyte.api.v1.Literal) NullValue(com.google.protobuf.NullValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) Literals(flyteidl.core.Literals) Matchers.containsString(org.hamcrest.Matchers.containsString) Struct(org.flyte.api.v1.Struct)

Example 2 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class StringUtilTest method shouldSerializeLiteralMap.

@Test
void shouldSerializeLiteralMap() {
    Map<String, Literal> input = new HashMap<>();
    Literal integer = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1337L)));
    Literal map = Literal.ofMap(singletonMap("b", integer));
    Literal list = Literal.ofCollection(singletonList(integer));
    BlobType type = BlobType.builder().dimensionality(BlobType.BlobDimensionality.MULTIPART).format("csv").build();
    BlobMetadata metadata = BlobMetadata.builder().type(type).build();
    Blob blob = Blob.builder().metadata(metadata).uri("file://test").build();
    input.put("string", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofStringValue("string"))));
    input.put("integer", integer);
    input.put("float", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofFloatValue(2.0))));
    input.put("boolean", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofBooleanValue(true))));
    input.put("datetime", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofDatetime(Instant.ofEpochSecond(60L)))));
    input.put("duration", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofDuration(Duration.ofSeconds(61)))));
    input.put("list", list);
    input.put("map", map);
    input.put("listOfList", Literal.ofCollection(ImmutableList.of(list, integer)));
    input.put("mapOfMap", Literal.ofMap(ImmutableMap.of("a", map, "c", integer)));
    input.put("struct", Literal.ofScalar(Scalar.ofGeneric(Struct.of(ImmutableMap.<String, Struct.Value>builder().put("bool", Struct.Value.ofBoolValue(true)).put("string", Struct.Value.ofStringValue("string")).put("list", Struct.Value.ofListValue(ImmutableList.of(Struct.Value.ofNumberValue(1)))).put("number", Struct.Value.ofNumberValue(2)).put("null", Struct.Value.ofNullValue()).put("struct", Struct.Value.ofStructValue(Struct.of(ImmutableMap.of()))).build()))));
    input.put("blob", Literal.ofScalar(Scalar.ofBlob(blob)));
    Map<String, String> expected = new HashMap<>();
    expected.put("string", "string");
    expected.put("integer", "1337");
    expected.put("float", "2.0");
    expected.put("boolean", "true");
    expected.put("datetime", "1970-01-01T00:01:00Z");
    expected.put("duration", "PT1M1S");
    expected.put("list", "[1337]");
    expected.put("listOfList", "[[1337], 1337]");
    expected.put("map", "{b=1337}");
    expected.put("mapOfMap", "{a={b=1337}, c=1337}");
    expected.put("struct", "{bool=true, string=string, list=[1.0], number=2.0, null=null, struct={}}");
    expected.put("blob", "{uri=file://test, metadata={type={dimensionality=MULTIPART, format=csv}}}");
    Map<String, String> output = StringUtil.serializeLiteralMap(input);
    assertEquals(expected, output);
}
Also used : BlobType(org.flyte.api.v1.BlobType) Blob(org.flyte.api.v1.Blob) HashMap(java.util.HashMap) BlobMetadata(org.flyte.api.v1.BlobMetadata) Literal(org.flyte.api.v1.Literal) Struct(org.flyte.api.v1.Struct) Test(org.junit.jupiter.api.Test)

Example 3 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class SdkLaunchPlanTest method shouldAddDefaultInputs.

@Test
void shouldAddDefaultInputs() {
    Instant now = Instant.now();
    Duration duration = Duration.ofSeconds(123);
    Map<String, Literal> defaultInputs = new HashMap<>();
    defaultInputs.put("inputsFoo", Literals.ofInteger(456));
    defaultInputs.put("inputsBar", Literals.ofFloat(4.56));
    SdkLaunchPlan plan = SdkLaunchPlan.of(new TestWorkflow()).withDefaultInput("integer", 123L).withDefaultInput("float", 1.23).withDefaultInput("string", "123").withDefaultInput("boolean", true).withDefaultInput("datetime", now).withDefaultInput("duration", duration).withDefaultInput(TestSdkType.of("inputsFoo", LiteralTypes.INTEGER, "inputsBar", LiteralTypes.FLOAT), defaultInputs);
    assertThat(plan.defaultInputs(), allOf(hasEntry("integer", asParameter(Primitive.ofIntegerValue(123), SimpleType.INTEGER)), hasEntry("float", asParameter(Primitive.ofFloatValue(1.23), SimpleType.FLOAT)), hasEntry("string", asParameter(Primitive.ofStringValue("123"), SimpleType.STRING)), hasEntry("boolean", asParameter(Primitive.ofBooleanValue(true), SimpleType.BOOLEAN)), hasEntry("datetime", asParameter(Primitive.ofDatetime(now), SimpleType.DATETIME)), hasEntry("duration", asParameter(Primitive.ofDuration(duration), SimpleType.DURATION))));
}
Also used : HashMap(java.util.HashMap) Instant(java.time.Instant) Literal(org.flyte.api.v1.Literal) Duration(java.time.Duration) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class LocalEngineTest method testStructWorkflow.

@Test
public void testStructWorkflow() {
    String workflowName = new StructWorkflow().getName();
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    WorkflowTemplate workflow = workflows.get(workflowName);
    Literal inputStructLiteral = Literal.ofScalar(Scalar.ofGeneric(Struct.of(ImmutableMap.of("someKey1", Struct.Value.ofStringValue("some_value_1"), "someKey2", Struct.Value.ofBoolValue(true)))));
    Map<String, Literal> inputs = ImmutableMap.of("someString", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofStringValue("some_string_value"))), "someStruct", inputStructLiteral);
    Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), inputs);
    Literal expectedOutput = Literal.ofScalar(Scalar.ofGeneric(Struct.of(ImmutableMap.of("someKey1", Struct.Value.ofStringValue("some_value_1-output"), "someKey2", Struct.Value.ofBoolValue(true)))));
    assertEquals(expectedOutput, outputs.get("outputStructData"));
}
Also used : StructWorkflow(org.flyte.localengine.examples.StructWorkflow) WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) Literal(org.flyte.api.v1.Literal) RunnableTask(org.flyte.api.v1.RunnableTask) Test(org.junit.jupiter.api.Test)

Example 5 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class SdkTestingExecutor method withFixedInputs.

public <T> SdkTestingExecutor withFixedInputs(SdkType<T> type, T value) {
    Map<String, Variable> variableMap = type.getVariableMap();
    @Var Builder builder = toBuilder();
    for (Map.Entry<String, Literal> entry : type.toLiteralMap(value).entrySet()) {
        LiteralType literalType = variableMap.get(entry.getKey()).literalType();
        builder = builder.putFixedInput(entry.getKey(), entry.getValue(), literalType);
    }
    return builder.build();
}
Also used : Variable(org.flyte.api.v1.Variable) Var(com.google.errorprone.annotations.Var) 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)

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