Git Product home page Git Product logo

Comments (1)

nixzhu avatar nixzhu commented on June 16, 2024

Baby can not infer all the Any? type and data which is a array with different type object in it.

You can modify the output struct as follow:

  1. Specify the type of limit, transform, columnIndex, collapse and order.
  2. Create a MyData enum to wrap the data's object (It's a little bit complicated).

Such as:

enum DecodingError: Error {
    case typeMismatch
}

enum MyData: Codable {
    case string(String)
    case double(Double)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        if let x = try? container.decode(Double.self) {
            self = .double(x)
            return
        }
        throw DecodingError.typeMismatch
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .string(let x):
            try container.encode(x)
        case .double(let x):
            try container.encode(x)
        }
    }
}

struct Aluminum: Codable {
    struct Dataset: Codable {
        let id: Int
        let datasetCode: String
        let databaseCode: String
        let name: String
        let description: String
        let refreshedAt: Date
        let newestAvailableDate: Date
        let oldestAvailableDate: Date
        let columnNames: [String]
        let frequency: String
        let type: String
        let premium: Bool
        let limit: Int?
        let transform: Int?
        let columnIndex: Int?
        let startDate: Date
        let endDate: Date
        let data: [[MyData]]
        let collapse: Int?
        let order: Int?
        let databaseId: Int
        private enum CodingKeys: String, CodingKey {
            case id
            case datasetCode = "dataset_code"
            case databaseCode = "database_code"
            case name
            case description
            case refreshedAt = "refreshed_at"
            case newestAvailableDate = "newest_available_date"
            case oldestAvailableDate = "oldest_available_date"
            case columnNames = "column_names"
            case frequency
            case type
            case premium
            case limit
            case transform
            case columnIndex = "column_index"
            case startDate = "start_date"
            case endDate = "end_date"
            case data
            case collapse
            case order
            case databaseId = "database_id"
        }
    }
    let dataset: Dataset
}

Baby can not guarantee that it is always correct, but it can save your time to create the base struct.

from baby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.