Skip to content

Commit 74794ce

Browse files
Brought CodeRenderer from OpenAPI Generator. (#1728)
Motivation: Provides an API which takes Structured Swift Representation of a file and generates an object containing the generated code as a String. Modifications: - Brought over RendererProtocol and TextBasedRenderer. - Brought over unit tests in Test_TextBasedRenderer. - Fixed TypeName bugs that produced errors while building. Result: The CodeGenLib will have a code renderer that transforms `StructuredSwiftRepresentation` into `SourceFile`.
1 parent bd5af62 commit 74794ce

File tree

6 files changed

+1776
-4
lines changed

6 files changed

+1776
-4
lines changed

NOTICES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ This product uses derivations of swift-extras/swift-extras-base64 'Base64.swift'
6161
---
6262

6363
This product uses derivations of apple/swift-openapi-generator 'StructuredSwiftRepresentation.swift',
64-
'TypeName.swift', 'TypeUsage.swift' and 'Builtins.swift'.
64+
'TypeName.swift', 'TypeUsage.swift', 'Builtins.swift', 'RendererProtocol.swift', 'TextBasedProtocol',
65+
and 'Test_TextBasedRenderer'.
6566

6667
* LICENSE (Apache License 2.0):
6768
* https://github.com/apple/swift-openapi-generator/blob/main/LICENSE.txt

Package.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ extension Target {
216216
.nioEmbedded,
217217
.nioTransportServices,
218218
.logging,
219-
.reflectionService,
219+
.reflectionService
220220
].appending(
221221
.nioSSL, if: includeNIOSSL
222222
),
@@ -235,6 +235,13 @@ extension Target {
235235
]
236236
)
237237

238+
static let grpcCodeGenTests: Target = .testTarget(
239+
name: "GRPCCodeGenTests",
240+
dependencies: [
241+
.grpcCodeGen
242+
]
243+
)
244+
238245
static let interopTestModels: Target = .target(
239246
name: "GRPCInteroperabilityTestModels",
240247
dependencies: [
@@ -559,9 +566,11 @@ let package = Package(
559566

560567
// v2
561568
.grpcCore,
569+
.grpcCodeGen,
562570

563571
// v2 tests
564572
.grpcCoreTests,
573+
.grpcCodeGenTests
565574
]
566575
)
567576

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2023, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
//===----------------------------------------------------------------------===//
17+
//
18+
// This source file is part of the SwiftOpenAPIGenerator open source project
19+
//
20+
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
21+
// Licensed under Apache License v2.0
22+
//
23+
// See LICENSE.txt for license information
24+
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
25+
//
26+
// SPDX-License-Identifier: Apache-2.0
27+
//
28+
//===----------------------------------------------------------------------===//
29+
30+
/// An object that renders structured Swift representations
31+
/// into Swift files.
32+
///
33+
/// Rendering is the last phase of the generator pipeline.
34+
protocol RendererProtocol {
35+
36+
/// Renders the specified structured code into a raw Swift file.
37+
/// - Parameters:
38+
/// - code: A structured representation of the Swift code.
39+
/// - config: The configuration of the generator.
40+
/// - diagnostics: The collector to which to emit diagnostics.
41+
/// - Returns: A raw file with Swift contents.
42+
/// - Throws: An error if an issue occurs during rendering.
43+
func render(structured code: StructuredSwiftRepresentation) throws -> SourceFile
44+
}

0 commit comments

Comments
 (0)