Last active
January 15, 2019 13:34
Revisions
-
Naveen Gattu revised this gist
Mar 21, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ work with case classes with greater than 22 fields. The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: Note: ** Requires Play 2.3 and shapeless 2.0.0 import SWrites._ -
Naveen Gattu revised this gist
Mar 21, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ work with case classes with greater than 22 fields. The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: Note: ** Requires Play 2.3 and shapeless 2.1.0 import SWrites._ -
Naveen Gattu revised this gist
Sep 27, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ Additionally, you may get boilerplate free Format typeclasses: import SFormats.auto._ case class Foo(value: String) case class Bar(value1: Int, foo: Foo) -
Naveen Gattu revised this gist
Sep 27, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ Additionally, you may get boilerplate free Format typeclasses: import SFormats._ -
Naveen Gattu revised this gist
Sep 27, 2014 . 1 changed file with 63 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,8 +5,9 @@ work with case classes with greater than 22 fields. The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: Note: ** Requires Play 2.3 and shapeless 2.0.0 import SWrites._ import SReads._ @@ -21,7 +22,21 @@ implicit val reads: Reads[Bar] = SReads.auto.derive[Bar] Additionally, you may get boilerplate free json Format typeclasses: import SFormats._ case class Foo(value: String) case class Bar(value1: Int, foo: Foo) def someFunc(value: T)(implicit val format: Format[T]) = ... **/ @@ -31,39 +46,57 @@ import shapeless.{ `::` => :#:, _ } import poly._ implicit val writesInstance: LabelledProductTypeClass[Writes] = new LabelledProductTypeClass[Writes] { def emptyProduct: Writes[HNil] = Writes(_ => Json.obj()) def product[F, T <: HList](name: String, FHead: Writes[F], FTail: Writes[T]) = Writes[F :#: T] { case head :#: tail => val h = FHead.writes(head) val t = FTail.writes(tail) (h, t) match { case (JsNull, t: JsObject) => t case (h: JsValue, t: JsObject) => Json.obj(name -> h) ++ t case _ => Json.obj() } } def project[F, G](instance: => Writes[G], to: F => G, from: G => F) = Writes[F](f => instance.writes(to(f))) } object SWrites extends LabelledProductTypeClassCompanion[Writes] implicit val readsInstance: LabelledProductTypeClass[Reads] = new LabelledProductTypeClass[Reads] { def emptyProduct: Reads[HNil] = Reads(_ => JsSuccess(HNil)) def product[F, T <: HList](name: String, FHead: Reads[F], FTail: Reads[T]) = Reads[F :#: T] { case obj @ JsObject(fields) => for { head <- FHead.reads(obj \ name) tail <- FTail.reads(obj - name) } yield head :: tail case _ => JsError("Json object required") } def project[F, G](instance: => Reads[G], to: F => G, from: G => F) = Reads[F](instance.map(from).reads) } object SReads extends LabelledProductTypeClassCompanion[Reads] implicit val formatInstance: LabelledProductTypeClass[Format] = new LabelledProductTypeClass[Format] { def emptyProduct: Format[HNil] = Format( readsInstance.emptyProduct, writesInstance.emptyProduct ) def product[F, T <: HList](name: String, FHead: Format[F], FTail: Format[T]) = Format[F :#: T] ( readsInstance.product[F, T](name, FHead, FTail), writesInstance.product[F, T](name, FHead, FTail) ) def project[F, G](instance: => Format[G], to: F => G, from: G => F) = Format[F]( readsInstance.project(instance, to, from), writesInstance.project(instance, to, from) ) } object SFormats extends LabelledProductTypeClassCompanion[Format] -
Naveen Gattu revised this gist
Jul 30, 2014 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,10 @@ The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: import SWrites._ import SReads._ case class Foo(value: String) case class Bar(value1: Int, foo: Foo) //Didnt want to type out 23 fields, but you get the idea -
Naveen Gattu renamed this gist
Jul 30, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Naveen Gattu revised this gist
Jul 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ /** The Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt work with case classes with greater than 22 fields. The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: -
Naveen Gattu revised this gist
Jul 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ implicit val reads: Reads[Bar] = SReads.auto.derive[Bar] Note: ** Requires Play 2.3 and shapeless 2.0.0 **/ -
Naveen Gattu revised this gist
Jul 30, 2014 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,9 @@ /** The Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt work with case class with greater than 22 fields. The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: case class Foo(value: String) @@ -15,7 +16,10 @@ implicit val writes: Writes[Bar] = SWrites.auto.derive[Bar] implicit val reads: Reads[Bar] = SReads.auto.derive[Bar] Note: ** Requires Play 2.3 and shapeless **/ import play.api.libs._ import json._ -
Naveen Gattu renamed this gist
Jul 30, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Naveen Gattu renamed this gist
Jul 30, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Naveen Gattu created this gist
Jul 30, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ /*Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt work with case class with greater than 22 fields (scala 2.10 has removed this restriction). THe following code leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this limitation. Simply stick it in a common location in your code base, and use like so: case class Foo(value: String) case class Bar(value1: Int, foo: Foo) //Didnt want to type out 23 fields, but you get the idea implicit val writes: Writes[Foo] = SWrites.auto.derive[Foo] implicit val reads: Reads[Foo] = SReads.auto.derive[Foo] implicit val writes: Writes[Bar] = SWrites.auto.derive[Bar] implicit val reads: Reads[Bar] = SReads.auto.derive[Bar] */ import play.api.libs._ import json._ import shapeless.{ `::` => :#:, _ } import poly._ object SWrites extends LabelledProductTypeClassCompanion[Writes] { implicit val writesInstance = new LabelledProductTypeClass[Writes] { def emptyProduct: Writes[HNil] = Writes(_ => Json.obj()) def product[F, T <: HList](name: String, FHead: Writes[F], FTail: Writes[T]) = Writes[F :#: T] { case head :#: tail => val h = FHead.writes(head) val t = FTail.writes(tail) (h, t) match { case (JsNull, t: JsObject) => t case (h: JsValue, t: JsObject) => Json.obj(name -> h) ++ t case _ => Json.obj() } } def project[F, G](instance: => Writes[G], to: F => G, from: G => F) = Writes[F](f => instance.writes(to(f))) } } object SReads extends LabelledProductTypeClassCompanion[Reads] { implicit val readsInstance = new LabelledProductTypeClass[Reads] { def emptyProduct: Reads[HNil] = Reads(_ => JsSuccess(HNil)) def product[F, T <: HList](name: String, FHead: Reads[F], FTail: Reads[T]) = Reads[F :#: T] { case obj @ JsObject(fields) => for { head <- FHead.reads(obj \ name) tail <- FTail.reads(obj - name) } yield head :: tail case _ => JsError("Json object required") } def project[F, G](instance: => Reads[G], to: F => G, from: G => F) = Reads[F](instance.map(from).reads) } }