Santiago
Santiago

Reputation: 982

Access entity within Lift Scala net.liftweb.util.Box object

I have a net.liftweb.util.Box object which is returned as a result of a search on a user id. The box object returns something like:

net.liftweb.util.Box[MyEntity{attributes}]

How can I access the entity within the Box? In particular, I want to access the attributes of the entity inside the box.

Upvotes: 2

Views: 383

Answers (2)

Byambatsogt
Byambatsogt

Reputation: 324

If you want execute some operation on the object on box dmap is helpful.

For example:

fooBox.dmap("Yo!")(foo => 
  // dome some staff with foo 
  result // must be string
)

You can get the element directly by calling fooBox.open_! but it's not recommended. It often raises null pointer exception when box is empty.

Upvotes: 1

David Pollak
David Pollak

Reputation: 7015

Please see http://lift.la/scala-option-lift-box-and-how-to-make-your-co

You can access the contents of a Box within a foreach/map method invocation. For example if you wanted to get the name out of your entity:

myBoxedEntity.map(_.name) openOr "No Name Provided"

If the Box is Full, you'll get the entity name otherwise, you'll get the message.

Upvotes: 5

Related Questions