Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make empty generated source files descriptive #2151

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,23 @@ struct IDLToStructuredSwiftTranslator: Translator {
}
}

let fileDescription = FileDescription(
topComment: .preFormatted(codeGenerationRequest.leadingTrivia),
imports: try self.makeImports(
let imports: [ImportDescription]
if codeGenerationRequest.services.isEmpty {
imports = []
codeBlocks.append(
CodeBlock(comment: .inline("This file contained no services."))
)
} else {
imports = try self.makeImports(
dependencies: codeGenerationRequest.dependencies,
accessLevel: accessLevel,
accessLevelOnImports: accessLevelOnImports
),
)
}

let fileDescription = FileDescription(
topComment: .preFormatted(codeGenerationRequest.leadingTrivia),
imports: imports,
codeBlocks: codeBlocks
)

Expand All @@ -87,7 +97,7 @@ struct IDLToStructuredSwiftTranslator: Translator {
return StructuredSwiftRepresentation(file: file)
}

private func makeImports(
internal func makeImports(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package is your friend here

dependencies: [Dependency],
accessLevel: SourceGenerator.Config.AccessLevel,
accessLevelOnImports: Bool
Expand Down
1 change: 1 addition & 0 deletions Sources/GRPCCodeGen/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public struct SourceGenerator: Sendable {
client: self.config.client,
server: self.config.server
)

let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)

return sourceFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Testing

@testable import GRPCCodeGen

extension StructuedSwiftTests {
extension StructuredSwiftTests {
@Suite("Client")
struct Client {
@Test(
Expand Down
200 changes: 200 additions & 0 deletions Tests/GRPCCodeGenTests/Internal/StructuredSwift+ImportTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2024, gRPC Authors All rights reserved.
* Copyright 2025, gRPC Authors All rights reserved.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Testing

@testable import GRPCCodeGen
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use a regular import if you use package appropriately

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 will do.


extension StructuredSwiftTests {
@Suite("Import")
struct Import {
static let translator = IDLToStructuredSwiftTranslator()

static let allAccessLevels: [SourceGenerator.Config.AccessLevel] = [
.internal, .public, .package,
]

@Test(
"import rendering",
arguments: allAccessLevels
)
func imports(accessLevel: SourceGenerator.Config.AccessLevel) throws {
var dependencies = [Dependency]()
dependencies.append(Dependency(module: "Foo", accessLevel: .public))
dependencies.append(
Dependency(
item: .init(kind: .typealias, name: "Bar"),
module: "Foo",
accessLevel: .internal
)
)
dependencies.append(
Dependency(
item: .init(kind: .struct, name: "Baz"),
module: "Foo",
accessLevel: .package
)
)
dependencies.append(
Dependency(
item: .init(kind: .class, name: "Bac"),
module: "Foo",
accessLevel: .package
)
)
dependencies.append(
Dependency(
item: .init(kind: .enum, name: "Bap"),
module: "Foo",
accessLevel: .package
)
)
dependencies.append(
Dependency(
item: .init(kind: .protocol, name: "Bat"),
module: "Foo",
accessLevel: .package
)
)
dependencies.append(
Dependency(
item: .init(kind: .let, name: "Baq"),
module: "Foo",
accessLevel: .package
)
)
dependencies.append(
Dependency(
item: .init(kind: .var, name: "Bag"),
module: "Foo",
accessLevel: .package
)
)
dependencies.append(
Dependency(
item: .init(kind: .func, name: "Bak"),
module: "Foo",
accessLevel: .package
)
)

let expected =
"""
\(accessLevel.level) import GRPCCore
public import Foo
internal import typealias Foo.Bar
package import struct Foo.Baz
package import class Foo.Bac
package import enum Foo.Bap
package import protocol Foo.Bat
package import let Foo.Baq
package import var Foo.Bag
package import func Foo.Bak
"""

let imports = try StructuredSwiftTests.Import.translator.makeImports(
dependencies: dependencies,
accessLevel: accessLevel,
accessLevelOnImports: true
)

#expect(render(imports) == expected)
}

@Test(
"preconcurrency import rendering"
)
func preconcurrencyImports() throws {
var dependencies = [Dependency]()
dependencies.append(
Dependency(
module: "Foo",
preconcurrency: .required,
accessLevel: .internal
)
)
dependencies.append(
Dependency(
item: .init(kind: .enum, name: "Bar"),
module: "Foo",
preconcurrency: .required,
accessLevel: .internal
)
)
dependencies.append(
Dependency(
module: "Baz",
preconcurrency: .requiredOnOS(["Deq", "Der"]),
accessLevel: .internal
)
)

let expected =
"""
public import GRPCCore
@preconcurrency internal import Foo
@preconcurrency internal import enum Foo.Bar
#if os(Deq) || os(Der)
@preconcurrency internal import Baz
#else
internal import Baz
#endif
"""

let imports = try StructuredSwiftTests.Import.translator.makeImports(
dependencies: dependencies,
accessLevel: .public,
accessLevelOnImports: true
)

#expect(render(imports) == expected)
}

@Test(
"SPI import rendering"
)
func spiImports() throws {
var dependencies = [Dependency]()
dependencies.append(
Dependency(module: "Foo", spi: "Secret", accessLevel: .internal)
)
dependencies.append(
Dependency(
item: .init(kind: .enum, name: "Bar"),
module: "Foo",
spi: "Secret",
accessLevel: .internal
)
)

let expected =
"""
public import GRPCCore
@_spi(Secret) internal import Foo
@_spi(Secret) internal import enum Foo.Bar
"""

let imports = try StructuredSwiftTests.Import.translator.makeImports(
dependencies: dependencies,
accessLevel: .public,
accessLevelOnImports: true
)

#expect(render(imports) == expected)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Testing

@testable import GRPCCodeGen

extension StructuedSwiftTests {
extension StructuredSwiftTests {
@Suite("Metadata")
struct Metadata {
@Test("typealias Input = <Name>", arguments: AccessModifier.allCases)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Testing

@testable import GRPCCodeGen

extension StructuedSwiftTests {
extension StructuredSwiftTests {
@Suite("Server")
struct Server {
@Test(
Expand Down
10 changes: 8 additions & 2 deletions Tests/GRPCCodeGenTests/Internal/StructuredSwiftTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Testing
@testable import GRPCCodeGen

// Used as a namespace for organising other structured swift tests.
@Suite("Structued Swift")
struct StructuedSwiftTests {}
@Suite("Structured Swift")
struct StructuredSwiftTests {}

func render(_ declaration: Declaration) -> String {
let renderer = TextBasedRenderer(indentation: 2)
Expand All @@ -40,6 +40,12 @@ func render(_ blocks: [CodeBlock]) -> String {
return renderer.renderedContents()
}

func render(_ imports: [ImportDescription]) -> String {
let renderer = TextBasedRenderer(indentation: 2)
renderer.renderImports(imports)
return renderer.renderedContents()
}

enum RPCKind: Hashable, Sendable, CaseIterable {
case unary
case clientStreaming
Expand Down
Loading
Loading