Skip to content

Commit 6de2477

Browse files
committed
cosmetics and docs
1 parent 540eee0 commit 6de2477

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

commons-core/src/main/scala/com/avsystem/commons/serialization/GenCodec.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ object GenCodec extends RecursiveAutoCodecs with TupleGenCodecs {
142142
def createNonNullList[T](readFun: ListInput => T, writeFun: (ListOutput, T) => Any) =
143143
createList(readFun, writeFun, allowNull = false)
144144

145+
/**
146+
* Helper method to manually implement a `GenCodec` that writes an object. NOTE: in most cases the easiest way to
147+
* have a custom object codec is to manually implement `apply` and `unapply`/`unapplySeq` methods in companion object
148+
* of your type or use [[fromApplyUnapplyProvider]] if the type comes from a third party code and you can't
149+
* modify its companion object.
150+
*/
145151
def createObject[T](readFun: ObjectInput => T, writeFun: (ObjectOutput, T) => Any, allowNull: Boolean) =
146152
new ObjectCodec[T] {
147153
def nullable = allowNull
@@ -214,6 +220,11 @@ object GenCodec extends RecursiveAutoCodecs with TupleGenCodecs {
214220
}
215221
}
216222

223+
/**
224+
* Convenience base class for `GenCodec`s that serialize values as objects.
225+
* NOTE: if you need to implement a custom `GenCodec` that writes an object, the best way to do it is to have
226+
* manually implemented `apply` and `unapply` in companion object or by using [[GenCodec.fromApplyUnapplyProvider]].
227+
*/
217228
trait ObjectCodec[T] extends NullSafeCodec[T] {
218229
def readObject(input: ObjectInput): T
219230
def writeObject(output: ObjectOutput, value: T): Unit

commons-core/src/main/scala/com/avsystem/commons/serialization/macroCodecs.scala

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,31 @@ abstract class FlatSealedHierarchyCodec[T](
151151
}
152152
}
153153

154-
def read(): T = if (input.hasNext) {
155-
val fi = input.nextField()
156-
if (fi.fieldName == caseFieldName) readCase(fi)
157-
else if (!oooFields.tryReadField(fi)) {
158-
if (caseDependentFieldNames.contains(fi.fieldName)) {
159-
if (defaultCaseIdx != -1) {
160-
val defaultCaseName = caseNames(defaultCaseIdx)
161-
val wrappedInput = new DefaultCaseObjectInput(fi, input, defaultCaseName)
162-
readFlatCase(defaultCaseName, oooFields, wrappedInput, caseDeps(defaultCaseIdx))
154+
def read(): T =
155+
if (input.hasNext) {
156+
val fi = input.nextField()
157+
if (fi.fieldName == caseFieldName) readCase(fi)
158+
else if (!oooFields.tryReadField(fi)) {
159+
if (caseDependentFieldNames.contains(fi.fieldName)) {
160+
if (defaultCaseIdx != -1) {
161+
val defaultCaseName = caseNames(defaultCaseIdx)
162+
val wrappedInput = new DefaultCaseObjectInput(fi, input, defaultCaseName)
163+
readFlatCase(defaultCaseName, oooFields, wrappedInput, caseDeps(defaultCaseIdx))
164+
} else {
165+
missingCase(fi.fieldName)
166+
}
163167
} else {
164-
missingCase(fi.fieldName)
168+
fi.skip()
169+
read()
165170
}
166171
} else {
167-
fi.skip()
168172
read()
169173
}
174+
} else if (defaultCaseIdx != -1) {
175+
readFlatCase(caseNames(defaultCaseIdx), oooFields, input, caseDeps(defaultCaseIdx))
170176
} else {
171-
read()
177+
missingCase
172178
}
173-
} else if (defaultCaseIdx != -1) {
174-
readFlatCase(caseNames(defaultCaseIdx), oooFields, input, caseDeps(defaultCaseIdx))
175-
} else {
176-
missingCase
177-
}
178179

179180
input.peekField(caseFieldName) match {
180181
case Opt(fi) => readCase(fi)

0 commit comments

Comments
 (0)