stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

Unable to decode optional custom type #1254

Closed pravdomil closed 2 months ago

pravdomil commented 2 months ago

Build Information

    {
      "identity" : "sqlite.swift",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/stephencelis/SQLite.swift",
      "state" : {
        "revision" : "e78ae0220e17525a15ac68c697a155eb7a672a8e",
        "version" : "0.15.0"
      }
    }

How to Reproduce

package.swift

// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyExecutable",
    dependencies: [
        .package(url: "https://github.com/stephencelis/SQLite.swift", .upToNextMajor(from: "0.15.0")),
        .package(url: "https://github.com/yaslab/ULID.swift", .upToNextMinor(from: "1.2.0")),
    ], targets: [
        .executableTarget(
            name: "MyExecutable", dependencies: [
                .product(name: "SQLite", package: "SQLite.swift"),
                .product(name: "ULID", package: "ULID.swift"),
            ]
        ),
    ]
)

main.swift

import Foundation
import SQLite
import ULID

struct User: Codable {
    let id: UUID
    let name: Name?
}

struct Name: Codable {
    let first: String
    let last: String
}

let users = Table("users")
let id = Expression<UUID>("id")
let name = Expression<String?>("name")

do {
    let db = try Connection("db.sqlite3")

    try db.run(users.create(ifNotExists: true) { t in
        t.column(id, primaryKey: true)
        t.column(name)
    })

    try print(db.run(users.insert(User(id: UUID(), name: nil))))
    try print(db.prepare(users).map { row in try row.decode() } as [User])

} catch {
    print(error)
}

gives

typeMismatch(MyExecutable.Name, Swift.DecodingError.Context(codingPath: [], debugDescription: "an unsupported type was found", underlyingError: nil))
pravdomil commented 2 months ago

https://github.com/stephencelis/SQLite.swift/blob/e78ae0220e17525a15ac68c697a155eb7a672a8e/Sources/SQLite/Typed/Coding.swift#L498-L517

nathanfallet commented 2 months ago

This is a duplicate of #1247 and was fixed in #1248 (released in 0.15.1)