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