Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Oct 26, 2024
2 parents e503fe3 + 83050ef commit da5d178
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension CodeGenerator {

case .literal, .variableReference, .variablePath,
.keyPathLookup, .keyPath, .functionCall, .selfInit, .typeInit,
.cast, .inlineClosureCall:
.cast, .inlineClosureCall, .closure:
return string(for: expression)
}
}
Expand Down Expand Up @@ -103,6 +103,12 @@ public extension CodeGenerator {
case .forceUnwrap(let expression):
return "\(string(for: expression, wrapIfComplex: true))!"

case .closure(let statements):
return nestedGeneration {
indentedCodeBlock(addSpaceIfMissing: false) {
generateStatements(statements)
}
}
case .inlineClosureCall(let statements):
return nestedGeneration {
indentedCodeBlock(endSuffix: "()", addSpaceIfMissing: false) {
Expand Down Expand Up @@ -143,6 +149,8 @@ public extension CodeGenerator {
append(string(for: expression))
case .varargs: // indent better
append(string(for: expression))
case .closure: // indent better
append(string(for: expression))
case .inlineClosureCall: // indent better
append(string(for: expression))
case .functionCall(let call):
Expand Down
31 changes: 22 additions & 9 deletions Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public indirect enum Expression: Equatable {
/// `a!`
case forceUnwrap(Expression)

/// `{ return 46 + 2 }`
/// `{ return 46 + 2 }` (w/o calling it)
case closure([ Statement ])

/// `{ return 46 + 2 }()` (calling it)
case inlineClosureCall([ Statement ])
}

Expand All @@ -82,12 +85,12 @@ public indirect enum Expression: Equatable {
*
* Used as the value for ``Expression/functionCall(_:)``.
*/
public struct FunctionCall: Equatable, Sendable {
public struct FunctionCall: Equatable {

/**
* A parameter passed as part of the function call.
*/
public struct Parameter: Equatable, Sendable {
public struct Parameter: Equatable {

/// The keyword/label of the parameter, can be `nil` if it is a wildcard
/// (unlabled) parameter.
Expand All @@ -105,7 +108,7 @@ public struct FunctionCall: Equatable, Sendable {
/**
* A trailing closure attached to a function call.
*/
public struct TrailingClosure: Equatable, Sendable {
public struct TrailingClosure: Equatable {

/// The parameter list of the trailing closure (e.g. `( a, b ) in`).
public let parameters: [ String ]
Expand Down Expand Up @@ -153,20 +156,27 @@ public struct FunctionCall: Equatable, Sendable {
public extension Expression {

/// `nil`
static var `nil` : Self { .literal(.nil) }
static let `nil` = Self.literal(.nil)
/// Bool `true`
static var `true` : Self { .literal(.true) }
static let `true` = Self.literal(.true)
/// Bool `false`
static var `false` : Self { .literal(.false) }

static let `false` = Self.literal(.false)

/// `$0`
static let closureArg0 = Self.raw("$0")

/// A literal integer (`42`).
@inlinable
static func integer(_ value: Int) -> Self { .literal(.integer(value)) }
/// A literal double (`13.37`).
@inlinable
static func double (_ value: Double) -> Self { .literal(.double (value)) }
/// A literal string (`"Them Bones"`).
@inlinable
static func string (_ value: String) -> Self { .literal(.string (value)) }

/// An array of `UInt8` integers (i.e. a data literal).
@inlinable
static func integerArray(_ value: [ UInt8 ]) -> Self {
.literal(.integerArray(value.map { Int($0) }))
}
Expand Down Expand Up @@ -282,5 +292,8 @@ public extension Expression {
}

#if swift(>=5.5)
extension Expression : Sendable {}
extension Expression : Sendable {}
extension FunctionCall : Sendable {}
extension FunctionCall.Parameter : Sendable {}
extension FunctionCall.TrailingClosure : Sendable {}
#endif
24 changes: 12 additions & 12 deletions Plugins/Libraries/LighterCodeGenAST/Nodes/TypeReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,38 @@ public indirect enum TypeReference: Equatable {
public extension TypeReference {

/// Swift `Int`.
static var int : TypeReference { .name("Int") }
static let int = TypeReference.name("Int")
/// Swift `String`.
static var string : TypeReference { .name("String") }
static let string = TypeReference.name("String")
/// Swift `Double`.
static var double : TypeReference { .name("Double") }
static let double = TypeReference.name("Double")
/// Swift `Bool`.
static var bool : TypeReference { .name("Bool") }
static let bool = TypeReference.name("Bool")

// common in SQLite
/// Swift `Int32`.
static var int32 : TypeReference { .name("Int32") }
static let int32 = TypeReference.name("Int32")
/// Swift `Int64`.
static var int64 : TypeReference { .name("Int64") }
static let int64 = TypeReference.name("Int64")
}

public extension TypeReference {
/// Swift `[ UInt8 ]`. Aka `Data`, w/o the need for Foundation.
static var uint8Array : TypeReference { .array(.name("UInt8")) }
static let uint8Array = TypeReference.array(.name("UInt8"))
}

public extension TypeReference {

/// A Foundation `URL`.
static var url : TypeReference { .name("URL") }
static let url = TypeReference.name("URL")
/// A Foundation `Decimal` number.
static var decimal : TypeReference { .name("Decimal") }
static let decimal = TypeReference.name("Decimal")
/// A Foundation `Date`.
static var date : TypeReference { .name("Date") }
static let date = TypeReference.name("Date")
/// A Foundation `Data`.
static var data : TypeReference { .name("Data") }
static let data = TypeReference.name("Data")
/// A Foundation `UUID`.
static var uuid : TypeReference { .name("UUID") }
static let uuid = TypeReference .name("UUID")
}

#if swift(>=5.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public extension DatabaseInfo {
var entities = [ EntityInfo ]()
entities.reserveCapacity(schema.tables.count + schema.views.count)

let dbName = schema.tables.contains(where: { $0.name == name }) ? "\(name)DB" : name
for table in schema.tables {
let isWithoutRowID = table.isTableWithoutRowID
let indices = schema.indices [table.name] ?? []
Expand Down Expand Up @@ -56,7 +57,7 @@ public extension DatabaseInfo {
entities.append(entity)
}

self.init(name: name, userVersion: schema.userVersion, entities: entities)
self.init(name: dbName, userVersion: schema.userVersion, entities: entities)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ extension EnlighterASTGenerator {
}
}

fileprivate static var defaultSQLiteDateFormatterExpression : Expression {
fileprivate static let defaultSQLiteDateFormatterExpression : Expression =
Expression.inlineClosureCall([
.let("formatter", is: .call(name: "DateFormatter")),
.set(instance: "formatter", "dateFormat",
Expand All @@ -613,5 +613,4 @@ extension EnlighterASTGenerator {
[ ("identifier", .string("en_US_POSIX"))])),
.return(.variable("formatter"))
])
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2024 ZeeZide GmbH.
//

import LighterCodeGenAST
Expand Down Expand Up @@ -194,7 +194,9 @@ extension EnlighterASTGenerator {
func generateSQLError(name: String = "SQLError") -> Struct {
return Struct(
public: options.public, name: name,
conformances: [.name("Swift.Error"), .name("Equatable")],
conformances: [
.name("Swift.Error"), .name("Equatable"), .name("Sendable")
],
variables: [
.let(public: options.public, "code" , .int32,
comment: "The SQLite3 error code (`sqlite3_errcode`)."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ extension EnlighterASTGenerator {

// MARK: - Helpers

fileprivate static var stepAndReturnError : [ Statement ] { [
fileprivate static let stepAndReturnError : [ Statement ] = [
.let("rc", is: .call(name: "sqlite3_step", .variable("statement"))),
.return(
.conditional(
Expand All @@ -157,7 +157,7 @@ extension EnlighterASTGenerator {
.variable("SQLITE_OK")
)
)
] }
]

private func prepareSQL(_ schemaSQLProperty: String, for entity: EntityInfo)
-> [ Statement ]
Expand All @@ -166,13 +166,13 @@ extension EnlighterASTGenerator {
schemaSQLProperty])) ]
+ Self.prepareSQL
}
static var prepareSQL : [ Statement ] { [
static let prepareSQL : [ Statement ] = [
// Could use `Self` for record attached funcs
.var("handle", type: .name("OpaquePointer?")),
.raw("guard sqlite3_prepare_v2(db, sql, -1, &handle, nil) == SQLITE_OK,"),
.raw(" let statement = handle else { return sqlite3_errcode(db) }"),
.raw("defer { sqlite3_finalize(statement) }")
] }
]

func functionName(for entity: EntityInfo, operation: String) -> String {
switch options.rawFunctions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2024 ZeeZide GmbH.
//

import LighterCodeGenAST
Expand Down Expand Up @@ -590,10 +590,10 @@ extension EnlighterASTGenerator {
!optional // but still yields an optional if dateformatter is nil
? .call(name: "\(database.name).dateFormatter?.string",
parameters: [ ( "from", ivar(propertyName) ) ])
: .flatMap(expression: ivar(propertyName), map: .call(
name: "\(database.name).dateFormatter?.string",
parameters: [ ( "from", .raw("$0") ) ]
))
: .flatMap(expression: ivar(propertyName), map: .closure([
.call(name: "\(database.name).dateFormatter?.string",
parameters: [ ( "from", .closureArg0 ) ])
]))
) ],
trailing: ( [ "s" ], [
.ifSwitch( (
Expand Down
8 changes: 6 additions & 2 deletions Sources/Lighter/Predicates/SQLColumnComparisonPredicate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2024 ZeeZide GmbH.
//

/**
Expand All @@ -20,7 +20,7 @@ public struct SQLColumnComparisonPredicate<L, R>: SQLPredicate
L.Value == R.Value
{

public enum ComparisonOperator: String, Sendable {
public enum ComparisonOperator: String {

/**
* Check whether the ``SQLColumn`` is the same like the other column
Expand Down Expand Up @@ -111,3 +111,7 @@ public struct SQLColumnComparisonPredicate<L, R>: SQLPredicate
builder.append(builder.sqlString(for: rhs))
}
}

#if swift(>=5.5)
extension SQLColumnComparisonPredicate.ComparisonOperator : Sendable {}
#endif
6 changes: 5 additions & 1 deletion Sources/Lighter/Predicates/SQLColumnValuePredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import struct Foundation.Data
*/
public struct SQLColumnValuePredicate<C: SQLColumn>: SQLPredicate {

public enum ComparisonOperator: String, Sendable {
public enum ComparisonOperator: String {

/**
* Check whether the ``SQLColumn`` is the same like the given value.
Expand Down Expand Up @@ -353,3 +353,7 @@ public struct SQLColumnValuePredicate<C: SQLColumn>: SQLPredicate {
}

private let specialLikeChars : Set<Character> = [ "_", "%", "'", "\"" ]

#if swift(>=5.5)
extension SQLColumnValuePredicate.ComparisonOperator : Sendable {}
#endif
9 changes: 3 additions & 6 deletions Sources/Lighter/Predicates/SQLTruePredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/
public struct SQLTruePredicate: SQLPredicate {

@inlinable
static var shared : SQLTruePredicate { SQLTruePredicate() }
public static let shared = SQLTruePredicate()

// MARK: - SQL Generation

Expand All @@ -34,10 +33,8 @@ extension SQLPredicate where Self == SQLTruePredicate {
*/
public struct SQLBoolPredicate: SQLPredicate {

@inlinable
public static var `true` : Self { Self(true) }
@inlinable
public static var `false` : Self { Self(false) }
public static let `true` = Self(true)
public static let `false` = Self(false)

public let value : Bool

Expand Down
5 changes: 4 additions & 1 deletion Sources/Lighter/Utilities/SQLError.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022-2023 ZeeZide GmbH.
// Copyright © 2022-2024 ZeeZide GmbH.
//

import func SQLite3.sqlite3_errcode
Expand Down Expand Up @@ -50,3 +50,6 @@ public struct SQLError: Swift.Error, Equatable {
self.message = sqlite3_errmsg(db).flatMap(String.init(cString:))
}
}
#if swift(>=5.5)
extension SQLError: Sendable {}
#endif
7 changes: 7 additions & 0 deletions Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,11 @@ final class ASTDatabaseStructGenerationTests: XCTestCase {
XCTAssertTrue(source.contains(
"[ Person.self, Address.self, AFancyTestTable.self ]"))
}

func testResolveConflictedDatabaseName() throws {
let name = try XCTUnwrap(Fixtures.addressSchema.tables.first?.name)
let dbInfo = DatabaseInfo(name: name, schema: Fixtures.addressSchema)

XCTAssertEqual(dbInfo.name, "\(name)DB")
}
}

0 comments on commit da5d178

Please sign in to comment.