|
| 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 ArgumentParser |
| 18 | +import Foundation |
| 19 | + |
| 20 | +struct GenerateJSON: ParsableCommand { |
| 21 | + static let configuration = CommandConfiguration( |
| 22 | + commandName: "generate-json", |
| 23 | + subcommands: [Generate.self, DumpConfig.self], |
| 24 | + defaultSubcommand: Generate.self |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +extension GenerateJSON { |
| 29 | + struct Generate: ParsableCommand { |
| 30 | + @Argument(help: "The path to a JSON input file.") |
| 31 | + var input: String |
| 32 | + |
| 33 | + func run() throws { |
| 34 | + // Decode the input file. |
| 35 | + let url = URL(filePath: self.input) |
| 36 | + let data = try Data(contentsOf: url) |
| 37 | + let json = JSONDecoder() |
| 38 | + let config = try json.decode(JSONCodeGeneratorRequest.self, from: data) |
| 39 | + |
| 40 | + // Generate the output and dump it to stdout. |
| 41 | + let generator = JSONCodeGenerator() |
| 42 | + let sourceFile = try generator.generate(request: config) |
| 43 | + print(sourceFile.contents) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +extension GenerateJSON { |
| 49 | + struct DumpConfig: ParsableCommand { |
| 50 | + func run() throws { |
| 51 | + // Create a request for the code generator using all four RPC kinds. |
| 52 | + var request = JSONCodeGeneratorRequest( |
| 53 | + service: ServiceSchema(name: "Echo", methods: []), |
| 54 | + config: .defaults |
| 55 | + ) |
| 56 | + |
| 57 | + let methodNames = ["get", "collect", "expand", "update"] |
| 58 | + let methodKinds: [ServiceSchema.Method.Kind] = [ |
| 59 | + .unary, |
| 60 | + .clientStreaming, |
| 61 | + .serverStreaming, |
| 62 | + .bidiStreaming, |
| 63 | + ] |
| 64 | + |
| 65 | + for (name, kind) in zip(methodNames, methodKinds) { |
| 66 | + let method = ServiceSchema.Method( |
| 67 | + name: name, |
| 68 | + input: "EchoRequest", |
| 69 | + output: "EchoResponse", |
| 70 | + kind: kind |
| 71 | + ) |
| 72 | + request.service.methods.append(method) |
| 73 | + } |
| 74 | + |
| 75 | + // Encoding the config to JSON and dump it to stdout. |
| 76 | + let encoder = JSONEncoder() |
| 77 | + encoder.outputFormatting = [.prettyPrinted] |
| 78 | + let data = try encoder.encode(request) |
| 79 | + let json = String(decoding: data, as: UTF8.self) |
| 80 | + print(json) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments