Search in sources :

Example 1 with BlobType

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

the class ProtoUtilTest method shouldSerializeBlob.

@Test
void shouldSerializeBlob() {
    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://uri").build();
    Literals.Blob proto = ProtoUtil.serialize(blob);
    assertThat(proto, equalTo(Literals.Blob.newBuilder().setMetadata(Literals.BlobMetadata.newBuilder().setType(Types.BlobType.newBuilder().setDimensionality(Types.BlobType.BlobDimensionality.MULTIPART).setFormat("csv").build())).setUri("file://uri").build()));
}
Also used : BlobType(org.flyte.api.v1.BlobType) Blob(org.flyte.api.v1.Blob) BlobMetadata(org.flyte.api.v1.BlobMetadata) Literals(flyteidl.core.Literals) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with BlobType

use of org.flyte.api.v1.BlobType 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 BlobType

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

the class VariableMapVisitor method toLiteralType.

private LiteralType toLiteralType(JavaType javaType) {
    Class<?> type = javaType.getRawClass();
    if (isPrimitiveAssignableFrom(Long.class, type)) {
        return LiteralTypes.INTEGER;
    } else if (isPrimitiveAssignableFrom(Double.class, type)) {
        return LiteralTypes.FLOAT;
    } else if (String.class == type || javaType.isEnumType()) {
        return LiteralTypes.STRING;
    } else if (isPrimitiveAssignableFrom(Boolean.class, type)) {
        return LiteralTypes.BOOLEAN;
    } else if (Instant.class.isAssignableFrom(type)) {
        return LiteralTypes.DATETIME;
    } else if (Duration.class.isAssignableFrom(type)) {
        return LiteralTypes.DURATION;
    } else if (List.class.isAssignableFrom(type)) {
        JavaType elementType = javaType.getBindings().getBoundType(0);
        return LiteralType.ofCollectionType(toLiteralType(elementType));
    } else if (Map.class.isAssignableFrom(type)) {
        JavaType keyType = javaType.getBindings().getBoundType(0);
        JavaType valueType = javaType.getBindings().getBoundType(1);
        if (!keyType.getRawClass().equals(String.class)) {
            throw new UnsupportedOperationException("Only Map<String, ?> is supported, got [" + javaType.getGenericSignature() + "]");
        }
        return LiteralType.ofMapValueType(toLiteralType(valueType));
    } else if (Blob.class.isAssignableFrom(type)) {
        // TODO add annotation to specify dimensionality and format
        BlobType blobType = BlobType.builder().format("").dimensionality(BlobType.BlobDimensionality.SINGLE).build();
        return LiteralType.ofBlobType(blobType);
    }
    BeanDescription bean = getProvider().getConfig().introspect(javaType);
    List<BeanPropertyDefinition> properties = bean.findProperties();
    if (properties.isEmpty()) {
        // doesn't look like a bean, can be java.lang.Integer, or something else
        throw new UnsupportedOperationException(String.format("Unsupported type: [%s]", type.getName()));
    } else {
        return LiteralType.ofSimpleType(SimpleType.STRUCT);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BlobType(org.flyte.api.v1.BlobType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) Duration(java.time.Duration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 4 with BlobType

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

the class ProtoUtil method deserialize.

static Blob deserialize(Literals.Blob blob) {
    BlobType type = BlobType.builder().format(blob.getMetadata().getType().getFormat()).dimensionality(deserialize(blob.getMetadata().getType().getDimensionality())).build();
    BlobMetadata metadata = BlobMetadata.builder().type(type).build();
    return Blob.builder().uri(blob.getUri()).metadata(metadata).build();
}
Also used : BlobType(org.flyte.api.v1.BlobType) BlobMetadata(org.flyte.api.v1.BlobMetadata)

Example 5 with BlobType

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

the class LiteralTypesTest method testBlobToPrettyString.

@Test
public void testBlobToPrettyString() {
    BlobType blobType = BlobType.builder().dimensionality(BlobType.BlobDimensionality.SINGLE).format("application/csv").build();
    assertThat(LiteralTypes.toPrettyString(LiteralType.ofBlobType(blobType)), equalTo("BLOB"));
}
Also used : BlobType(org.flyte.api.v1.BlobType) Test(org.junit.jupiter.api.Test)

Aggregations

BlobType (org.flyte.api.v1.BlobType)5 BlobMetadata (org.flyte.api.v1.BlobMetadata)3 Test (org.junit.jupiter.api.Test)3 HashMap (java.util.HashMap)2 Blob (org.flyte.api.v1.Blob)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1 Literals (flyteidl.core.Literals)1 Duration (java.time.Duration)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Literal (org.flyte.api.v1.Literal)1 Struct (org.flyte.api.v1.Struct)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1