Skip to content

Commit

Permalink
Merge pull request #10 from andooown/warning
Browse files Browse the repository at this point in the history
Suppress warnings on the generated file
  • Loading branch information
andooown authored Oct 21, 2023
2 parents 524389a + 9a36c33 commit fe53757
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
35 changes: 24 additions & 11 deletions Sources/LexiconGenKit/Generator/Generator+Builders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public extension Generator {
if let parameters = method.parameters {
try objectSyntax(
name: "Parameters",
modifiers: ["public"],
inheritances: ["XRPCRequestParametersConvertible"],
object: parameters
) {
Expand All @@ -93,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 All @@ -111,6 +120,7 @@ public extension Generator {
case .object(let object):
try objectSyntax(
name: "Input",
modifiers: ["public"],
inheritances: ["Encodable"],
object: object
)
Expand All @@ -124,6 +134,7 @@ public extension Generator {
case .object(let object):
try objectSyntax(
name: "Output",
modifiers: ["public"],
inheritances: ["Decodable", "Hashable"],
object: object
)
Expand Down Expand Up @@ -175,13 +186,15 @@ public extension Generator {
@MemberBlockItemListBuilder
static func objectSyntax(
name: String,
modifiers: [String] = [],
inheritances: [String] = [],
object: LexiconObjectSchema<LexiconAbsoluteReference>,
@MemberBlockItemListBuilder additionalBody: () throws -> MemberBlockItemListSyntax = { MemberBlockItemListSyntax([]) }
) throws -> MemberBlockItemListSyntax {
let modifier = modifiers.isEmpty ? "" : modifiers.joined(separator: " ") + " "
let inherit = inheritances.isEmpty ? "" : ": " + inheritances.joined(separator: ", ")

try StructDeclSyntax("public struct \(raw: name)\(raw: inherit)") {
try StructDeclSyntax("\(raw: modifier)struct \(raw: name)\(raw: inherit)") {
try Generator.objectPropertiesSyntax(object.properties, required: object.required)
Generator.objectInitializerSyntax(object.properties, required: object.required)

Expand Down
46 changes: 43 additions & 3 deletions Tests/LexiconGenKitTests/Generator/Generator+BuildersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ final class GeneratorBuildersTests: XCTestCase {
)
).formatted().description,
"""
public struct Foo: UnionCodable, Hashable {
struct Foo: UnionCodable, Hashable {
@Indirect
public var optionalValue: String?
@Indirect
Expand Down Expand Up @@ -216,7 +216,7 @@ final class GeneratorBuildersTests: XCTestCase {
)
).formatted().description,
"""
public struct Foo: UnionCodable, Hashable {
struct Foo: UnionCodable, Hashable {
@Indirect
public var optionalValue: String?
@Indirect
Expand All @@ -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 Expand Up @@ -408,7 +448,7 @@ final class GeneratorBuildersTests: XCTestCase {
}
).formatted().description,
"""
public struct Object: Protocol1, Protocol2 {
struct Object: Protocol1, Protocol2 {
@Indirect
public var bar: Int?
@Indirect
Expand Down

0 comments on commit fe53757

Please sign in to comment.