score:2
Accepted answer
you could try the following:
trait modelops[t] {
def table: appdb.table // not sure about type
def order: appdb.orderbypredicate // not sure about type
def tojson(model: t): jsvalue
def add(model: t): option[t] = option(appdb.categories.insert(model))
def remove(id: long) = appdb.categories.delete(id)
def getall(): list[t] = from(table)(model => select(model) orderby(order)) tolist
def alltojson() = {
val json:list[jsvalue] = getall.map{tojson(_)}
json.tojson(json.toseq)
}
}
then you can for each model type:
object modeltype extends modelops[modeltype] {
def table = appdb.examples
def order = yourpredicate
def tojson(model:modeltype):jsvalue = {
json.tojson(map("field" -> json.tojson(model.field)))
}
def validate(different values for each model) = // is fairly different for each one. validates the submitted fields from a user
}
update about the true type of appdb.orderbypredicate
:
calling select
on primitivetypemode
returns a selectstate
. on this selectstate
, you will call orderby
which takes a list[basequeryyield#o]
(or multiple of those in the same argument list). hence you should define:
def order(model: t): list[basequeryyield#o]
and
def getall() = from(table)(model => select(model) orderby(order(model))) tolist
by the way, basequeryyield#o
resolves to expressionnode
.
Source: stackoverflow.com
Related Query
- Scala Data Modeling and Generics
- About generics in Java and Scala
- Parsing JSON in Play2 and Scala without Data Type
- Which is best data access options available for Play framework with Scala and PostgreSQL?
- Guice And Scala - Injection on Generics Dependencies
- Scala generics with <: and multiple traits
- Scala: Higher kinded, open-type and wild card generics in Java, C#, Scala and C++
- scala generics and inheritance
- Scala and Java Generics -- Extracting and returning nested types
- Java interoperability woes with Scala generics and boxing
- Scala Generics and Numeric Implicits
- "Composite" algebraic data types in Scala and Haskell
- Using Iteratees and Enumerators in Play Scala to Stream Data to S3
- cast schema of a data frame in Spark and Scala
- The intersection of Java generics and Scala is...not going well
- group by and filter highest value in data frame in scala
- Spark Scala - How do I iterate rows in dataframe, and add calculated values as new columns of the data frame
- Scala data structures: chain of operations (such as mapValues, filter ...) and intermediate results
- Data Structures and Algorithms book in Java 5 or 6 or Scala for programmers and not a CS class textbook
- Type classes vs Data Types in functional Libraries in scala such as Cats and ScalaZ
- What mechanisms does Scala have for generics and wildcards compared to Java?
- Read from Kafka topic process the data and write back to Kafka topic using scala and spark
- Data preprocessing with apache spark and scala
- How to read from textfile(String type data) map and load data into parquet format(multiple columns with different datatype) in Spark scala dynamically
- Scala data structure and complexity O(1)
- Creating Test Scope/Context to seed data and run application, Scala Play Framework 2
- Scala traits using generics and case class methods
- Scala type inference with generics and covariance, possible issue in scalac
- Scala : generics and variable inheritance issue
- How to call a function which returns an int in Scala data frame and append it
More Query from same tag
- Add/Remove elements in a JSON Array in Scala/Play
- scala.MatchError on actor
- Scala 2.8.0 problems on Windows 7
- Spark RDD[(String, String)] into RDD[Map[String, String]]
- scala collections : map a list and carry some state?
- Scala API: view IndexedSeq[T] as Map[Int, T]
- How can I ensure that the dynamic type of my custom Scala collection is preserved during a map()?
- in Scala, execute a block and ignore but log exceptions
- Subtype restriction on type parameter
- Unable to authenticate OAuth2 with Akka-Http
- Scala SBT custom lib managed path
- Is it possible to override built-in operators in Scala?
- Finding the average of corresponding values by zipping
- Spark Reading Json
- Apache SparkĀ“s behavior with Regular Files
- How to convert, in Scala, an Array[VertexIds] to a Map?
- Can someone explain the right way to use SBT?
- better solution than timertask with scala in play framework
- Scala Gson Type Adapter not Being Called on List[Enumeration]
- View Bound methods, ordered Trait and implicit converting
- Scala Macro: Create new classes with Option types
- What languages are mature to develop native apps in Android [except Java]
- How do I create an Enum from an interger value
- How to group by a dataframe of a specific class
- Pattern matching and (ereased) generic function type argument
- How do I verify mockito calls in asynchronous methods
- Reproduce akka-stream async output
- Is it possible in scala to have a generic method taking a method as an argument?
- Spark, applying filters on DataFrame(or RDD) multiple times without redundant evaluations
- Akka - when should I use the Bootable class