Skip to content

Commit 320c3f8

Browse files
committed
feat: add support for avro4s in schema generation
1 parent 4103439 commit 320c3f8

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

kafka/check-local-schemas.sh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,36 @@ get_md5sum() {
3737
md5sum "${file}" | awk '{ print $1}'
3838
}
3939

40-
41-
find_schema_class() {
40+
find_schema_class_file() {
4241
# The schema class heuristic is a bit hacky for now, we just expect
4342
# the filename containing the schema code to end with Schema or is named InputModel
4443
# We might want to improve this in the future
45-
schema_class_file="$(find src -name "*Schema.scala" -o -name "InputModel.scala" | head -n 1)"
44+
find src -name "*Schema.scala" -o -name "InputModel.scala" | head -n 1 || return 0
45+
}
46+
47+
find_schema_class() {
48+
local schema_class_file="$1"
4649
schema_class_name="$(basename "${schema_class_file}" .scala)"
4750
schema_package="$(awk ' $1 == "package" { print $2 }' "${schema_class_file}")"
4851

4952
echo "${schema_package}.${schema_class_name}"
5053
}
5154

55+
find_avro_library() {
56+
local schema_class_file="$1"
57+
58+
if grep -q "import com.sksamuel.avro4s" "${schema_class_file}"; then
59+
echo "avro4s"
60+
elif grep -q "import vulcan" "${schema_class_file}"; then
61+
echo "vulcan"
62+
else
63+
error "Could not find any avro library import in ${schema_class_file}"
64+
fi
65+
66+
}
67+
5268
generate_schema_generator_code() {
53-
local schema_class="$1"
69+
local schema_class="$1" schema_library="$2"
5470

5571
schema_class_name="${schema_class##*.}"
5672
schema_package="${schema_class%.*}"
@@ -60,7 +76,7 @@ generate_schema_generator_code() {
6076
sed \
6177
-e "s/__SCHEMA_CLASS_NAME__/${schema_class_name}/g" \
6278
-e "s/__SCHEMA_PACKAGE__/${schema_package}/g" \
63-
"${SCRIPT_DIR}/generators/VulcanSchemaGenerator.tmpl.scala"
79+
"${SCRIPT_DIR}/generators/${schema_library^}SchemaGenerator.tmpl.scala"
6480
}
6581

6682
run_schema_generator_code() {
@@ -96,7 +112,13 @@ generator_code_file="${generator_source_folder}/SchemaGenerator.scala"
96112

97113
[[ ! -f "${target_schema_file}" ]] || checksum_before="$(get_md5sum "${target_schema_file}")"
98114

99-
generate_schema_generator_code "$(find_schema_class)" > "${generator_code_file}"
115+
schema_class_file="$(find_schema_class_file)"
116+
[[ -n "${schema_class_file}" ]] || error "Could not find any schema class file"
117+
118+
schema_class="$(find_schema_class "${schema_class_file}")"
119+
schema_library="$(find_avro_library "${schema_class_file}")"
120+
121+
generate_schema_generator_code "${schema_class}" "${schema_library}" > "${generator_code_file}"
100122
run_schema_generator_code "${generator_code_file}" "${target_schema_file}"
101123

102124
if ! is_git_tracked "${target_schema_file}"; then
@@ -105,5 +127,5 @@ fi
105127

106128
checksum_after="$(get_md5sum "${target_schema_file}")"
107129
if [[ "${checksum_after}" != "${checksum_before:-}" ]]; then
108-
error "Schema file \"${target_schema_file}\" was not consistent with code. Please commit the updated version."
130+
error "Schema file \"${target_schema_file}\" was missing or not consistent with code. Please commit the updated version."
109131
fi
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package kp_pre_commit_hooks
2+
3+
import org.apache.avro.Schema
4+
import __SCHEMA_PACKAGE__.__SCHEMA_CLASS_NAME__
5+
6+
import com.sksamuel.avro4s.AvroSchema
7+
8+
def schemaToString(schema: Schema): String = ujson.write(ujson.read(schema.toString()), indent = 4)
9+
10+
def writeSchema(schema: Schema, schemaFilename: String) = {
11+
Console.println(s"Writing ${schema.getName} schema to ${schemaFilename}")
12+
val schemaFilePath = os.Path(schemaFilename, os.pwd)
13+
os.write.over(schemaFilePath, schemaToString(schema), createFolders = true)
14+
}
15+
16+
@main def generateSchemaFile(schemaFilename: String) = {
17+
18+
val generatedSchema = AvroSchema[__SCHEMA_CLASS_NAME__]
19+
writeSchema(generatedSchema, schemaFilename)
20+
}

0 commit comments

Comments
 (0)