Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFormat does not use Custom string Encoder #801

Open
linkleonard opened this issue Oct 11, 2023 · 0 comments
Open

DataFormat does not use Custom string Encoder #801

linkleonard opened this issue Oct 11, 2023 · 0 comments

Comments

@linkleonard
Copy link

Hello,

I'm defining a encoding for java.util.Currency where the underlying format is just a string. However, when I attempt to use it with the DataFormat, it blows up with a failed cast operation. I've traced it down to this line here. Wondering if there are any issues changing this to call the encoder, for derived types like this?

// codec
implicit object CurrencyCodec extends Codec[Currency] {
  override def schemaFor: SchemaFor[Currency] = 
    SchemaFor[Currency](SchemaBuilder.builder.stringType())

  def encode(value: Currency): AnyRef =
    Encoder.StringEncoder.encode(value.getCurrencyCode)

  override def decode(value: Any): Currency =
    Currency.getInstance(Decoder.StringDecoder.decode(value))
}
// spec
val currency = Currency.getInstance("USD")
val byteStream = new ByteArrayOutputStream
val outputStream = AvroOutputStream.data(CurrencyCodec).to(byteStream).build()
try {
  outputStream.write(currency)
  outputStream.flush()
} finally {
  outputStream.close()
}

byteStream.toByteArray
org.apache.avro.file.DataFileWriter$AppendWriteException: java.lang.ClassCastException: class java.util.Currency cannot be cast to class java.lang.CharSequence (java.util.Currency and java.lang.CharSequence are in module java.base of loader 'bootstrap')
org.apache.avro.file.DataFileWriter$AppendWriteException: java.lang.ClassCastException: class java.util.Currency cannot be cast to class java.lang.CharSequence (java.util.Currency and java.lang.CharSequence are in module java.base of loader 'bootstrap')
	at org.apache.avro.file.DataFileWriter.append(DataFileWriter.java:317)
	at com.sksamuel.avro4s.AvroDataOutputStream.$anonfun$x$1$1(AvroDataOutputStream.scala:30)
	at com.sksamuel.avro4s.AvroDataOutputStream.$anonfun$x$1$1$adapted(AvroDataOutputStream.scala:30)
	at com.sksamuel.avro4s.AvroDataOutputStream.write(AvroDataOutputStream.scala:48)
	at CurrencyCodecSpec.$anonfun$new$2(CurrencyCodecSpec.scala:19)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant