Search in sources :

Example 1 with Struct

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

the class JFlyteCustomTest method testSerializeCustomToStruct.

@Test
public void testSerializeCustomToStruct() {
    JFlyteCustom custom = JFlyteCustom.builder().artifacts(ImmutableList.of(Artifact.create("location0", "name0", 1337), Artifact.create("location1", "name1", 1337))).build();
    List<Struct.Value> expectedArtifacts = ImmutableList.of(Struct.Value.ofStructValue(Struct.of(ImmutableMap.of("name", Struct.Value.ofStringValue("name0"), "location", Struct.Value.ofStringValue("location0")))), Struct.Value.ofStructValue(Struct.of(ImmutableMap.of("name", Struct.Value.ofStringValue("name1"), "location", Struct.Value.ofStringValue("location1")))));
    Struct expected = Struct.of(ImmutableMap.of("jflyte", Struct.Value.ofStructValue(Struct.of(ImmutableMap.of("artifacts", Struct.Value.ofListValue(expectedArtifacts))))));
    assertThat(custom.serializeToStruct(), equalTo(expected));
}
Also used : Struct(org.flyte.api.v1.Struct) Test(org.junit.Test)

Example 2 with Struct

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

the class ProjectClosureTest method testMerge.

@Test
public void testMerge() {
    Struct source = Struct.of(ImmutableMap.of("a", Struct.Value.ofStringValue("a0"), "b", Struct.Value.ofStringValue("b0")));
    Struct target = Struct.of(ImmutableMap.of("b", Struct.Value.ofStringValue("b1"), "c", Struct.Value.ofStringValue("c1")));
    Struct expected = Struct.of(ImmutableMap.of("a", Struct.Value.ofStringValue("a0"), "b", Struct.Value.ofStringValue("b0"), "c", Struct.Value.ofStringValue("c1")));
    assertThat(expected.fields().size(), equalTo(3));
    assertThat(ProjectClosure.merge(source, target), equalTo(expected));
}
Also used : Struct(org.flyte.api.v1.Struct) Test(org.junit.jupiter.api.Test)

Example 3 with Struct

use of org.flyte.api.v1.Struct 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 4 with Struct

use of org.flyte.api.v1.Struct 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 5 with Struct

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

the class JFlyteCustom method deserializeArtifact.

private static Artifact deserializeArtifact(Struct.Value structValue) {
    Preconditions.checkArgument(structValue.kind() == Struct.Value.Kind.STRUCT_VALUE, "'structValue' expected to be STRUCT_VALUE, but got %s", structValue.kind());
    Struct struct = structValue.structValue();
    return Artifact.create(/*location=*/
    struct.fields().get("location").stringValue(), /*name=*/
    struct.fields().get("name").stringValue(), // we don't expose size yet, and we aren't sure if we want to
    0);
}
Also used : Struct(org.flyte.api.v1.Struct)

Aggregations

Struct (org.flyte.api.v1.Struct)10 Test (org.junit.jupiter.api.Test)4 Literal (org.flyte.api.v1.Literal)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TaskTemplate (org.flyte.api.v1.TaskTemplate)2 ListValue (com.google.protobuf.ListValue)1 NullValue (com.google.protobuf.NullValue)1 Value (com.google.protobuf.Value)1 Literals (flyteidl.core.Literals)1 LinkedHashMap (java.util.LinkedHashMap)1 BindingData (org.flyte.api.v1.BindingData)1 Blob (org.flyte.api.v1.Blob)1 BlobMetadata (org.flyte.api.v1.BlobMetadata)1 BlobType (org.flyte.api.v1.BlobType)1 ContainerError (org.flyte.api.v1.ContainerError)1 DynamicJobSpec (org.flyte.api.v1.DynamicJobSpec)1 DynamicWorkflowTask (org.flyte.api.v1.DynamicWorkflowTask)1 DynamicWorkflowTaskRegistrar (org.flyte.api.v1.DynamicWorkflowTaskRegistrar)1 LiteralType (org.flyte.api.v1.LiteralType)1