Skip to content

Instantly share code, notes, and snippets.

@shatestest
Last active November 5, 2018 12:22
Show Gist options
  • Save shatestest/fdeaba767d78e171bb6c08b359fbd1bf to your computer and use it in GitHub Desktop.
Save shatestest/fdeaba767d78e171bb6c08b359fbd1bf to your computer and use it in GitHub Desktop.
object {
@throws(classOf[Exception])
def process(ymlFilename:String):Option[QueryEntities] = {
val ymlFilePath = URLDecoder.decode(ymlFilename, "UTF-8")
Try(Source.fromFile(ymlFilePath).mkString.parseYaml) match {
case Success(yml) => {
println("Parsed")
import CustomYamlProtocol._
try{ //some logic
println( " Input yml :: \n " + yml.prettyPrint)
val eties:QueryEntities = yml.convertTo[QueryEntities]
/* for (e: Entity <- eties.entities) {
println(" columnFamily : " + e.columnFamily)
println(" fromDate : " + e.fromDate)
println(f" toDate : " + e.toDate)
}*/
println("done")
Some(eties)
//Some(yml.convertTo[QueryEntities])
}catch{
case e:Exception => e.printStackTrace()
//throw e
None
}
//evalYml(yml);
}
case Failure(error) => {
error.printStackTrace()
println("Unable to parse YAML")
None
}
}
}
}
case class QueryEntity(columnFamily: String, fromDate: Option[String], toDate: Option[String])
case class QueryEntities(entities: List[QueryEntity])
object Tester{
def main(args:Array[String]) {
val ymlFilename ="some.yml";
val entities: Option[QueryEntities] = InputYamlProcessor.process(ymlFilename)
for( e: QueryEntities <- entities ){
/// this is not working
//How to access the columnFamily, fromData and toDate ?
}
}
}
@shatestest
Copy link
Author

shatestest commented Nov 5, 2018

If I say like below
val entities:QueryEntities = InputYamlProcessor.process(ymlFilename)
//ERROR type mismatch; found : Option[com.snp.helpers.Model.QueryEntities] required: com.snp.helpers.Model.QueryEntities

for( e: QueryEntity <- entities ){ //error value filter is not a member of com.snp.helpers.Model.QueryEntities
e.columnFamily
}

@AndriiStefaniv
Copy link

Try it:

def main(args:Array[String]) {
    val ymlFilename ="some.yml";
  
  InputYamlProcessor.process(ymlFilename) match {
     case Some(entities) => println(entities.columnFamily)
     case None => println("error")
  }
}

@phongngtuan
Copy link

try remove the type on e

scala> case class Foo(s: String)
defined class Foo
scala> val a: Option[Foo] = Option(Foo("Hello"))
a: Option[Foo] = Some(Foo(Hello))
scala> for (x <- a) { println(x) }
Foo(Hello)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment