|
| 1 | +/* |
| 2 | + * Copyright 2025, 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 | +import Foundation |
| 18 | +import GRPCCodeGen |
| 19 | + |
| 20 | +struct JSONCodeGenerator { |
| 21 | + private static let currentYear: Int = { |
| 22 | + let now = Date() |
| 23 | + let year = Calendar.current.component(.year, from: Date()) |
| 24 | + return year |
| 25 | + }() |
| 26 | + |
| 27 | + private static let header = """ |
| 28 | + /* |
| 29 | + * Copyright \(Self.currentYear), gRPC Authors All rights reserved. |
| 30 | + * |
| 31 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 32 | + * you may not use this file except in compliance with the License. |
| 33 | + * You may obtain a copy of the License at |
| 34 | + * |
| 35 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 36 | + * |
| 37 | + * Unless required by applicable law or agreed to in writing, software |
| 38 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 39 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 40 | + * See the License for the specific language governing permissions and |
| 41 | + * limitations under the License. |
| 42 | + */ |
| 43 | + """ |
| 44 | + |
| 45 | + private static let jsonSerializers: String = """ |
| 46 | + fileprivate struct JSONSerializer<Message: Codable>: MessageSerializer { |
| 47 | + fileprivate func serialize<Bytes: GRPCContiguousBytes>( |
| 48 | + _ message: Message |
| 49 | + ) throws -> Bytes { |
| 50 | + do { |
| 51 | + let jsonEncoder = JSONEncoder() |
| 52 | + let data = try jsonEncoder.encode(message) |
| 53 | + return Bytes(data) |
| 54 | + } catch { |
| 55 | + throw RPCError( |
| 56 | + code: .internalError, |
| 57 | + message: "Can't serialize message to JSON.", |
| 58 | + cause: error |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +
|
| 64 | + fileprivate struct JSONDeserializer<Message: Codable>: MessageDeserializer { |
| 65 | + fileprivate func deserialize<Bytes: GRPCContiguousBytes>( |
| 66 | + _ serializedMessageBytes: Bytes |
| 67 | + ) throws -> Message { |
| 68 | + do { |
| 69 | + let jsonDecoder = JSONDecoder() |
| 70 | + let data = serializedMessageBytes.withUnsafeBytes { Data($0) } |
| 71 | + return try jsonDecoder.decode(Message.self, from: data) |
| 72 | + } catch { |
| 73 | + throw RPCError( |
| 74 | + code: .internalError, |
| 75 | + message: "Can't deserialize message from JSON.", |
| 76 | + cause: error |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + """ |
| 82 | + |
| 83 | + func generate(request: JSONCodeGeneratorRequest) throws -> SourceFile { |
| 84 | + let generator = CodeGenerator(config: CodeGenerator.Config(request.config)) |
| 85 | + |
| 86 | + let codeGenRequest = CodeGenerationRequest( |
| 87 | + fileName: request.service.name + ".swift", |
| 88 | + leadingTrivia: Self.header, |
| 89 | + dependencies: [ |
| 90 | + Dependency( |
| 91 | + item: Dependency.Item(kind: .struct, name: "Data"), |
| 92 | + module: "Foundation", |
| 93 | + accessLevel: .internal |
| 94 | + ), |
| 95 | + Dependency( |
| 96 | + item: Dependency.Item(kind: .class, name: "JSONEncoder"), |
| 97 | + module: "Foundation", |
| 98 | + accessLevel: .internal |
| 99 | + ), |
| 100 | + Dependency( |
| 101 | + item: Dependency.Item(kind: .class, name: "JSONDecoder"), |
| 102 | + module: "Foundation", |
| 103 | + accessLevel: .internal |
| 104 | + ), |
| 105 | + ], |
| 106 | + services: [ServiceDescriptor(request.service)], |
| 107 | + makeSerializerCodeSnippet: { type in "JSONSerializer<\(type)>()" }, |
| 108 | + makeDeserializerCodeSnippet: { type in "JSONDeserializer<\(type)>()" } |
| 109 | + ) |
| 110 | + |
| 111 | + var sourceFile = try generator.generate(codeGenRequest) |
| 112 | + |
| 113 | + // Insert a fileprivate serializer/deserializer for JSON at the bottom of each file. |
| 114 | + sourceFile.contents += "\n\n" |
| 115 | + sourceFile.contents += Self.jsonSerializers |
| 116 | + |
| 117 | + return sourceFile |
| 118 | + } |
| 119 | +} |
0 commit comments