Given an average REST API, sometimes the same model may be retrieved from different endpoints with different snippet.
For example, having this model:
struct User {
var firstname: String
var lastname: String
}
from one endpoint I may want to apply this mapping:
firstname <- map["firstname"]
lastname <- map["lastname"]
meanwhile from another endpoint I may need to use something like:
firstname <- map["first_name"]
lastname <- map["last_name"]
This may happen quiet often in the case where we are retrieving a resource which contains a nested object in the payload, sometimes (for some arcane reasons), the fields in the snippet may differ.
Now we may argue that those would be different models, or that a REST API shouldn't do this, but I can't figure out how I can somehow keep the model clean and define multiple mappings to use from different endpoints to generate the same kind of object.
Am I missing something here?
+1
+1
+1
This is an interesting scenario that I have not really considered.
I cannot think of a solution for this given the current code base.
Perhaps the Map
object, could carry some data that could be used to distinguish the type of mapping. I'll have to think about it some more. Any ideas or pull requests are welcomed
+1
I used to use RestKit which had this decoupling available but that library was just too heavy. I am going to dig in and see if I can come up with any thoughts to help out. A bit new to swift so it might take a bit :)
Any more thoughts on your end @tristanhimmelman ?
Anyway to change the map key before calling the map function?
I have a object with two JSON structure .
The main difference between an object mapper like this and the one used in RestKit is that in Objective-C you have reflection both ways (thanks to KVC), that made that possible. In Swift reflection is read-only and it's not possible to use the same approach.
make everything optional.....
func mapping(map: Map) {
detail <- map["detail"]
name <- map["Name"]
if let _ = name {
print("try another")
} else {
name <- map["name"]
}
projects <- map["projects"]
}
I don't know whether it's good.
+1
+1
I think using MappableCluster is good to deal with this.
Hi,
This subscript is convenience for me.
extension Map {
public subscript(keys: [String]) -> Map {
for key in keys {
let nested = key.containsString(".")
self[key, nested: nested]
if currentValue != nil { break }
}
return self
}
}
Usage:
firstname <- map[["firstname", "first_name"]]
But, I have a problem now.
In the case of Object -> JSON
mapping, which key is adequate? ("firstname" or "first_name")
P.S. I am a newcomer to this community, if this posting on this place is unsuitable, so sorry.
+1 PLEASE, really useful.
Keep it up man!
I've just added a new feature to ObjectMapper that should resolve this issue. You now have the ability to create a custom object which gets passed around during mapping. This object just needs to conform to the new MapContext
protocol (an empty protocol). You can store any information you want in this object and subsequently use it to make decisions about how to map any given class/struct. The MapContext
object will be passed around in the Map
object.
See documentation:
https://github.com/Hearst-DD/ObjectMapper#mapping-context
Most helpful comment
I've just added a new feature to ObjectMapper that should resolve this issue. You now have the ability to create a custom object which gets passed around during mapping. This object just needs to conform to the new
MapContext
protocol (an empty protocol). You can store any information you want in this object and subsequently use it to make decisions about how to map any given class/struct. TheMapContext
object will be passed around in theMap
object.See documentation:
https://github.com/Hearst-DD/ObjectMapper#mapping-context