I need the User class to contain a map of strings and I saw MongoMapField in the api so I used it because it seemed appropriate. Here’s my code:
class User private () extends ProtoAuthUser[User] with ObjectIdPk[User] {
def meta = User
object oauth extends MongoMapField[String, String]("") {
def setToken(provider: String, token: String) {this.value += (provider -> token) }
def getToken(value: String) = {
if(!this.value.isEmpty) Full(this.value.get(value) match {
case Some(value) => value
case None => ""
})
else Empty
}
}
}
setToken in this case doesn’t work because MongoMapField is immutable (from my assumption) and the following code doesn’t work either:
def setToken(provider: String, token: String) { this.value = this.value + (provider -> token) }
Does anyone know what is the best way to create/update a map of strings in mongo that is also attached to the user?
Thanks =)
When dealing with immutable data structures, you need to think in terms of replacing the existing value with a new value instead of trying to modify the existing value.
So, to get your code to work, you would need to change it to: