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

Simplify code for Record codec #330

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 50 additions & 63 deletions modules/core/src/main/scala/vulcan/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -911,64 +911,56 @@ object Codec extends CodecCompanionCompat {
free.analyze {
new (Field[A, *] ~> λ[a => Either[AvroError, Chain[Schema.Field]]]) {
def apply[B](field: Field[A, B]) =
field.codec.schema.flatMap { schema =>
field.props.toChain
.flatMap { props =>
field.default
.traverse(field.codec.encode(_))
.map { default =>
Chain.one {
val schemaField =
new Schema.Field(
field.name,
schema,
field.doc.orNull,
adaptForSchema {
default.map {
case null => Schema.Field.NULL_DEFAULT_VALUE
case other => other
}.orNull
},
field.order.getOrElse(Schema.Field.Order.ASCENDING)
)

field.aliases.foreach(schemaField.addAlias)

props.foldLeft(()) {
case ((), (name, value)) =>
schemaField.addProp(name, value)
}

schemaField
}
}
}
(
field.codec.schema,
field.props.toChain,
field.default.traverse(field.codec.encode(_))
).mapN { (schema, props, default) =>
val schemaField =
new Schema.Field(
field.name,
schema,
field.doc.orNull,
default.map {
case null => Schema.Field.NULL_DEFAULT_VALUE
case other => adaptForSchema(other)
}.orNull,
field.order.getOrElse(Schema.Field.Order.ASCENDING)
)

field.aliases.foreach(schemaField.addAlias)

props.foldLeft(()) {
case ((), (name, value)) =>
schemaField.addProp(name, value)
}

Chain.one(schemaField)
}
}
}

fields.flatMap { fields =>
props.toChain.map { props =>
val record =
Schema.createRecord(
name,
doc.orNull,
namespace,
false,
fields.toList.asJava
)

aliases.foreach(record.addAlias)

props.foldLeft(()) {
case ((), (name, value)) =>
record.addProp(name, value)
}
(fields, props.toChain).mapN { (fields, props) =>
val record =
Schema.createRecord(
name,
doc.orNull,
namespace,
false,
fields.toList.asJava
)

aliases.foreach(record.addAlias)

record
props.foldLeft(()) {
case ((), (name, value)) =>
record.addProp(name, value)
}

record
}
}

Codec
.instanceForTypes[GenericRecord, A](
"IndexedRecord",
Expand All @@ -995,22 +987,17 @@ object Codec extends CodecCompanionCompat {
}
}, {
case (record: IndexedRecord, _) =>
val recordSchema = record.getSchema()
val recordFields = recordSchema.getFields()

free.foldMap {
new (Field[A, *] ~> Either[AvroError, *]) {
def apply[B](field: Field[A, B]) = {
val schemaField = recordSchema.getField(field.name)
if (schemaField != null) {
val value = record.get(recordFields.indexOf(schemaField))
field.codec.decode(value, schemaField.schema())
} else {
field.default.toRight {
AvroError.decodeMissingRecordField(field.name)
}
def apply[B](field: Field[A, B]): Either[AvroError, B] =
record.getSchema.getField(field.name) match {
case null =>
field.default.toRight {
AvroError.decodeMissingRecordField(field.name)
}
case schemaField =>
field.codec.decode(record.get(schemaField.pos), schemaField.schema)
}
}
}
}
}
Expand Down