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

Double quotes when using ULID #1253

Open
pravdomil opened this issue Apr 6, 2024 · 1 comment
Open

Double quotes when using ULID #1253

pravdomil opened this issue Apr 6, 2024 · 1 comment

Comments

@pravdomil
Copy link

pravdomil commented Apr 6, 2024

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

extension ULID: Value {
    public static var declaredDatatype: String {
        String.declaredDatatype
    }

    public static func fromDatatypeValue(_ stringValue: String) -> ULID {
        ULID(ulidString: stringValue)!
    }

    public var datatypeValue: String {
        ulidString
    }
}

struct User: Codable {
    let id: ULID
}

let users = Table("users")
let id = Expression<ULID>("id")
try print(users.insert(User(id: ULID())))

gives me

INSERT INTO "users" ("id") VALUES ('"01HTTP6QD08BP47AJBEEEWXVJ9"')
@pravdomil
Copy link
Author

func encode<T>(_ value: T, forKey key: Key) throws where T: Swift.Encodable {
switch value {
case let data as Data:
encoder.setters.append(Expression(key.stringValue) <- data)
case let date as Date:
encoder.setters.append(Expression(key.stringValue) <- date.datatypeValue)
case let uuid as UUID:
encoder.setters.append(Expression(key.stringValue) <- uuid.datatypeValue)
default:
let encoded = try JSONEncoder().encode(value)
let string = String(data: encoded, encoding: .utf8)
encoder.setters.append(Expression(key.stringValue) <- string)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant