Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Demo Codable Enum issue #1124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Tests/SQLiteTests/QueryIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ class QueryIntegrationTests: SQLiteTestCase {
XCTAssertNil(values[0].sub?.sub)
}

func test_select_codable_camelCase() throws {
let table = Table("codable_enum")
let column = Expression<StringEnum>("camelCaseString")
try db.run(table.create { builder in
builder.column(column)
})
let value = CamelCaseStruct(camelCaseString: .one)
try db.run(table.insert(value))
try db.run(table.insert(column <- .one))

let rows = try db.prepare(table)
let values: [CamelCaseStruct] = try rows.map({ try $0.decode() })
XCTAssertEqual(values.count, 2)
XCTAssertEqual(values[0].camelCaseString, .one)
XCTAssertEqual(values[1].camelCaseString, .one)
}

func test_scalar() {
XCTAssertEqual(0, try! db.scalar(users.count))
XCTAssertEqual(false, try! db.scalar(users.exists))
Expand Down
21 changes: 21 additions & 0 deletions Tests/SQLiteTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,24 @@ class TestCodable: Codable, Equatable {
lhs.sub == rhs.sub
}
}

enum StringEnum: String {
case one
case nope
}
extension StringEnum: Codable {}
struct CamelCaseStruct: Codable {
var camelCaseString: StringEnum
}
extension StringEnum: Value {
public typealias Datatype = String
public static var declaredDatatype: String = String.declaredDatatype

public static func fromDatatypeValue(_ datatypeValue: String) -> StringEnum {
return StringEnum(rawValue: datatypeValue) ?? .nope
}

public var datatypeValue: String {
return self.rawValue
}
}