-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for explicit field numbers and skipping unknown fields during…
… deserialization (#637) * Support for explicit field numbers and skipping unknown fields during deserialization * ScalaFix
- Loading branch information
Showing
7 changed files
with
249 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
zio-schema-protobuf/shared/src/main/scala/zio/schema/codec/FieldMappingCache.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package zio.schema.codec | ||
|
||
import scala.collection.mutable | ||
|
||
import zio.schema.Schema | ||
|
||
/** | ||
* A per-encooding/decoding cache for field mappings. No need for thread safety as a single encoding/decoding | ||
* is sequential. | ||
*/ | ||
private class FieldMappingCache { | ||
private val mapping: mutable.Map[Schema[_], FieldMapping] = mutable.Map.empty | ||
|
||
def get(schema: Schema.Record[_]): FieldMapping = | ||
mapping.getOrElseUpdate(schema, FieldMapping.fromSchema(schema)) | ||
} | ||
|
||
final case class FieldMapping(indexToFieldNumber: Map[Int, Int], fieldNumberToIndex: Map[Int, Int]) | ||
|
||
object FieldMapping { | ||
|
||
def fromSchema(schema: Schema.Record[_]): FieldMapping = { | ||
val indexToFieldNumber = schema.fields.zipWithIndex.map { | ||
case (field, index) => { | ||
val customFieldNumber = getFieldNumber(field) | ||
index -> customFieldNumber.getOrElse(index + 1) | ||
} | ||
}.toMap | ||
val fieldNumberToIndex = indexToFieldNumber.map(_.swap) | ||
FieldMapping(indexToFieldNumber, fieldNumberToIndex) | ||
} | ||
|
||
def getFieldNumber(field: Schema.Field[_, _]): Option[Int] = | ||
field.annotations.collectFirst { | ||
case fieldNumber(n) => n | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
zio-schema-protobuf/shared/src/main/scala/zio/schema/codec/fieldNumber.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package zio.schema.codec | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
final case class fieldNumber(n: Int) extends StaticAnnotation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.