Skip to content

Commit 7f15e42

Browse files
committed
public inits for input structs
1 parent 239f983 commit 7f15e42

4 files changed

Lines changed: 145 additions & 1 deletion

File tree

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,14 @@ public struct SwiftLanguage: Language {
527527
rowDecodableInit(for: model)
528528
writer.blankLine()
529529
memberWiseInit(for: model)
530+
} else {
531+
// Input structs only get a memberwise init so they can be
532+
// constructed directly, including from outside the module when
533+
// the generated code is `public`.
534+
writer.blankLine()
535+
memberWiseInit(for: model)
530536
}
531-
537+
532538
if addDynamicLookup {
533539
for (fieldName, table, isOptional) in dynamicLookupTables {
534540
dynamicMemberLookup(

Tests/CompilerTests/Gen/Swift.output

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ struct InsertFooReturningFooInput: Hashable, Sendable {
7474
let dateWithAdapterNotNull: Date
7575
let dateWithAdapterNullable: Date?
7676
let dateWithCustomAdapter: Date?
77+
78+
init(
79+
textNotNull: String,
80+
textNullable: String?,
81+
dateWithAdapterNotNull: Date,
82+
dateWithAdapterNullable: Date?,
83+
dateWithCustomAdapter: Date?
84+
) {
85+
self.textNotNull = textNotNull
86+
self.textNullable = textNullable
87+
self.dateWithAdapterNotNull = dateWithAdapterNotNull
88+
self.dateWithAdapterNullable = dateWithAdapterNullable
89+
self.dateWithCustomAdapter = dateWithCustomAdapter
90+
}
7791
}
7892

7993
struct InsertFooReturningFooOutput: Hashable, Sendable, RowDecodableWithAdapters {
@@ -123,11 +137,27 @@ struct InsertFooReturningFooOutput: Hashable, Sendable, RowDecodableWithAdapters
123137
struct InsertBarReturningIntPkInput: Hashable, Sendable {
124138
let customNameIntPk: Int
125139
let barNotNullText: String
140+
141+
init(
142+
customNameIntPk: Int,
143+
barNotNullText: String
144+
) {
145+
self.customNameIntPk = customNameIntPk
146+
self.barNotNullText = barNotNullText
147+
}
126148
}
127149

128150
struct InsertBarReturningExtraColumnInput: Hashable, Sendable {
129151
let intPk: Int
130152
let barNotNullText: String
153+
154+
init(
155+
intPk: Int,
156+
barNotNullText: String
157+
) {
158+
self.intPk = intPk
159+
self.barNotNullText = barNotNullText
160+
}
131161
}
132162

133163
struct InsertBarReturningExtraColumnOutput: Hashable, Sendable, RowDecodable {
@@ -212,11 +242,27 @@ struct BothColumnsShouldNotBeNullableOutput: Hashable, Sendable, RowDecodable {
212242
struct SelectWithManyInputsInput: Hashable, Sendable {
213243
let intPk: Int
214244
let textNotNull: String
245+
246+
init(
247+
intPk: Int,
248+
textNotNull: String
249+
) {
250+
self.intPk = intPk
251+
self.textNotNull = textNotNull
252+
}
215253
}
216254

217255
struct InputContainsArrayInput: Hashable, Sendable {
218256
let intPks: [Int]
219257
let barNotNullText: String
258+
259+
init(
260+
intPks: [Int],
261+
barNotNullText: String
262+
) {
263+
self.intPks = intPks
264+
self.barNotNullText = barNotNullText
265+
}
220266
}
221267

222268
struct QueriesQueries: PureSQL.ConnectionWrapper, Sendable {

Tests/CompilerTests/Gen/SwiftPublic.output

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public struct InsertFooReturningFooInput: Hashable, Sendable {
7474
public let dateWithAdapterNotNull: Date
7575
public let dateWithAdapterNullable: Date?
7676
public let dateWithCustomAdapter: Date?
77+
78+
public init(
79+
textNotNull: String,
80+
textNullable: String?,
81+
dateWithAdapterNotNull: Date,
82+
dateWithAdapterNullable: Date?,
83+
dateWithCustomAdapter: Date?
84+
) {
85+
self.textNotNull = textNotNull
86+
self.textNullable = textNullable
87+
self.dateWithAdapterNotNull = dateWithAdapterNotNull
88+
self.dateWithAdapterNullable = dateWithAdapterNullable
89+
self.dateWithCustomAdapter = dateWithCustomAdapter
90+
}
7791
}
7892

7993
public struct InsertFooReturningFooOutput: Hashable, Sendable, RowDecodableWithAdapters {
@@ -123,11 +137,27 @@ public struct InsertFooReturningFooOutput: Hashable, Sendable, RowDecodableWithA
123137
public struct InsertBarReturningIntPkInput: Hashable, Sendable {
124138
public let customNameIntPk: Int
125139
public let barNotNullText: String
140+
141+
public init(
142+
customNameIntPk: Int,
143+
barNotNullText: String
144+
) {
145+
self.customNameIntPk = customNameIntPk
146+
self.barNotNullText = barNotNullText
147+
}
126148
}
127149

128150
public struct InsertBarReturningExtraColumnInput: Hashable, Sendable {
129151
public let intPk: Int
130152
public let barNotNullText: String
153+
154+
public init(
155+
intPk: Int,
156+
barNotNullText: String
157+
) {
158+
self.intPk = intPk
159+
self.barNotNullText = barNotNullText
160+
}
131161
}
132162

133163
public struct InsertBarReturningExtraColumnOutput: Hashable, Sendable, RowDecodable {
@@ -212,11 +242,27 @@ public struct BothColumnsShouldNotBeNullableOutput: Hashable, Sendable, RowDecod
212242
public struct SelectWithManyInputsInput: Hashable, Sendable {
213243
public let intPk: Int
214244
public let textNotNull: String
245+
246+
public init(
247+
intPk: Int,
248+
textNotNull: String
249+
) {
250+
self.intPk = intPk
251+
self.textNotNull = textNotNull
252+
}
215253
}
216254

217255
public struct InputContainsArrayInput: Hashable, Sendable {
218256
public let intPks: [Int]
219257
public let barNotNullText: String
258+
259+
public init(
260+
intPks: [Int],
261+
barNotNullText: String
262+
) {
263+
self.intPks = intPks
264+
self.barNotNullText = barNotNullText
265+
}
220266
}
221267

222268
public struct QueriesQueries: PureSQL.ConnectionWrapper, Sendable {

Tests/CompilerTests/Gen/SwiftWithPattern.output

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ struct InsertFooReturningFooInput: Hashable, Sendable {
7474
let dateWithAdapterNotNull: Date
7575
let dateWithAdapterNullable: Date?
7676
let dateWithCustomAdapter: Date?
77+
78+
init(
79+
textNotNull: String,
80+
textNullable: String?,
81+
dateWithAdapterNotNull: Date,
82+
dateWithAdapterNullable: Date?,
83+
dateWithCustomAdapter: Date?
84+
) {
85+
self.textNotNull = textNotNull
86+
self.textNullable = textNullable
87+
self.dateWithAdapterNotNull = dateWithAdapterNotNull
88+
self.dateWithAdapterNullable = dateWithAdapterNullable
89+
self.dateWithCustomAdapter = dateWithCustomAdapter
90+
}
7791
}
7892

7993
struct InsertFooReturningFooOutput: Hashable, Sendable, RowDecodableWithAdapters {
@@ -123,11 +137,27 @@ struct InsertFooReturningFooOutput: Hashable, Sendable, RowDecodableWithAdapters
123137
struct InsertBarReturningIntPkInput: Hashable, Sendable {
124138
let customNameIntPk: Int
125139
let barNotNullText: String
140+
141+
init(
142+
customNameIntPk: Int,
143+
barNotNullText: String
144+
) {
145+
self.customNameIntPk = customNameIntPk
146+
self.barNotNullText = barNotNullText
147+
}
126148
}
127149

128150
struct InsertBarReturningExtraColumnInput: Hashable, Sendable {
129151
let intPk: Int
130152
let barNotNullText: String
153+
154+
init(
155+
intPk: Int,
156+
barNotNullText: String
157+
) {
158+
self.intPk = intPk
159+
self.barNotNullText = barNotNullText
160+
}
131161
}
132162

133163
struct InsertBarReturningExtraColumnOutput: Hashable, Sendable, RowDecodable {
@@ -212,11 +242,27 @@ struct BothColumnsShouldNotBeNullableOutput: Hashable, Sendable, RowDecodable {
212242
struct SelectWithManyInputsInput: Hashable, Sendable {
213243
let intPk: Int
214244
let textNotNull: String
245+
246+
init(
247+
intPk: Int,
248+
textNotNull: String
249+
) {
250+
self.intPk = intPk
251+
self.textNotNull = textNotNull
252+
}
215253
}
216254

217255
struct InputContainsArrayInput: Hashable, Sendable {
218256
let intPks: [Int]
219257
let barNotNullText: String
258+
259+
init(
260+
intPks: [Int],
261+
barNotNullText: String
262+
) {
263+
self.intPks = intPks
264+
self.barNotNullText = barNotNullText
265+
}
220266
}
221267

222268
struct QueriesQueries: PureSQL.ConnectionWrapper, Sendable {

0 commit comments

Comments
 (0)