-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@whenAbsent annotation for GenCodec and RPC
- Loading branch information
Showing
13 changed files
with
102 additions
and
20 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
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
28 changes: 28 additions & 0 deletions
28
commons-annotations/src/main/scala/com/avsystem/commons/serialization/whenAbsent.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,28 @@ | ||
package com.avsystem.commons | ||
package serialization | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
/** | ||
* An alternative way to provide default value for case class parameter used during deserialization with `GenCodec` | ||
* when its field is missing in data being deserialized. Normally, Scala-level default parameter values are picked | ||
* up, but you may want to use this annotation instead if you don't want to pollute your Scala classes with | ||
* unintended default parameter values (i.e. you want a default value *only* for deserialization). | ||
* | ||
* {{{ | ||
* case class HasDefault(@whenAbsent("default") str: String) | ||
* object HasDefault extends HasGenCodec[HasDefault] | ||
* }}} | ||
* | ||
* If a parameter has both Scala-level default value and is annotated with `@whenAbsent` then value from annotation | ||
* takes priority. You can use this to have different source-level default value and different | ||
* default value for deserialization. You can also leverage this to "remove" default value for deserialization: | ||
* | ||
* {{{ | ||
* case class HasNoDefault(@whenAbsent(throw new Exception) str: String = "default") | ||
* object HasDefault extends HasGenCodec[HasDefault] | ||
* }}} | ||
*/ | ||
class whenAbsent[+T](v: => T) extends StaticAnnotation { | ||
def value: T = v | ||
} |
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
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
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
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
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