Migration Guide
Vulcan 2.x is a rewrite of the library in pure Scala, without the official Apache Avro Java library, published for Scala 2.13 and 3.3 on the JVM, Scala.js and Scala Native. This page describes what has changed from 1.x, and what to use instead in 2.x. Codec schemas remain the same as in 1.x, except in some minor cases.
Dependencies
Vulcan 2.x is published under org.typelevel instead of com.github.fd4s, and uses the org.typelevel.vulcan package instead of vulcan. This means 1.x and 2.x can be used side by side.
libraryDependencies += "org.typelevel" %% "vulcan" % "2.0.0-RC2"
The vulcan-generic, vulcan-enumeratum and vulcan-refined modules keep their names.
Scala 2.12 is no longer supported.
API Changes
The following table shows what has changed from 1.x, and what to use instead in 2.x.
| 1.x | 2.x |
|---|---|
import vulcan._, vulcan.generic._, vulcan.refined._ |
import org.typelevel.vulcan._, org.typelevel.vulcan.generic._, org.typelevel.vulcan.refined._ |
enumeratum.VulcanEnum, enumeratum.values.IntVulcanEnum and the other value enums |
The same names in org.typelevel.vulcan.enumeratum and org.typelevel.vulcan.enumeratum.values |
Codec.Aux[Avro.Record, A], Avro.* |
Codec[A] |
codec.AvroType, codec.Repr |
AvroValue |
codec.encode(a), Codec.encode(a) |
codec.encoder, then encoder.encode(a) |
codec.decode(value, schema) |
codec.decoder(schema), then decoder.decode(value) |
Codec.decode[A](value) |
codec.schema.flatMap(codec.decoder), then decoder.decode(value) |
Codec.toBinary(a) |
codec.binaryEncoder, then encoder.encode(a); .toArray gives an Array[Byte] |
Codec.fromBinary[A](bytes, schema) |
codec.binaryDecoder(schema), then decoder.decode(ByteVector.view(bytes)) |
Codec.toJson(a) |
codec.jsonEncoder, then encoder.encode(a); returns circe Json, and .noSpaces gives a String |
Codec.fromJson[A](string, schema) |
codec.jsonDecoder(schema), then decoder.decode(json); parse string with io.circe.jawn.parse |
imapError(f)(g) |
narrow(f)(g) |
imapErrors(f)(g) |
exmap(f)(g) |
imapTry(f)(g) |
narrow(a => f(a).toEither.left.map(e => AvroException(e.toString, e)))(g) |
Codec.instance(schema, encode, decode) |
Codec.custom(schema, encoder, writerSchema => decoder) |
Codec.enum |
Codec.enumeration |
Codec.deriveEnum, Codec.deriveFixed, vulcan.AvroDoc, vulcan.AvroNamespace |
The same names in org.typelevel.vulcan.generic |
Codec.BooleanCodec, Codec.FloatCodec, Codec.LongCodec |
Codec.boolean, Codec.float, Codec.long |
| Shapeless coproduct codecs | Codec.union, or a derived sealed trait |
Alt#imap, Prism#imap |
Removed |
Codec.InstantMicros |
Instant, with the non-implicit Codec.instantMicros; see timestamps |
Codec[Array[Byte]] |
Codec[ByteVector]; for Array[Byte] fields, use Codec[ByteVector].imap(_.toArray)(ByteVector.view(_)) |
Codec.fixed, deriveFixed with Array[Byte] functions |
The same, with ByteVector functions |
org.apache.avro.Schema |
org.typelevel.vulcan.Schema; for Java tooling, use new org.apache.avro.Schema.Parser().parse(schema.toString) |
new Schema.Parser().parse(string) |
Schema.parse(string), or Schema.fromJson(json) for circe Json |
SchemaBuilder.builder().intType() |
Schema.Int; records are Schema.Record(name, fields), with withX modifiers |
schema.getFields, schema.getLogicalType |
case record: Schema.Record => record.fields, schema.logicalType |
schema.toString(true) |
schema.toJson.spaces2 |
order = Some(Schema.Field.Order.DESCENDING) |
order = Schema.Field.Order.Descending |
GenericRecord, Utf8, ByteBuffer, decoded from Any |
AvroValue.Record, AvroValue.String, AvroValue.Bytes |
| Untagged union values | AvroValue.Union(index, value) |
Props.one("a", 1).add("b", "c") |
JsonObject("a" -> 1.asJson, "b" -> "c".asJson), with io.circe.syntax._ |
@AvroProps(props), .prop |
@AvroProps(jsonObject), .props |
AvroError(message) |
AvroException(message) or AvroException(message, cause) |
AvroDecodingError |
AvroException |
error.throwable |
error, which is now the exception |
Eq[AvroError] |
No Eq instance; compare error.message, or define an Eq locally |
Behaviour Changes
- Logical types are checked at every level, so decoding a field with a logical type, like an
Instant, fails if the writer field doesn't have the same logical type. To decode values written without the logical type, use a codec without one, likeCodec[Long].imap(Instant.ofEpochMilli)(_.toEpochMilli). - Unions also match writer types to reader types by promotion, so an
intcan be read as a union'slong. - Error messages have changed. Errors have a subclass per case, like
AvroException.MissingField. - To protect against invalid input, decoding and schema parsing have limits, see untrusted input.
- On Scala 3,
deriveEnum,deriveFixedand enumeratum codecs for types nested in objects no longer have namespaces ending with$, likecom.example.Outer$, which are invalid in Avro. The namespaces now match Scala 2 and derived records. - Properties are
Json, rather than values encoded with a Codec. To keep schemas the same, convert values to the sameJsonas before. For example, circe encodesInstants andLocalDates as strings, while 1.x encoded them as numbers since the epoch.
New Features
- Recursive types, where record codecs refer to themselves, and
Codec.recursivefordefcodecs. - Codecs for additional logical types:
Codec.decimalFixed,Codec.bigDecimal,Codec.uuidFixedandCodec.duration, andCodec.instantNanos,Codec.localDateTimeMicrosandCodec.localDateTimeNanosfor timestamps. Codec#widenfor mappings where only encoding can fail,withLogicalTypeandwithTypeNamefor custom codecs, andCodec.fail.binaryEncoder(writerSchema), to encode values using a different writer schema, see schema resolution.canonicalFormandfingerprint64on Schemas.