I have the following Phantom table definition:
package myPackage
import com.outworkers.phantom.CassandraTable
import com.outworkers.phantom.connectors.RootConnector
import com.outworkers.phantom.dsl._
import com.outworkers.phantom.keys.{PartitionKey, PrimaryKey}
import scala.concurrent.Future
case class KeysTwoThreeAndFour(myKeyTwo: Int, myKeyThree: String, myKeyFour: Int)
abstract class MyTable extends CassandraTable[MyTable, Int] with RootConnector {
object myKeyOne extends IntColumn(this) with PartitionKey
object myKeyTwo extends IntColumn(this) with PrimaryKey
object myKeyThree extends StringColumn(this) with PrimaryKey
object myKeyFour extends IntColumn(this) with PrimaryKey
object myValue extends IntColumn(this)
def insertValue(myKeyOne: Int, valuesMap: Map[KeysTwoThreeAndFour, Int]): Future[Unit] = {
val resultFutures = for ((key: KeysTwoThreeAndFour, myValue) <- valuesMap) yield {
store(myKeyOne, key.myKeyTwo, key.myKeyThree, key.myKeyFour, myValue).future()
}
Future.sequence(resultFutures).map { _ => () }
}
}
This compiles fine, but at runtime throws the following exception:
java.lang.ClassCastException: scala.Tuple5 cannot be cast to scala.runtime.Nothing$
at myPackage.MyTable$anon$macro$1$1.store(MyTable.scala:10)
at com.outworkers.phantom.CassandraTable.store(CassandraTable.scala:125) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
at myPackage.MyTable$$anonfun$2.apply(MyTable.scala:19)
at myPackage.MyTable$$anonfun$2.apply(MyTable.scala:18)
...
I am following the examples in the bottom of the Phantom table docs, what seems to be the problem? Is the issue perhaps that I have a simple Int as my "Record" type instead of an actual class?
I am using phantom-dsl 2.7.6, Play Framework 2.3.10 and Scala 2.11.11.
Note that the following code works fine:
insert
.value(_.myKeyOne, myKeyOne)
.value(_.myKeyTwo, key.myKeyTwo)
.value(_.myKeyThree, key.myKeyThree)
.value(_.myKeyFour, key.myKeyFour)
.value(_.myValue, myValue)
.future()
Thanks.
It seems that the issue was due to the "Record" type being a primitive, instead of a class; in other words, wrapping my Int in a case class having that Int as the only member solved the problem.
Additionally, there was a problem with the Select statement, which was solved too:
java.util.concurrent.ExecutionException: Boxed Error
at com.outworkers.phantom.CassandraTable.fromRow(CassandraTable.scala:85) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
at com.outworkers.phantom.SelectTable$$anonfun$select$1.apply(SelectTable.scala:24) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
at com.outworkers.phantom.SelectTable$$anonfun$select$1.apply(SelectTable.scala:24) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
at com.outworkers.phantom.builder.query.SelectQuery.fromRow(SelectQuery.scala:59) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
at com.outworkers.phantom.builder.query.RootExecutableQuery$class.singleResult(ExecutableQuery.scala:176) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
at com.outworkers.phantom.builder.query.SelectQuery.singleResult(SelectQuery.scala:33) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
Related
I have followed the pattern from examples on GitHub. When I call store on the model object, passing an instance of the entity, I get a compile error indicating one of the implicit parameters is missing as shown below.
could not find implicit value for parameter sg: com.outworkers.phantom.macros.SingleGeneric.Aux[com.ss.wuhu.settlement.entity.Settlement,Repr,HL,Out]
I guess I am missing something obvious. Could someone please point out how to bring the implicit into scope?
Regards
Meeraj
This is the code snippet where I am storing the data.
import akka.Done
import com.outworkers.phantom.dsl._
import com.outworkers.phantom.connectors.{CassandraConnection, ContactPoints}
import com.ss.wuhu.settlement.entity.Settlement
import com.ss.wuhu.settlement.entity.mapping.{SettlementForCourierModel, SettlementForVendorModel}
object Connector {
private val hosts = Seq("127.0.0.1") // TODO from environment
lazy val connector: CassandraConnection = ContactPoints(hosts).keySpace("wuhu_order")
}
class SettlementDatabase(override val connector: CassandraConnection) extends Database[SettlementDatabase](connector) {
object SettlementForCourierModel extends SettlementForCourierModel with connector.Connector
object SettlementForVendorModel extends SettlementForVendorModel with connector.Connector
def truncateAll() = {
Database.truncate()
}
def store(set: Settlement) = {
for {
v <- Database.SettlementForVendorModel.store(set)
d <- Database.SettlementForCourierModel.store(set)
} yield (Done)
}
}
object Database extends SettlementDatabase(Connector.connector)
This is a known bug with an open issue: https://github.com/outworkers/phantom/issues/774
I suggest either using the workaround as described in the link above, or my workaround which was creating my own .store() using .insert().
example:
def myStore(person: Person) : Future[ResultSet] =
insert
.value(_.name, person.name)
.value(_.age, person.age)
.value(_.timeCreate, person.timeCreate)
.future()
Is there a way to nest an enum within a data class in Kotlin?
data class D(val a:Any) {
enum class E {F,G}
...
}
Or to declare it inline within a function?
fun foo() {
enum class E {F,G}
doSomething()
}
I can't find documentation on the rules for where enums are allowed to be declared.
Yes, you can nest the enum in a data class, but not in a function:
data class Outer(val a: InnerEnum) {
enum class InnerEnum { A, B }
}
fun foo() {
val o = Outer(Outer.InnerEnum.A)
println(o) // --> Outer(a=A)
}
There is no kotlin specification for the syntax at present. if you want to find the specification you can see JLS, because Kotlin is based on Java, so some specifications also appropriate in Kotlin.
A nested enum type is implicitly static. It is permitted for the declaration of a nested enum type to redundantly specify the static modifier.
This implies that it is impossible to declare an enum type in the body of an inner class (§8.1.3), because an inner class cannot have static members except for constant variables.
and, all local classes are inner classes.
So the enum class can be declared anywhere except the local function scope and inner classes.
If you don't sure where can define a type you can try to prompt the scope in turn: local > class > top, then the kotlin compiler will give you the correct compiler error message to you. for example:
IF you define a const val in the local function, the compiler report the error as below:
fun local() {
const val foo="bar"
// ^--- the modifier `const` is not applicable to `local variable`
}
IF you define a const val in the common class/interface, the compiler will report the error as below:
interface Foo {
const val foo = "bar"
//^--- `const val` only allowed on top-level or objects.
}
Considering the given code:
val repository =
context.getBean(
Introspector.decapitalize(t.getClass.getSimpleName).replace("C", "E").concat("Repository"))
and that my repositories have a String as Serializable.
I'm trying to do the following:
repository.asInstanceOf[ElasticsearchRepository[_, String]].save(getObject(t))
This one works fine:
repository.asInstanceOf[ElasticsearchRepository[_, String]].findAll()
But I don't know how to put that above to work.
Assuming the method getObject(t) is retuning the correct object to be persisted and since it's a Spring Data Repository, there are 2 save method. One that accept a single entity and another for a list of entities and it says overloaded method value save.
What I have tried so far:
I saw in another thread to force the method with a type, something like this:
repository.asInstanceOf[ElasticsearchRepository[_, String]].save(getObject(t) : TYPE)
This is ok if I knew the type and also my method getObject should return that same type.
Here is my getObject method which I return the object itself without any specific type:
#throws[IOException]
def getObject[T](t : T) = {
objectMapper.readValue(objectMapper.writeValueAsString(t), getClazz(t))
}
So I was trying to get the type like this:
val m = Manifest.classType(getClazz(t))
type TYPE = m.type
Looks good if I force my object to this type using getObject(t) : TYPE but I don't know how to use this same type in my getObject method to be returned.
Anyway, I don't even know if this is the best approach to do this, invoking a generic repository and save a generic object.
Just to understand what I'm trying to do, I'm using a aspect to intercept a Cassandra entity to be persisted, then get it and turn into a ElasticSearch entity to save a json(thats why the getObject(t)) and replicate into ElasticSearch.
Here is the full aspect class:
#Component
#Aspect
class ElasticAop {
#Autowired val context : ApplicationContext = null
val objectMapper : ObjectMapper = new ObjectMapper()
#Pointcut("execution(* com.test.service.cassandra.*.post(..)) && args(t)")
def getPointcutPost[T](t : T) : Unit = {}
#throws[Throwable]
#Before("getPointcutPost(t)")
def elasticSaveAspect[T](joinPoint: JoinPoint, t: T) = {
val m = Manifest.classType(getClazz(t))
type TYPE = m.type
val repository =
context.getBean(
Introspector.decapitalize(t.getClass.getSimpleName).replace("C", "E").concat("Repository"))
repository.asInstanceOf[ElasticsearchRepository[_, String]].findAll()
repository.asInstanceOf[ElasticsearchRepository[_, String]].save(getObject(t))
}
#throws[ClassNotFoundException]
def getClazz[T](t : T) = {
val className = t.getClass.getName.replace("cassandra", "elastic").replace("C", "E")
Class.forName(className)
}
#throws[IOException]
def getObject[T](t : T) = {
objectMapper.readValue(objectMapper.writeValueAsString(t), getClazz(t))
}
}
EDITED
Even setting up a type return in my getObject to Address and then setting the save method as follow save(getObject(t) : Address) give me the same overloaded error.
EDITED
I just figured out it's a limitation and a possible work around is to create a factory or something like this.
Then I created a service with a saveOrUpdate method:
trait ElasticGenericService[T <: ElasticGenericKey, R <: ElasticsearchRepository[T, String]] {
var r : R = _
def saveOrUpdate(t: T) = r.save(t)
}
and now I'm getting a cast exception:
java.lang.ClassCastException: Address cannot be cast to scala.runtime.Nothing$
What i can see here:
getObject[T](t : T) returns existential type _1 and actually kills all type checks, as you choosing the class in runtime
ElasticsearchRepository[_, String].save require existential type _2 to be passed to the save method, so _1 doesn't fit
Possible solution:
repository.asInstanceOf[ElasticsearchRepository[Any, String]].save(getObject(t).asInstanceOf[Any]) //getClass will work with runtime class instead of Any, so should be fine
Another solution (saving existential type):
def getObject[T](t : T) = {
objectMapper.readValue(objectMapper.writeValueAsString(t), getClazz(t)).asInstanceOf[T]
} //assuming T is an existential - it will return same existential as you passed
We have a class like this in a Grails 2.4.3 application (migrated from 2.3.8):
#Validateable
class Foo {
Integer noDefault;
Integer withDefault = 1;
static constraints = {
noDefault(nullable:false)
withDefault(nullable:false)
}
}
This class is being instantiated in a complex configuration mechanism using a Map like this:
[
noDefault: 0,
withDefault: 2
]
(In fact the Map is part of a huge one, but the class constructor sees this small one.) Formerly the class worked if we omitted the withDefault entry from the config map, using the default value which is not null. In Grails 2.4.3, however, it tells me that this field cannot be null. I can fix it by letting it be null in the constraint, but it lets setting the explicite value null (and overwrite the default value), which causes problem during operation.
Do you know some workaround, which preserves the semantics and correct operation?
Thanx in advance, best regards: Balázs
What you are describing is not consistent with what I would expect and not consistent with the behavior I am seeing. The project at https://github.com/jeffbrown/validatedefaults contains the following code.
At https://github.com/jeffbrown/validatedefaults/blob/master/src/groovy/demo/Foo.groovy
// src/groovy/demo/Foo.groovy
package demo
import grails.validation.Validateable
#Validateable
class Foo {
Integer noDefault;
Integer withDefault = 1;
static constraints = {
noDefault(nullable:false)
withDefault(nullable:false)
}
}
The test at https://github.com/jeffbrown/validatedefaults/blob/master/test/unit/demo/FooSpec.groovy passes:
// test/unit/demo/FooSpec.groovy
package demo
import spock.lang.Specification
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
#TestMixin(GrailsUnitTestMixin)
class FooSpec extends Specification {
void 'test validating default values'() {
given:
def map = [noDefault: 0]
def foo = new Foo(map)
expect:
foo.validate()
}
}
When I run the app I get the same behavior.
// grails-app/conf/BootStrap.groovy
import demo.Foo
class BootStrap {
def init = { servletContext ->
def map = [noDefault: 0]
def foo = new Foo(map)
// this prints true...
println "Foo is valid? : ${foo.validate()}"
}
def destroy = {
}
}
I hope that helps.
export class MyClass extends kendo.data.ObservableObject {
constructor() {
super();
super.init(this);
}
.
.
.
.
}
I get compilation error:
Error 599 Build: Type name 'kendo.data.ObservableObject' in extends clause does not reference constructor function for 'kendo.data.ObservableObject'.
What am I missing here ?
Type name 'kendo.data.ObservableObject' in extends clause does not reference constructor function for 'kendo.data.ObservableObject'.
Based on http://docs.telerik.com/kendo-ui/api/framework/observableobject the constructor needs as least one argument (the object to observe) So you need to call the constructor with an argument as shown:
export class MyClass extends kendo.data.ObservableObject {
constructor(objToObserve) {
super(objToObserve);
}
.
.
.
.
}
This error, perhaps the most confusing issued by the compiler, means that when the compiler looked up the value indicated by the type in the extends clause, it didn't resolve to a value that was a constructor function for that type.
A smaller example:
class C { }
module M {
var C = 3;
class D extends C { } // <-- Error
}
Here, the type name C inside M means class C, but the value name C means the var declared in the module.
It sounds like you might have a var kendo or var data somewhere in your program. To diagnose this, above the line you write extends kendo.data.ObservableObject, write var foo: kendo.data.ObservableObject and see what error you get.