diff --git a/.github/workflows/java_ci.yaml b/.github/workflows/java_ci.yaml index 16af3dc..d2f27b0 100644 --- a/.github/workflows/java_ci.yaml +++ b/.github/workflows/java_ci.yaml @@ -5,6 +5,8 @@ on: [push] jobs: build: runs-on: ubuntu-latest + env: + GRADLE_OPTS: '-Dfile.encoding=utf-8 -Dorg.gradle.daemon=false' steps: - uses: actions/checkout@v2 @@ -16,8 +18,17 @@ jobs: java-version: '14' - name: Validate Gradle wrapper uses: gradle/wrapper-validation-action@v1 + - name: Set up cache + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- - name: Build with Gradle - run: ./gradlew build + run: ./gradlew clean build - name: Cleanup Gradle Cache # Remove some files from the Gradle cache, so they aren't cached by GitHub Actions. # Restoring these files from a GitHub Actions cache might cause problems for future builds. diff --git a/README.md b/README.md index 0849d6c..adc1db5 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,36 @@ # json-avro-converter -[![Java CI](https://github.com/airbytehq/json-avro-converter/actions/workflows/java_ci.yaml/badge.svg)](https://github.com/airbytehq/json-avro-converter/actions/workflows/java_ci.yaml) +[![Java CI](https://github.com/airbytehq/json-avro-converter/actions/workflows/java_ci.yaml/badge.svg)](https://github.com/airbytehq/json-avro-converter/actions/workflows/java_ci.yaml) [![JitPack](https://jitpack.io/v/airbytehq/json-avro-converter.svg)](https://jitpack.io/#airbytehq/json-avro-converter) This is a Json to Avro object converter used by Airbyte. It is based on [json-avro-converter](https://github.com/allegro/json-avro-converter) from [Allegro Tech](https://github.com/allegro). For basic usage, see the README in the original repo. +## Install +This library is available on JitPack. Check [here](https://jitpack.io/#airbytehq/json-avro-converter) for details. + +1. Add the JitPack repository to the root `build.gradle`: + ```groovy + allprojects { + repositories { + maven { url 'https://jitpack.io' } + } + } + ``` +2. Add the dependency + ```groovy + dependencies { + implementation 'com.github.airbytehq:json-avro-converter:' + } + ``` + +## Publish +To publish a new version, tag the desired commit with a new version label. + +```bash +VERSION=1.0.0 +git tag -a "${VERSION}" -m "Version ${VERSION}" +git push origin "${VERSION}" +``` + ## Airbyte Specific Features ### Fluent Constructor diff --git a/converter/src/main/java/tech/allegro/schema/json2avro/converter/AvroTypeExceptions.java b/converter/src/main/java/tech/allegro/schema/json2avro/converter/AvroTypeExceptions.java index 195b880..a847a0c 100644 --- a/converter/src/main/java/tech/allegro/schema/json2avro/converter/AvroTypeExceptions.java +++ b/converter/src/main/java/tech/allegro/schema/json2avro/converter/AvroTypeExceptions.java @@ -1,39 +1,41 @@ package tech.allegro.schema.json2avro.converter; -import static java.util.stream.Collectors.*; - import java.util.Deque; - import org.apache.avro.AvroTypeException; class AvroTypeExceptions { - static AvroTypeException enumException(Deque fieldPath, String expectedSymbols) { + static AvroTypeException enumException(Deque fieldPath, String expectedSymbols, Object offendingValue) { return new AvroTypeException(new StringBuilder() .append("Field ") .append(PathsPrinter.print(fieldPath)) .append(" is expected to be of enum type and be one of ") .append(expectedSymbols) + .append(", but it is: ") + .append(offendingValue) .toString()); } - static AvroTypeException unionException(String fieldName, String expectedTypes, Deque offendingPath) { + static AvroTypeException unionException(String fieldName, String expectedTypes, Deque offendingPath, Object offendingValue) { return new AvroTypeException(new StringBuilder() .append("Could not evaluate union, field ") .append(fieldName) .append(" is expected to be one of these: ") .append(expectedTypes) - .append(". If this is a complex type, check if offending field: ") + .append(". If this is a complex type, check if offending field (path: ") .append(PathsPrinter.print(offendingPath)) - .append(" adheres to schema.") + .append(") adheres to schema: ") + .append(offendingValue) .toString()); } - static AvroTypeException typeException(Deque fieldPath, String expectedType) { + static AvroTypeException typeException(Deque fieldPath, String expectedType, Object offendingValue) { return new AvroTypeException(new StringBuilder() .append("Field ") .append(PathsPrinter.print(fieldPath)) .append(" is expected to be type: ") .append(expectedType) + .append(", but it is: ") + .append(offendingValue) .toString()); } } diff --git a/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java b/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java index dd406de..6e68cd4 100644 --- a/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java +++ b/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java @@ -289,7 +289,8 @@ private Object readUnion(Schema.Field field, Schema schema, Object value, Deque< throw unionException( field.name(), types.stream().map(Schema::getType).map(Object::toString).collect(joining(", ")), - path); + path, + value); } private Object ensureEnum(Schema schema, Object value, Deque path) { @@ -297,7 +298,7 @@ private Object ensureEnum(Schema schema, Object value, Deque path) { if (symbols.contains(value)) { return new GenericData.EnumSymbol(schema, value); } - throw enumException(path, symbols.stream().map(String::valueOf).collect(joining(", "))); + throw enumException(path, symbols.stream().map(String::valueOf).collect(joining(", ")), value); } private ByteBuffer bytesForString(String string) { @@ -315,7 +316,7 @@ public Object onValidType(Object value, Class type, Deque path, b if (silently) { return INCOMPATIBLE; } else { - throw typeException(path, type.getTypeName()); + throw typeException(path, type.getTypeName(), value); } } }