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))));
}
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);
}
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))));
}
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"));
}
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();
}
Aggregations