I am using quill library and mapping entities via case classes
case class Country(id: Long, name: String)
How can I set unique
constraint on name field?
Related
I have a usecase where incoming json string doesn't have a structure similar to the case class.
I have following case classes:
case class Parent(children: Seq[Child])
case class Child(name: Option[String], age: Option[Int])
User inputs in Parent field as:
John:20, Peter:12
Incoming json is as:
{"parent":{"parent":"John:20, Peter:12"},....}
Is it possible I write json formatter to map this to the Parent case class with nested Child list, using play framework json API?
What I'm trying to do in Scala 2.11 and akka is have one case class but two different validations based on which route is being hit.
For example, let's consider the case class below
case class User(_id: String, name: String, age: Int, address: String)
Now while the /create route is hit, I don't need _id but I need all the other fields.
But while /update route is hit, I need the _id and the fields that are to be updated (which could be one or all three)
Only declaring Option doesn't serve the purpose because then my /create route goes for a toss.
Even extending case classes doesn't really work seamlessly (there's too much code duplicity).
I would love if something like this was possible
case class User(_id: String, name: String, age: Int, address: String)
case class SaveUser() extends User {
require(name.nonEmpty)
require(age.nonEmpty)
require(address.nonEmpty)
}
case class UpdateUser() extends User {
require(_id.nonEmpty)
}
Is there an elegant solution to this? Or do I have to create two identical case classes?
My suggestion would be to encode different case classes for different requirements, but if you insist you must share code between these two cases a possible solution would be to parameterize the case class
case class User[Id[_], Param[_]](_id: Id[String], name: Param[String], age: Param[Int], address: Param[String])
Then you define an alias for the Identity type constructor and your two uses of the case class
type Identity[T] = T
type SaveUser = User[Option, Identity]
type UpdateUser = User[Identity, Option]
I'm trying to work out how to save nested case classes with Spark Cassandra Connector. As a simple example:
Case classes:
case class Foo(id: String, bar: Bar)
case class Bar(field1: String)
Cassandra table:
CREATE TABLE foo (id text, field1 text, PRIMARY KEY (id));
Spark code:
val foo = Foo("a", Bar("b"))
val fooRdd = sparkContext.parallelize(Seq(foo))
fooRdd.saveToCassandra(cassandraKeyspace, "foo")
Results in:
Exception in thread "main" java.lang.IllegalArgumentException: requirement failed: Columns not found in Foo: [field1]
I realise I could make a new case class that flattens out Foo, but I'd rather not do this if possible. Ive played around with Column Mappers but to no avail. Is there a better way?
please take a look at the following code:
scala> sealed abstract class Person(val name: String)
defined class Person
scala> case class Student(id: Int, name: String) extends Person(name)
<console>:8: error: overriding value name in class Person of type String;
value name needs `override' modifier
case class Student(id: Int, name: String) extends Person(name)
^
This might be a trivial question, but after searching the web for quite some time, I wasn't able to figure out how to simply pass the string that Student's constructor will be provided as name to the Person's constructor. I don't want to override anything. What am I doing wrong?
Thank you very much in advance!
All constructor parameters of a case class are vals. That's the whole point. Roughly speaking, it is what gives you the ability to enjoy the benefits cases classes provide compared to regular classes: copying, extraction, pattern matching, etc.
If you want Student to be a case class, you should override name. Theoretically, you can avoid overriding it by giving the val a different name: case class Student(id: Int, studentName: String) extends Person(studentName) - this works, but just doesn't make very much sense - you end up having two different member vals whose values are always identical.
Alternatively, if there is an actual reason why you don't want to override name (I can't imagine what one could possibly be, but if ...), then Student should not be a case class: class Student(val id: Int, name: String) extends Person(name).
I have the following case class
case class AccountId(value: Long) extends EntityId(value)
And I use it in many DTOs, such as:
case class GetAccount(
#ApiModelProperty(dataType = "long")
id: AccountId,
email: Email
)
But in every dto in which I use AccountId I have to add ApiModelProperty to properly display the model structure in swagger.
Is it possible to annotate only the AccountIt case class definition and avoid the redundant code?