Ok, thought as much, that just struck me as a bit odd, having to do a map
just to get the value.
That's because you still think imperative style and not functional. With
map, You didn't just "get the value", you converted a Validation of the url
into a Validation of hmac without having to think about the Validation
being a Success or a Failure and got the potential error message
automatically conveyed for you.
Would be nice to have a method to get the content. A get/getOrElse in the
Option style?
get is a big No way! It's just a dangerous stuff that might produce a NPE,
being there for imperative developers.
getOrElse is a no too, as you'd lose the potential error message if the
Validation is a Failure. You can pattern match on Success and Failure if
you want.
You have to realize that Validation potentially contains more information
than just the success object.
If I have to get several values from a bag of session attributes then it
gets a bit abstruse.
Nope, that's actually very neat:
for {
a <- session("a").validation[A]
b <- session("b").validation[B]
c <- session("c").validation[C]
} yield doSomething(a, b, c)
BTW, first url could be even shorter
def url:Expression[String]=_("oid").map("/myurl/"+_)
Shorter doesn't mean more readable, coding is no character typing race
Ha!
Anyway, got my stuff working this way:
def urlString:(Session=>String)={
"/myurl/orderid_"+_("oid").as[Integer]
}
def url:Expression[String]=urlString(_)
def expectedHmac:Expression[String]={
session=>
hmacgen.generateHmac(
urlString(session),
"GET", x_date,
"").getHmac()
}
then
.get(url)
.header("Authorization",expectedHmac)
Well, as[Integer] might throw exceptions (for example, if oid is missing
for whatever reason), which would harm performance.
You're used to imperative style, and starting this way is fine, but I
advice at some point you try to wrap your mind around functional style and
use validate instead of as.