Skip to content

Commit

Permalink
Define parameters as let if no parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
andooown committed Oct 21, 2023
1 parent 4d50233 commit 9a36c33
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Sources/LexiconGenKit/Generator/Generator+Builders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,24 @@ public extension Generator {
return "parameters.append(contentsOf: \(k).toQueryItems(name: \"\(k)\"))"
}

try VariableDeclSyntax(
"""
public var queryItems: [URLQueryItem] {
var parameters = [URLQueryItem]()
\(raw: params.joined(separator: "\n"))
return parameters
}
"""
)
if params.isEmpty {
try VariableDeclSyntax(
"""
public let queryItems: [URLQueryItem] = []
"""
)
} else {
try VariableDeclSyntax(
"""
public var queryItems: [URLQueryItem] {
var parameters = [URLQueryItem]()
\(raw: params.joined(separator: "\n"))
return parameters
}
"""
)
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions Tests/LexiconGenKitTests/Generator/Generator+BuildersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,46 @@ final class GeneratorBuildersTests: XCTestCase {
)

// query
XCTAssertNoDifference(
try Generator.definition(
makeDefinition(
.query(
LexiconMethodSchema(
parameters: LexiconObjectSchema(properties: [:], required: nil),
input: nil,
output: .object(LexiconObjectSchema(properties: [:], required: nil))
)
)
)
).formatted().description,
"""
struct Foo: XRPCRequest {
public struct Parameters: XRPCRequestParametersConvertible {
public init(
) {
}
public let queryItems: [URLQueryItem] = []
}
public struct Output: Decodable, Hashable {
public init(
) {
}
}
public init(
parameters: Parameters
) {
self.parameters = parameters
}
public let type = XRPCRequestType.query
public let requestIdentifier = "com.example.foo"
public let parameters: Parameters
}
"""
)
XCTAssertNoDifference(
try Generator.definition(
makeDefinition(
Expand Down

0 comments on commit 9a36c33

Please sign in to comment.