diff --git a/Sources/LexiconGenKit/Generator/Generator+Builders.swift b/Sources/LexiconGenKit/Generator/Generator+Builders.swift index 9f71ca5..3b7d0fc 100644 --- a/Sources/LexiconGenKit/Generator/Generator+Builders.swift +++ b/Sources/LexiconGenKit/Generator/Generator+Builders.swift @@ -80,6 +80,7 @@ public extension Generator { if let parameters = method.parameters { try objectSyntax( name: "Parameters", + modifiers: ["public"], inheritances: ["XRPCRequestParametersConvertible"], object: parameters ) { @@ -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 + } + """ + ) + } } } @@ -111,6 +120,7 @@ public extension Generator { case .object(let object): try objectSyntax( name: "Input", + modifiers: ["public"], inheritances: ["Encodable"], object: object ) @@ -124,6 +134,7 @@ public extension Generator { case .object(let object): try objectSyntax( name: "Output", + modifiers: ["public"], inheritances: ["Decodable", "Hashable"], object: object ) @@ -175,13 +186,15 @@ public extension Generator { @MemberBlockItemListBuilder static func objectSyntax( name: String, + modifiers: [String] = [], inheritances: [String] = [], object: LexiconObjectSchema, @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) diff --git a/Tests/LexiconGenKitTests/Generator/Generator+BuildersTests.swift b/Tests/LexiconGenKitTests/Generator/Generator+BuildersTests.swift index dd6cf9b..46e0275 100644 --- a/Tests/LexiconGenKitTests/Generator/Generator+BuildersTests.swift +++ b/Tests/LexiconGenKitTests/Generator/Generator+BuildersTests.swift @@ -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 @@ -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 @@ -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( @@ -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