use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToStruct.
@Test
public void shouldCoerceJsonObjectToStruct() {
// Given:
final SqlType structType = SqlTypes.struct().field("FOO", SqlTypes.BIGINT).field("NULL", SqlTypes.BIGINT).build();
// When:
final Result result = coercer.coerce(new JsonObject().put("FOO", 12), structType);
// Then:
assertThat("", !result.failed());
final Struct coerced = ((Optional<Struct>) result.value()).get();
assertThat(coerced.get("FOO"), is(12L));
assertThat(coerced.get("NULL"), is(nullValue()));
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToNestedStruct.
@Test
public void shouldCoerceJsonObjectToNestedStruct() {
// Given:
final SqlType structType = SqlTypes.struct().field("F1", SqlTypes.struct().field("G1", SqlTypes.BIGINT).field("NULL", SqlTypes.BIGINT).build()).field("F2", SqlTypes.array(SqlTypes.STRING)).field("F3", SqlTypes.map(SqlTypes.STRING, SqlTypes.STRING)).build();
final JsonObject obj = new JsonObject().put("F1", new JsonObject().put("G1", 12)).put("F2", new JsonArray().add("v1").add("v2")).put("F3", new JsonObject().put("k1", "v1"));
// When:
final Result result = coercer.coerce(obj, structType);
// Then:
assertThat("", !result.failed());
final Struct coerced = ((Optional<Struct>) result.value()).get();
final Struct innerStruct = (Struct) coerced.get("F1");
assertThat(innerStruct.get("G1"), is(12L));
assertThat(innerStruct.get("NULL"), is(nullValue()));
final List<?> innerList = (List<?>) coerced.get("F2");
assertThat(innerList, is(ImmutableList.of("v1", "v2")));
final Map<?, ?> innerMap = (Map<?, ?>) coerced.get("F3");
assertThat(innerMap, is(ImmutableMap.of("k1", "v1")));
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldCoerceJsonStructToNestedDecimal.
@Test
public void shouldCoerceJsonStructToNestedDecimal() {
// Given:
final SqlType structType = SqlTypes.struct().field("INT", SqlTypes.decimal(2, 1)).field("LONG", SqlTypes.decimal(2, 1)).field("STRING", SqlTypes.decimal(2, 1)).field("DOUBLE", SqlTypes.decimal(2, 1)).build();
final JsonObject obj = new JsonObject().put("INT", 1).put("LONG", 1L);
obj.put("STRING", "1.1").put("DOUBLE", 1.2);
// When:
final Result result = coercer.coerce(obj, structType);
// Then:
assertThat("", !result.failed());
final Struct coerced = ((Optional<Struct>) result.value()).get();
assertThat(coerced.get("INT"), is(new BigDecimal("1.0")));
assertThat(coerced.get("LONG"), is(new BigDecimal("1.0")));
assertThat(coerced.get("STRING"), is(new BigDecimal("1.1")));
assertThat(coerced.get("DOUBLE"), is(new BigDecimal("1.2")));
}
use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.
the class ApiSqlValueCoercerTest method shouldCoerceToStruct.
@Test
public void shouldCoerceToStruct() {
// Given:
final JsonObject struct = new JsonObject().put("foo", 2L);
final SqlType structType = SqlTypes.struct().field("FOO", SqlTypes.decimal(2, 1)).build();
// When:
final Result result = coercer.coerce(struct, structType);
// Then:
assertThat("", !result.failed());
final Optional<Struct> coerced = (Optional<Struct>) result.value();
assertThat(coerced.get().get("FOO"), is(new BigDecimal("2.0")));
}
Aggregations