I see, so you message contains routing-related keys, and module related keys, both keys are on the same level.
You want to extract all routing-related keys, remove it, then pass the rest onto the next part of the program.
If the structure on module-related and routing-related keys are fixed. I’d suggest that you include both in a single Codable
anyway. Codable
is designed so that you want to have one struct for each point of conversion between data, and internal representation, reusing some sub-structure if possible. Something like this
struct FullMessageData {
// Both routing data & module data
}
struct RoutingData {
// Routing data only
}
struct ModuleData {
// Module data only
}
let fullMessage: FullMessageData = ...
let routingData = fullMessage.routingData
let moduleData = fullMessage.moduleData
If the message logic makes it too cumbersome. Another way is to parse the data twice, once as RoutingData
, again as ModuleData
.
Because you’re not done with parsing when in routing progam, you’ll want to pass the entire message to the module, and have it ignore the routing keys.
struct RoutingData { ... }
struct ModuleData { ... }
let routingData = JSONDecoder.decode(RoutingData.self, ...)
let moduleData = JSONDecoder.decode(ModuleData.self, ...)
If all else fails, you can still use JSONSerialization which IMO isn’t exactly Swifty, but does exactly what you need.