levibostian / iOSBlanky

My opinionated iOS app boilerplate
MIT License
6 stars 5 forks source link

Sourcery auto generate VO + model from protocol #37

Closed levibostian closed 4 years ago

levibostian commented 4 years ago

When using this new concept of how to update existing app DB models with new data, I have a VO codable object that represents the JSON structure of an API network response body, a model that has all properties of JSON structure plus some optional local-only properties, and a protocol that represents all of the properties that the VO and model have in common.

I would love to have sourcery generate the VO and maybe even the model from a protocol for me.

    protocol _FolderItem: Equatable, AutoVo {
        associatedtype FolderItem

        var id: String { get }
        var contents: [FolderItem]? { get }
    }

The sourcery template would generate automatically:

    struct FolderItemVo: _FolderItem {
        typealias FolderItem = FolderItemVo

        var id: String { get }
        var contents: [FolderItem]? { get }
    }

    struct FolderItemModel: _FolderItem {
        typealias FolderItem = FolderItemModel

        var id: String { get }
        var contents: [FolderItem]? { get }
    }

I can then use optional annotations for naming the associatedValues. I can also use extensions for the model if I want to add optional local-only properties.

levibostian commented 4 years ago

Not going to do this. I am not using a protocol anymore for VO objects to respect. I am creating a model and a VO separately. It works well because since you must convert from one to the other, that's the oppourtunity to make sure there are not any properties missing.