SnakeYaml issue in scala - scala

I'm trying to figure out how to use nested data types with snakeyaml. I'm not able to get the inner class to display properly
class RawEdgeTypeSpec() {
#BeanProperty var edge_type: Int = -1
#BeanProperty var weighted: String = ""
}
class RawNodeTypeSpec {
#BeanProperty var node_type: Int = -1
#BeanProperty var edge_types: List[RawEdgeTypeSpec] = List()
}
val spec3 = new RawEdgeTypeSpec()
spec3.setEdge_type(2)
spec3.setWeighted("true")
val spec2 = new RawNodeTypeSpec()
spec2.setNode_type(2)
spec2.setEdge_types(List(spec3))
val output = new Yaml().dump(spec2)
This prints
edge_types: !!scala.collection.immutable.$colon$colon {}
node_type: 2
edge_types is not recognized and serialized properly
Similarly I'm having trouble deserializing yaml

SnakeYAML is a Java lib that doesn't know Scala, so you'll need to use java.util.List[RawEdgeTypeSpec] for SnakeYAML to understand that this is a list.

Related

Scala case classes with new firebase-server-sdk

Does the new server SDK for firebase firebase-server-sdk (3.0.1) support Scala case class deserialization? The previous firebase java sdk used jackson which you could bolt in a scala module to support case classes. It's unclear if its possible to do something similar with the new SDK? Does it use Gson or some custom class mapper?
In the simplest example:
case class Person(firstName: String, lastName: String, age: Int)
With a Firebase listener setup such as:
var options = new FirebaseOptions.Builder()
.setDatabaseUrl("https://<your firebase>.firebaseio.com")
.setServiceAccount(new FileInputStream("firebase-auth.json"))
.build()
FirebaseApp.initializeApp(options);
var ref = FirebaseDatabase.getInstance().getReference("somepath")
ref.addListenerForSingleValueEvent(new ValueEventListener {
override def onDataChange(dataSnapshot: DataSnapshot): Unit = {
println(dataSnapshot.getValue(classOf[Person]))
}
override def onCancelled(databaseError: DatabaseError): Unit = {
println(databaseError.getMessage)
}
})
This will fail on the getValue call dataSnapshot.getValue(classOf[Person]) with the exception:
Exception in thread "FirebaseDatabaseEventTarget" com.google.firebase.database.DatabaseException: No properties to serialize found on class Person
at com.google.firebase.database.utilities.encoding.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:495)
at com.google.firebase.database.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:285)
at com.google.firebase.database.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:379)
at com.google.firebase.database.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:187)
at com.google.firebase.database.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:61)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:181)
at PetEventsNodeActorSpec$$anonfun$2$$anonfun$apply$mcV$sp$2$$anonfun$apply$mcV$sp$3$$anon$1.onDataChange(PetEventsNodeActorSpec.scala:290)
at com.google.firebase.database.Query$1.onDataChange(Query.java:147)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:57)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:45)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:35)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I've tried adding annotations to the class such as #BeanProperty but then get:
Exception in thread "FirebaseDatabaseEventTarget" com.google.firebase.database.DatabaseException: Class Person is missing a constructor with no arguments
Rather than go down the rabbit hole of annotating or adding code to every case class, any ideas on getting scala case classes to play nicely with the new firebase sdk?
AFAIK, there isn't a way to use a case class directly. I ended up creating plain classes with #BeanProperty annotations and then converting them to case classes. The reason for
Exception in thread "FirebaseDatabaseEventTarget" com.google.firebase.database.DatabaseException: Class Person is missing a constructor with no arguments
is because your class constructor must be nullary (i.e. it cannot take any arguments):
import scala.beans.BeanProperty
case class Person(firstName: String, lastName: String, age: Int) {
def toBean: PersonBean = {
val person = new PersonBean()
person.firstName = firstName
person.lastName = lastName
person.age = age
person
}
}
class PersonBean() {
#BeanProperty var firstName: String = ""
#BeanProperty var lastName: String = ""
#BeanProperty var age: Int = 0
def toCase: Person = Person(firstName, lastName, age)
}
var ref = FirebaseDatabase.getInstance().getReference("somepath")
ref.addListenerForSingleValueEvent(new ValueEventListener {
override def onDataChange(dataSnapshot: DataSnapshot): Unit = {
val record = dataSnapshot.getValue(classOf[PersonBean])
val person = if (record != null) record.toCase else null
}
override def onCancelled(databaseError: DatabaseError): Unit = {
println(databaseError.getMessage)
}
})

Map key not found error despite using option classes

I'm new to the concept of using the Option type but I've tried to use it multiple places in this class to avoid these errors.
The following class is used to store data.
class InTags(val tag35: Option[String], val tag11: Option[String], val tag_109: Option[String], val tag_58: Option[String])
This following code takes a string and converts it into a Int -> String map by seperating on an equals sign.
val message= FIXMessage("8=FIX.4.29=25435=D49=REDACTED56=REDACTED115=REDACTED::::::::::CENTRAL34=296952=20151112-17:11:1111=Order7203109=CENTRAL1=TestAccount63=021=155=CSCO48=CSCO.O22=5207=OQ54=160=20151112-17:11:1338=5000040=244=2815=USD59=047=A13201=CSCO.O13202=510=127
")
val tag58 = message.fields(Some(58)).getOrElse("???")
val in_messages= new InTags(message.fields(Some(35)), message.fields(Some(11)), message.fields(Some(109)), Some(tag58))
println(in_messages.tag_109.getOrElse("???"))
where the FIXMessage object is defined as follows:
class FIXMessage (flds: Map[Option[Int], Option[String]]) {
val fields = flds
def this(fixString: String) = this(FIXMessage.parseFixString(Some(fixString)))
override def toString: String = {
fields.toString
}
}
object FIXMessage{
def apply(flds: Map[Option[Int], Option[String]]) = {
new FIXMessage(flds)
}
def apply(flds: String) = {
new FIXMessage(flds)
}
def parseFixString(fixString: Option[String]): Map[Option[Int], Option[String]] = {
val str = fixString.getOrElse("str=???")
val parts = str.split(1.toChar)
(for {
part <- parts
p = part.split('=')
} yield Some(p(0).toInt) -> Some(p(1))).toMap
}
}
The error I'm getting is ERROR key not found: Some(58) but doesnt the option class handle this? Which basically means that the string passed into the FIXMessage object doesnt contain a substring of the format 58=something(which is true) What is the best way to proceed?
You are using the apply method in Map, which returns the value or throw NoSuchElementException if key is not present.
Instead you could use getOrElse like
message.fields.getOrElse(Some(58), Some("str"))

Sharing variables among objects in Scala

Is there a way to share a variable among all objects (instantiated from the same type)? Consider the following simple program. Two objects name and name2 have the same type A. Is there way to connect the properyList inside the two instantiation name and name2?
class A {
var properyList = List[String]()
def printProperties(): Unit = {
println(properyList)
}
}
object Experiment {
def main(args: Array[String]): Unit = {
val name = new A
val name2 = new A
name.properyList = List("a property")
name.printProperties()
name2.printProperties()
}
}
The output is
List(a property)
List()
Any way to change the class definition so that by just changing the .properyList in one of the objects, it is changed in all of the instatiations?
What you seem to be looking for is a class variable. Before I get into why you should avoid this, let me explain how you can do it:
You can attach propertyList to the companion object instead of the class:
object A {
var properyList = List[String]()
}
class A {
def printProperties(): Unit = {
println(A.properyList)
}
}
Now, to the why you shouldn't:
While scala let's you do pretty much anything that the JVM is capable of, its aims are to encourage a functional programming style, which generally eschews mutable state, especially shared, mutable state. I.e. the anti-pattern in A is not only that propertyList is a var, not a val but by sharing it via the companion object, you further allow anyone, from any thread to change the state of all instances at anytime.
The benefit of declaring your data as val is that you can safely pass it around, since you can be sure that nobody can change from under you at any time in the future.
You seem to be looking for something like java static fields.
In scala you usually achieve something like that by using a companion object:
object Main extends App {
class A {
import A._
def printProperties(): Unit = {
println(properyList)
}
}
object A {
private var properyList = List[String]()
def addProperty(prop: String): Unit = {
properyList ::= prop
}
}
val name = new A
val name2 = new A
A.addProperty("a property")
name.printProperties()
name2.printProperties()
}
If you want to have something similar to java's static fields you will have to use companion objects.
object Foo {
private var counter = 0
private def increment = {
counter += 1;
counter
}
}
class Foo {
val i = Foo.increment
println(i)
}
Code copied from:
"Static" field in Scala companion object
http://daily-scala.blogspot.com/2009/09/companion-object.html
Based on Arne Claassen's answer, but using private mutable collection with the companion object, which makes it visible only to the companion classes. Very simplistic example tried out in scala 2.11.7 console:
scala> :paste
// Entering paste mode (ctrl-D to finish)
object A {
private val mp = scala.collection.mutable.Map("a"->1)
}
class A {
def addToMap(key:String, value:Int) = { A.mp += (key -> value) }
def getValue(key:String) = A.mp.get(key)
}
// Exiting paste mode, now interpreting.
defined object A
defined class A
// create a class instance, verify it can access private map in object
scala> val a = new A
a: A = A#6fddee1d
scala> a.getValue("a")
res1: Option[Int] = Some(1)
// create another instance and use it to change the map
scala> val b = new A
b: A = A#5e36f335
scala> b.addToMap("b", 2)
res2: scala.collection.mutable.Map[String,Int] = Map(b -> 2, a -> 1)
// verify that we cannot access the map directly
scala> A.mp // this will fail
<console>:12: error: value mp is not a member of object A
A.mp
^
// verify that the previously created instance sees the updated map
scala> a.getValue("b")
res4: Option[Int] = Some(2)

Scala serialization/deserialization of singleton object

I am quite new to the scala programming language, and I currently need to do the following. I have a signleton object like the following:
object MyObject extends Serializable {
val map: HashMap[String, Int] = null
val x: int = -1;
val foo: String = ""
}
Now i want to avoid to have to serialize each field of this object separately, thus I was considering writing the whole object to a file, and then, in the next execution of the program, read the file and initialize the singleton object from there. Is there any way to do this?
Basically what I want is when the serialization file doesn't exist, those variables to be initialized to new structures, while when it exists, the fields to be initialized from the ones on the file. But I want to avoid having to serialize/deserialize every field manually...
UPDATE:
I had to use a custom deserializer as presented here: https://issues.scala-lang.org/browse/SI-2403, since i had issues with a custom class I use inside the HashMap as values.
UPDATE2:
Here is the code I use to serialize:
val store = new ObjectOutputStream(new FileOutputStream(new File("foo")))
store.writeObject(MyData)
store.close
And the code to deserialize (in a different file):
#transient private lazy val loadedData: MyTrait = {
if(new File("foo").exists()) {
val in = new ObjectInputStream(new FileInputStream("foo")) {
override def resolveClass(desc: java.io.ObjectStreamClass): Class[_] = {
try { Class.forName(desc.getName, false, getClass.getClassLoader) }
catch { case ex: ClassNotFoundException => super.resolveClass(desc) }
}
}
val obj = in.readObject().asInstanceOf[MyTrait]
in.close
obj
}
else null
}
Thanks,
No needs to serialize an object with only immutable fields (because the compiler will do it for you...) I will assume that the object provides default values. Here is a way to do this:
Start by writing an trait with all the required fields:
trait MyTrait {
def map: HashMap[String, Int]
def x: Int
def foo: String
}
Then write an object with the defaults:
object MyDefaults extends MyTrait {
val map = Map()
val x = -1
val foo =
}
Finally write an implementation unserializing data if it exists:
object MyData extends MyTrait {
private lazy val loadedData: Option[MyTrait] = {
if( /* filename exists */ ) Some( /*unserialize filename as MyTrait*/)
else None
}
lazy val map = loadedData.getOrElse( MyDefault ).map
lazy val x = loadedData.getOrElse( MyDefault ).x
lazy val foo = loadedData.getOrElse( MyDefault ).foo
}

How to store child objects on GAE using JDO from Scala

I'm have a parent-child relation between 2 classes, but the child objects are never stored. I do get an warning:
"org.datanucleus.store.appengine.MetaDataValidator checkForIllegalChildField: Unable to validate relation net.vermaas.kivanotify.model.UserCriteria.internalCriteria"
but it is unclear to me why this occurs. Already tried several alternatives without luck.
The parent class is "UserCriteria" which has a List of "Criteria" as children.
The classes are defined as follows (Scala):
class UserCriteria(tu: String, crit: Map[String, String]) extends LogHelper {
#PrimaryKey
#Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
var id = KeyFactory.createKey("UserCriteria", System.nanoTime)
#Persistent
var twitterUser = tu
#Persistent
var internalCriteria: java.util.List[Criteria] = flatten(crit)
def flatten(crits: Map[String, String]) : java.util.List[Criteria] = {
val list = new java.util.ArrayList[Criteria]
for (key <- crits.keySet) {
list.add(new Criteria(this, key, crits(key)))
}
list
}
def criteria: Map[String, String] = {
val crits = mutable.Map.empty[String, String]
for (i <- 0 to internalCriteria.size-1) {
crits(internalCriteria.get(i).name) = internalCriteria.get(i).value
}
Map.empty ++ crits
}
// Stripped the equals, canEquals, hashCode, toString code to keep the code snippet short...
}
#PersistenceCapable
#EmbeddedOnly
class Criteria(uc: UserCriteria, nm: String, vl: String) {
#Persistent
var userCriteria = uc
#Persistent
var name = nm
#Persistent
var value = vl
override def toString = {
"Criteria name: " + name + " value: " + value
}
}
Any ideas why the childs are not stored? Or why I get the error message?
Thanks,
Gero
It looks to me like you are trying to implement an owned one-to-many relationship. Also you seem to have forgotten the #Embedded annotation for the UserCriteria's internalCriteria field, though I think, that it might still not work since the field contains a list and not the embedded class itself.