segmentio / analytics-swift

The hassle-free way to add Segment analytics to your Swift app (iOS/tvOS/watchOS/macOS/Linux).
MIT License
102 stars 85 forks source link

Typewritter- struct is not created for some events definision #210

Closed rashidbinlatheef closed 1 year ago

rashidbinlatheef commented 1 year ago

Describe the bug

I have an event which does not have any properties. So when I try to create segment client for my iOS project using npx typewriter command the properties struct is not getting created. Instead it creates a dcitionary type alias

To Reproduce

my json event


{
            "createdAt": "2022-11-03T07:21:24.000Z",
            "deprecatedAt": "0001-01-01T00:00:00.000Z",
            "jsonSchema": {
                "$id": "event_creation_completed",
                "$schema": "http://json-schema.org/draft-07/schema#",
                "description": "User finishes the event creation process. Event is triggered when user clicks on the \"Create\" button on the top right of the Event Creation screen.",
                "eventMetadata": {
                    "name": "event_creation_completed",
                    "type": "TRACK"
                },
                "properties": {
                },
                "type": "object"
            },
            "key": "event_creation_completed",
            "type": "TRACK",
            "updatedAt": "2022-11-03T07:21:24.000Z",
            "version": 1
        }

Created code after running npx typewriter


typealias EventCreationCompleted = [String: JSONAny]

extension Dictionary where Key == String, Value == JSONAny {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(EventCreationCompleted.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

If I have some other events without any properties. eg: event_creation_began Then the typewriter will autogenerate method

extension Dictionary where Key == String, Value == JSONAny {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(EventCreationBegan.self, from: data)
    }

This will cause error while running.

Screenshots Screenshot 2023-03-27 at 7 56 37 PM Platform (please complete the following information):

oscb commented 1 year ago

Thanks for the report, we're aware of this and tracking it over at: https://github.com/segmentio/typewriter/issues/284