0
votes

I am trying to get value in Map field. If value is not found, then default to "== 1". But getting an error

val arg = args.asInstanceOf[Map[String,String]]
println(arg)
val Pattern = "(<[=>]?|==|>=?|\\&\\&|\\|\\|) (\\d*\\.?\\d+)".r
var result =  arg.get("condition")
result = Option(result.getOrElse("== 1"))
val Pattern(condition,chk_val) = result.toString

I was getting error in line val Pattern(condition,chk_val) = result.toString Solved it by adding .fold("")(_.toString)

Map(column -> ACCOUNT_NUMBER, Pattern -> [0-9]{8}))

scala.MatchError: Some(== 1) (of class java.lang.String)

2
That was a print statement. Modified the code. - Arvinth

2 Answers

4
votes

I would avoid using var for result and instead have something like

val arg = args.asInstanceOf[Map[String, String]] // Would seriously consider removing the cast, .asInstanceOf is not typically idiomatic...
val Pattern = """(<[=>]?|==|>=?|&&|\|\|) (\d*\.?\d+)""".r
val result: String = arg.getOrElse("condition", "== 1")
val Pattern(condition, chk_val) = result

In general, if there's an Option that's always defined, there's not really any point in keeping it wrapped.

Triple-quoted strings (""") are really useful for regexes.

-1
votes

Converting Some(String) to String solved the issue.

 val arg = args.asInstanceOf[Map[String,String]]
 val Pattern = "(<[=>]?|==|>=?|\\&\\&|\\|\\|) (\\d*\\.?\\d+)".r
 var result =  arg.get("condition")
 result = Option(result.getOrElse("== 1"))
 val Pattern(condition,chk_val) = result.fold("")(_.toString)