Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    raffaello mesquita de pieri
    @raffaello-mdp_gitlab
    How would you approach this @igmar ?
    Igmar Palsenberg
    @igmar
    There is a huge difference between running a cli from play, or write a cli using play
    The first one is just regular JVM, the second one isn't really possible.
    Or at least, makes no sense.
    raffaello mesquita de pieri
    @raffaello-mdp_gitlab
    You are right @igmar, I don't know how I didn't think about it! thanks
    N Banek
    @natonic77

    I'm to update a JSON formatter in my Play app to support multiple legacy formats, and would like to find a less redundant solution.

    Below is a simplistic example of what I am doing. I've changed the name of one of the case class arguments, and have built an OFormat to support reading JSON for the old and new format, and writing only in the new format.

    The problem is that this solution requires duplication of the parsing logic for all unchanged case class arguments (in my actual situation, there are more arguments, more than one legacy format, and more complex parsing operations).

    Is there a better way to do this to avoid the duplicated parsing logic?

    case class Simple(rank: String, desc: String, code: String)
    object Simple {
      private val readsOrig: Reads[Simple] = (
        (JsPath \ "title").read[String] and
        (JsPath \ "desc").read[String](minLength[String](5)) and
        (JsPath \ "code").read[String](minLength[String](3))
      )(Simple(_,_,_))
      private val readsCurr: Reads[Simple] = (
        (JsPath \ "rank").read[String] and
        (JsPath \ "desc").read[String](minLength[String](5)) and
        (JsPath \ "code").read[String](minLength[String](3))
      )(Simple(_,_,_))
    
      private implicit val format: OFormat[Simple] = OFormat[Simple](
        new Reads[Simple]{override def reads(js: JsValue): JsResult[Simple] = {
          readsCurr.reads(js) match {
            case s: JsSuccess[Simple] => s
            case _ => readsOrig.reads(js)
          }
        }},
        Json.writes[Simple] // write in the current supported format
      )
    Tim Moore
    @TimMoore
    @natonic77 you can use an or combinator:
      object Simple {
        implicit val format: OFormat[Simple] = OFormat[Simple](
          (((JsPath \ "rank").read[String] or (JsPath \ "title").read[String]) and
            (JsPath \ "desc").read[String](minLength[String](5)) and
            (JsPath \ "code").read[String](minLength[String](3))
            )(Simple(_, _, _)),
          Json.writes[Simple] // write in the current supported format
        )
      }
    N Banek
    @natonic77
    @TimMoore awesome - thanks! Not sure how I missed that on the combinators doc page
    vito-c
    @vito-c
    does anyone compile with -Ywarn-unused-import enabled? I just enabled it on our code base and the routes it's compiling a lot about the routes file
    N.S. Cutler
    @godenji
    yeah, iirc generated routes file still wreaks havoc with above flag + -Xfatal-warnings. Still on 2.6 here, maybe this was fixed in 2.7.x?
    Anyone know where default config values for application.conf are defined? Interested in seeing a list of all config options and their default values -- e.g. play.http.parser.maxDiskBuffer has a default value of 10MB, but I only discovered that reading through body parsers documentation. Remember seeing a central defaults config file somewhere but it was a couple of years ago.
    vito-c
    @vito-c
    yah won't work at all
    Igmar Palsenberg
    @igmar
    @godenji The defaults are in play's reference.conf
    And in the matching submodules (so akka-http has its own reference.conf, with a set of defaults)
    N.S. Cutler
    @godenji
    @igmar thanks
    N Banek
    @natonic77

    @TimMoore

    @natonic77 you can use an or combinator:

    ...

    This was a great solution for my basic case, but since "and" returns a FunctionalBuilder instead of a Reads, I can't do a more complex case where a group of changes belongs together (say I renamed desc and code to newDesc and newCode).

    object Simple {
         implicit val format: OFormat[Simple] = OFormat[Simple](
           (((JsPath \ "rank").read[String] or (JsPath \ "title").read[String]) and
             ((JsPath \ "desc").read[String](minLength[String](5)) and
             (JsPath \ "code").read[String](minLength[String](3)))
             or
             ((JsPath \ "newDesc").read[String](minLength[String](5)) and
             (JsPath \ "newCode").read[String](minLength[String](3)))
             )(Simple(_, _, _)),
           Json.writes[Simple] // write in the current supported format
         )
       }

    ...wouldn't work.

    The following approach would work, and I may have to use it, but it doesn't allow bundling the changes together (it would allow for "newDesc" coupled with "code" in a single JSON string for instance):

      object Simple {
        implicit val format: OFormat[Simple] = OFormat[Simple](
          (((JsPath \ "rank").read[String] or (JsPath \ "title").read[String]) and
            ((JsPath \ "desc").read[String](minLength[String](5)) or (JsPath \ "newDesc").read[String](minLength[String](5))) and
            ((JsPath \ "code").read[String](minLength[String](3)) or (JsPath \ "newCode").read[String](minLength[String](3)))
            )(Simple(_, _, _)),
          Json.writes[Simple] // write in the current supported format
        )
      }

    Any ideas? I feel like I might be asking for more flexibility than the combinators are designed to provide.

    Abhishek Srivastava
    @abhishes_twitter
    Can someone please help me? I am handling a case class which has more than 22 fields. So I use play json extensions. I also have json in snake case which I want to automatically handle to camel case. Problem is that the play json extensions don't seem to work very well with the JsonNaming.SnakeCase.
    {
      case class Foo(accountNumber: String)
      object Foo {
          implicit val config = JsonConfiguration(SnakeCase)
          import ai.x.play.json.Jsonx
          implicit lazy val jsonFormat = Jsonx.formatCaseClass[Foo]
      }
    }
    Json.parse("""{"account_number":"a"}""").validate[Foo]
    But this code fails with error
    JsError( List((JsPath(List(KeyPathNode("accountNumber"))), List(JsonValidationError(List("error.path.missing"), WrappedArray())))) )
    Tim Moore
    @TimMoore
    @natonic77 for more flexibility you can use JSON transformers to convert an old format to a new one: https://www.playframework.com/documentation/2.7.x/ScalaJsonTransformers
    I only discovered this yesterday, though, so I probably can't be more helpful than that :)
    N Banek
    @natonic77
    Thanks, @TimMoore. I hadn't looked at those before. Looks like it might be a help.
    Michael Wolfendale
    @wolfendale
    @natonic77
    final case class Simple(rank: String, code: String, desc: String)
    
    object Simple {
    
      import play.api.libs.json._
      import play.api.libs.functional.syntax._
    
      implicit val writes: OWrites[Simple] = (
        (__ \ "rank").write[String] and
        (__ \ "code").write[String] and
        (__ \ "desc").write[String]
      )(unlift(Simple.unapply))
    
      implicit val reads: Reads[Simple] = {
    
        val oldReads: Reads[Simple] = (
          (__ \ "rank").read[String] and
          (__ \ "code").read[String] and
          (__ \ "desc").read[String]
        )(Simple(_, _, _))
    
        val newReads = (
          (__ \ "rank").read[String] and
          (__ \ "newCode").read[String] and
          (__ \ "newDesc").read[String]
        )(Simple(_, _, _))
    
        newReads orElse oldReads
      }
    }
    There’s an implicit conversion to OFormat as long as you have a Reads and OWrites separating out their definitions can make things like this a bit cleaner and easier to represent.
    N Banek
    @natonic77
    Thanks, @wolfendale. I had pursued an option like this previously, and I agree it is a lot cleaner looking, but the duplicated .read calls in oldReads and newReads (where there was no change to the format) were what led me to look for a less redundant solution.
    nafg
    @nafg
    Is there a way to compile Twirl templates without the SBT plugin?
    prog20901
    @prog20901

    What are the differences between an akka actor vs java thread

    Lot of people shows interest about Akka framework, but so far have not seen many actual examples of use cases it would be good for.

    So I would be interested in hearing about things developers have used it successfully.

    Also, i found it difficult in understanding any snippet

    Can anyone shows a simple example to show how Akka works and makes the difference.

    Does Default Java features misses things and has been achieved via Akka ??

    multi threading with Executor Service vs Using Akka

    1. Does actors was very good and faster than executor service threads.

    2. Why people are moving to Actors when java executor service is good

    nafg
    @nafg
    @prog20901 how do you communicate between threads?
    Michael Wolfendale
    @wolfendale
    @natonic77
    final case class Simple(rank: String, somethingElse: String, code: String, desc: String)
    
    object Simple {
    
      import play.api.libs.json._
      import play.api.libs.functional.syntax._
    
      implicit val writes: OWrites[Simple] = (
        (__ \ "rank").write[String] and
        (__ \ "somethingElse").write[String] and
        (__ \ "code").write[String] and
        (__ \ "desc").write[String]
      )(unlift(Simple.unapply))
    
      implicit val reads: Reads[Simple] = {
    
        val commonFields =
          (__ \ "rank").read[String] and
          (__ \ "somethingElse").read[String]
    
        val oldReads: Reads[Simple] = (
          commonFields and
          (__ \ "code").read[String] and
          (__ \ "desc").read[String]
        )(Simple(_, _, _, _))
    
        val newReads = (
          commonFields and
          (__ \ "newCode").read[String] and
          (__ \ "newDesc").read[String]
        )(Simple(_, _, _, _))
    
        newReads orElse oldReads
      }
    }
    something like this?
    Shinse Tanaka
    @rms-shinse
    Hi guys, Can I create custom Routing modifier tag like + nocsrf and How???
    I'm using Play 2.6 now.
    Shinse Tanaka
    @rms-shinse

    I found official doc, and understood howto use HandlerDef. getModifiers();
    resolved.

    https://www.playframework.com/documentation/2.7.x/Highlights26#Route-modifier-tags

    N Banek
    @natonic77

    @wolfendale, that is exactly what I tried before my initial post, but I ran into trouble getting it to compile. I thought it was because I couldn't combine commonFields with other .read calls, but your reply inspired me to take another look, and I realized it had nothing to do with the 'and'.

    My problem stemmed from the fact that my format changes were not sequential fields and not at the beginning or end. I was using invalid syntax to apply the Reads to my object with explicitly named parameters. For example, in a case where "code" was after "rank" in the Simple case class signature, I was trying to do this:

        val newReads = (
          commonFields and
          (__ \ "newCode").read[String] and
          (__ \ "newDesc").read[String]
        )(Simple(rank=_, somethingElse=_, code=_, desc=_))

    when I should have been doing this:

        val newReads = (
          commonFields and
          (__ \ "newCode").read[String] and
          (__ \ "newDesc").read[String]
        )((rank,somethingElse,code,desc) => Simple(rank, code,somethingElse,desc))

    My scala-fu is still weak. Anyway, the corrected syntax for applying the Reads parameters explicitly (as in the second example) solved my problem.

    Thanks a ton for writing back in.

    Michael Wolfendale
    @wolfendale
    @natonic77 No problem. If you want to put parameters in different positions you can do that, you just need to pass your own function to the builder
        val oldReads: Reads[Simple] = (
          commonFields and
          (__ \ "code").read[String] and
          (__ \ "desc").read[String]
        )((rank, somethingElse, code, desc) => Simple(rank, somethingElse, code, desc))
    But obviously you can now put those paramters anywhere you want.
    mam-ijinus
    @mam-ijinus
    Hi !
    I have a little issue :)
    Ime start a new project using play 2.7 and EBean with a h2 db.
    I've used this doc: https://www.playframework.com/documentation/2.7.0/JavaEbean for the first implementation test
    The issue is that the field "dueDate" of the TASK entity is created with the name DUEDATE into the database. And play look for the field "DUE_DATE" with an underscore...
    Why the field is created without underscore ? Is there a ebean property to set ?
    Thanks for the help.
    mam-ijinus
    @mam-ijinus
    Nobody ??
    mam-ijinus
    @mam-ijinus
    I 've deleted my db and now the field are well created. But now at each change it erase all table to recreate all with evolution framework of play
    mam-ijinus
    @mam-ijinus
    I've also deleted a sql file, so ebean DDL add a 1.sql
    Alexis Hernandez
    @AlexITC
    you are supposed to create new evolution files, reusing the already applied ones will rollback and apply them again
    btw, why don't just use something decent for scala? play has great support with anorm
    and it's very simple to use
    mam-ijinus
    @mam-ijinus
    ok, so the use of sql files are mandatory. I code in java and I don't know anorm.
    Will see that