Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Is there a way to create adhoc types conforming to protocols #81

Open
avnerbarr opened this issue Feb 29, 2024 · 0 comments
Open

Is there a way to create adhoc types conforming to protocols #81

avnerbarr opened this issue Feb 29, 2024 · 0 comments

Comments

@avnerbarr
Copy link

avnerbarr commented Feb 29, 2024

I have a use case where there can be multiple datasources accessed through a strategy pattern. Those strategies return similar objects (json from the network).

Internally they handle the data differently and can't share the same data model, but for the functionality using those API's this doesn't really matter.

I'd like to "proxy" the data behind some protocol which is backed by the JSON data itself. Is there a good pattern for that?

I'll share an example:

protocol EventType {
   associatedtype PointType: DataPointType
   var timestamp: Int {get}
   var value: Double {get}
   var data : PointType
}

protocol DataPointType {
   var field1: Int {get}
   var field2: String {get}
    // etc. etc.
}

   .... get some data as json ....
   let jsonData: AnyCodable = .... get the data from somewhere .... there are many internal types and don't want to craft custom types for each one
  // jsonData structure adheres to EventType .... 
   /// I need a concrete dynamic object conforming to the EventType  

// some sort of generic object which implements the EventType protocol
let typedData = Proxy<EventType>(jsonData) // behave like the protocol with the backing jsonData and I don't want to create. a concrete type implementing EventType
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant