-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add deserializer for SqlType (#4830)
- Loading branch information
Showing
6 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ksqldb-common/src/main/java/io/confluent/ksql/json/SqlTypeSchemaSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import io.confluent.ksql.schema.ksql.FormatOptions; | ||
import io.confluent.ksql.schema.ksql.types.SqlType; | ||
import java.io.IOException; | ||
|
||
public class SqlTypeSchemaSerializer extends JsonSerializer<SqlType> { | ||
|
||
@Override | ||
public void serialize( | ||
final SqlType value, | ||
final JsonGenerator gen, | ||
final SerializerProvider serializers | ||
) throws IOException { | ||
gen.writeString(value.toString(FormatOptions.none())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
ksqldb-parser/src/main/java/io/confluent/ksql/parser/json/SqlTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser.json; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import io.confluent.ksql.metastore.TypeRegistry; | ||
import io.confluent.ksql.schema.ksql.SqlTypeParser; | ||
import io.confluent.ksql.schema.ksql.types.SqlType; | ||
import java.io.IOException; | ||
|
||
public class SqlTypeDeserializer extends JsonDeserializer<SqlType> { | ||
|
||
public SqlTypeDeserializer() { | ||
} | ||
|
||
@Override | ||
public SqlType deserialize( | ||
final JsonParser p, | ||
final DeserializationContext ctxt | ||
) throws IOException { | ||
final String text = p.readValueAs(String.class); | ||
return SqlTypeParser.create(TypeRegistry.EMPTY).parse(text).getSqlType(); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
ksqldb-parser/src/test/java/io/confluent/ksql/parser/json/KsqlTypesSerdeModuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser.json; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.confluent.ksql.json.KsqlTypesSerializationModule; | ||
import io.confluent.ksql.schema.ksql.types.SqlArray; | ||
import io.confluent.ksql.schema.ksql.types.SqlMap; | ||
import io.confluent.ksql.schema.ksql.types.SqlStruct; | ||
import io.confluent.ksql.schema.ksql.types.SqlType; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Test; | ||
|
||
public class KsqlTypesSerdeModuleTest { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper() | ||
.registerModule(new KsqlTypesDeserializationModule(false)) | ||
.registerModule(new KsqlTypesSerializationModule()); | ||
|
||
@Test | ||
public void shouldSerDeSqlPrimitiveTypes() throws JsonProcessingException { | ||
// Given: | ||
final SqlType[] types = new SqlType[]{ | ||
SqlTypes.INTEGER, | ||
SqlTypes.BIGINT, | ||
SqlTypes.DOUBLE, | ||
SqlTypes.STRING | ||
}; | ||
|
||
for (final SqlType type : types) { | ||
// When: | ||
final SqlType out = MAPPER.readValue(MAPPER.writeValueAsString(type), SqlType.class); | ||
|
||
// Then | ||
assertThat(out, is(type)); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldSerDeSqlArrayTypes() throws JsonProcessingException { | ||
// Given: | ||
final SqlType[] types = new SqlType[]{ | ||
SqlTypes.INTEGER, | ||
SqlTypes.BIGINT, | ||
SqlTypes.DOUBLE, | ||
SqlTypes.STRING | ||
}; | ||
|
||
for (final SqlType type : types) { | ||
// When: | ||
final SqlType out = MAPPER.readValue(MAPPER.writeValueAsString(SqlArray.of(type)), SqlType.class); | ||
|
||
// Then | ||
assertThat(out, is(SqlArray.of(type))); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldSerDeSqlMapTypes() throws JsonProcessingException { | ||
// Given: | ||
final SqlType[] types = new SqlType[]{ | ||
SqlTypes.INTEGER, | ||
SqlTypes.BIGINT, | ||
SqlTypes.DOUBLE, | ||
SqlTypes.STRING | ||
}; | ||
|
||
for (final SqlType type : types) { | ||
// When: | ||
final SqlType out = MAPPER.readValue(MAPPER.writeValueAsString(SqlMap.of(type)), SqlType.class); | ||
|
||
// Then | ||
assertThat(out, is(SqlMap.of(type))); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldSerDeStructType() throws JsonProcessingException { | ||
// Given: | ||
SqlStruct struct = SqlStruct.builder().field("foo", SqlArray.of(SqlTypes.STRING)).build(); | ||
|
||
// When: | ||
final SqlType out = MAPPER.readValue(MAPPER.writeValueAsString(struct), SqlType.class); | ||
|
||
// Then: | ||
assertThat(out, is(struct)); | ||
} | ||
} |