Kitura / Swift-Kuery-ORM

An ORM for Swift, built on Codable
Apache License 2.0
212 stars 30 forks source link

keyNotFound issue when variable name using camel case. #90

Closed diuming closed 5 years ago

diuming commented 5 years ago

Swift-Kuery-ORM: 0.4.1 PostgreSQL: 10.6

I don't know what happened in version 0.4.1. The variable isActivated is a Bool type. (variable name using camel case)

get all users

 do {
    let users = try User.getUsers()
    print(users)
} catch {
     print(error)
}

error: 702 : keyNotFound(CodingKeys(stringValue: "isActivated", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "isActivated", intValue: nil)], debugDescription: "No value for property with this key", underlyingError: nil))

swift code

struct User: Codable {
    static var tableName =  "Users"
    var id: String =  UUID.init().uuidString
    var name: String
    var password: String
    var email: String
    var isActivated: Bool = false
    var isLoggedIn: Bool = false

    init (name: String, password: String, email: String, isActivated: Bool, isLoggedIn: Bool) {
        self.name = name
        self.password = password
        self.email = email
        self.isActivated = isActivated
        self.isLoggedIn = isLoggedIn
    }
}

extension User: Model {
    static func getUsers() throws -> [User] {
        var reqError = RequestError.notFound
        var reqUsers: [User]?
        User.findAll { (users, error) in
            if let e = error {
                reqError = e
                print("error: \(e)")
            } else {
                reqUsers = users
            }
        }
        if let users = reqUsers {
            return users
        } else {
            throw reqError
        }
    }
}
kilnerm commented 5 years ago

I suspect this is related to the changes in Kuery 3.0 regarding respecting case in table definitions. Will do some further investigation.

kilnerm commented 5 years ago

I have merged a fix for this issue into master. It will be available in the next release.