use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class DefaultSqlValueCoercerTest method coerce.
private static Optional<?> coerce(final DefaultSqlValueCoercer coercer, final SqlType from, final SqlType to, final Object value) {
// When:
final Optional<SqlType> coercedType = coercer.canCoerce(from, to);
final Result result = coercer.coerce(value, to);
// Then:
assertThat("canCoerce(" + from + "," + to + ")", coercedType, is(not(Optional.empty())));
assertThat("coerce(" + value + "," + to + ")", result, is(not(Result.failure())));
return result.value();
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class DefaultSqlValueCoercerTest method assertUnsupported.
private static void assertUnsupported(final DefaultSqlValueCoercer coercer, final SqlType from, final SqlType to) {
// Given:
final Object value = InstanceInstances.instanceFor(from, to);
// When:
final Optional<SqlType> coercedType = coercer.canCoerce(from, to);
final Result result = coercer.coerce(value, to);
// Then:
assertThat("canCoerce(" + from + "," + to + ")", coercedType, is(Optional.empty()));
assertThat("coerce(" + value + "," + to + ")", result, is(Result.failure()));
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToNestedMap.
@Test
public void shouldCoerceJsonObjectToNestedMap() {
// Given:
final SqlType mapType = SqlTypes.map(SqlTypes.STRING, SqlTypes.struct().field("F1", SqlTypes.BIGINT).field("F2", SqlTypes.STRING).build());
final JsonObject obj = new JsonObject().put("k1", new JsonObject().put("F1", 1).put("F2", "foo")).put("k2", new JsonObject().put("F1", 2)).put("k3", new JsonObject()).putNull("k4");
// When:
final Result result = coercer.coerce(obj, mapType);
// Then:
assertThat("", !result.failed());
final Map<?, ?> coerced = ((Optional<Map<?, ?>>) result.value()).get();
assertThat(((Struct) coerced.get("k1")).get("F1"), is(1L));
assertThat(((Struct) coerced.get("k1")).get("F2"), is("foo"));
assertThat(((Struct) coerced.get("k2")).get("F1"), is(2L));
assertThat(((Struct) coerced.get("k2")).get("F2"), is(nullValue()));
assertThat(((Struct) coerced.get("k3")).get("F1"), is(nullValue()));
assertThat(((Struct) coerced.get("k3")).get("F2"), is(nullValue()));
assertThat(coerced.get("k4"), is(nullValue()));
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldCoerceToJsonObjectWithNullValues.
@Test
public void shouldCoerceToJsonObjectWithNullValues() {
// Given:
final JsonObject jsonObject = new JsonObject().put("foo", (Long) null);
final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.decimal(2, 1)).build();
// When:
final Result result = coercer.coerce(jsonObject, structType);
// Then:
assertThat("", !result.failed());
final Optional<Struct> coerced = (Optional<Struct>) result.value();
assertThat(coerced.get().get("foo"), is(nullValue()));
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldNotCoerceToStructIfAnyFieldFailsToCoerce.
@Test
public void shouldNotCoerceToStructIfAnyFieldFailsToCoerce() {
// Given:
final Schema schema = SchemaBuilder.struct().field("foo", Schema.INT64_SCHEMA);
final Struct struct = new Struct(schema).put("foo", 2L);
final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.array(SqlTypes.INTEGER)).build();
// When:
final Result result = coercer.coerce(struct, structType);
// Then:
assertThat(result, is(Result.failure()));
}
Aggregations