Utils.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. import Foundation
  17. import GRPC
  18. import SwiftProtobuf
  19. internal func generateFileDescriptorProto(
  20. fileName name: String,
  21. suffix: String
  22. ) -> Google_Protobuf_FileDescriptorProto {
  23. let inputMessage = Google_Protobuf_DescriptorProto.with {
  24. $0.name = "inputMessage" + suffix
  25. $0.field = [
  26. Google_Protobuf_FieldDescriptorProto.with {
  27. $0.name = "inputField"
  28. $0.type = .bool
  29. }
  30. ]
  31. }
  32. let inputMessageExtension = Google_Protobuf_FieldDescriptorProto.with {
  33. $0.name = "extensionInputMessage" + suffix
  34. $0.extendee = "inputMessage" + suffix
  35. $0.number = 2
  36. }
  37. let outputMessage = Google_Protobuf_DescriptorProto.with {
  38. $0.name = "outputMessage" + suffix
  39. $0.field = [
  40. Google_Protobuf_FieldDescriptorProto.with {
  41. $0.name = "outputField"
  42. $0.type = .int32
  43. }
  44. ]
  45. }
  46. let enumType = Google_Protobuf_EnumDescriptorProto.with {
  47. $0.name = "enumType" + suffix
  48. $0.value = [
  49. Google_Protobuf_EnumValueDescriptorProto.with {
  50. $0.name = "value1"
  51. },
  52. Google_Protobuf_EnumValueDescriptorProto.with {
  53. $0.name = "value2"
  54. },
  55. ]
  56. }
  57. let method = Google_Protobuf_MethodDescriptorProto.with {
  58. $0.name = "testMethod" + suffix
  59. $0.inputType = inputMessage.name
  60. $0.outputType = outputMessage.name
  61. }
  62. let serviceDescriptor = Google_Protobuf_ServiceDescriptorProto.with {
  63. $0.method = [method]
  64. $0.name = "service" + suffix
  65. }
  66. let fileDescriptorProto = Google_Protobuf_FileDescriptorProto.with {
  67. $0.service = [serviceDescriptor]
  68. $0.name = name + suffix + ".proto"
  69. $0.package = "package" + name + suffix
  70. $0.messageType = [inputMessage, outputMessage]
  71. $0.enumType = [enumType]
  72. $0.extension = [inputMessageExtension]
  73. }
  74. return fileDescriptorProto
  75. }
  76. /// Creates the dependencies of the proto used in the testing context.
  77. internal func makeProtosWithDependencies() -> [Google_Protobuf_FileDescriptorProto] {
  78. var fileDependencies: [Google_Protobuf_FileDescriptorProto] = []
  79. for id in 1 ... 4 {
  80. let fileDescriptorProto = generateFileDescriptorProto(fileName: "bar", suffix: String(id))
  81. if id != 1 {
  82. // Dependency of the first dependency.
  83. fileDependencies[0].dependency.append(fileDescriptorProto.name)
  84. }
  85. fileDependencies.append(fileDescriptorProto)
  86. }
  87. return fileDependencies
  88. }
  89. internal func makeProtosWithComplexDependencies() -> [Google_Protobuf_FileDescriptorProto] {
  90. var protos: [Google_Protobuf_FileDescriptorProto] = []
  91. protos.append(generateFileDescriptorProto(fileName: "foo", suffix: "0"))
  92. for id in 1 ... 10 {
  93. let fileDescriptorProtoA = generateFileDescriptorProto(
  94. fileName: "fooA",
  95. suffix: String(id) + "A"
  96. )
  97. let fileDescriptorProtoB = generateFileDescriptorProto(
  98. fileName: "fooB",
  99. suffix: String(id) + "B"
  100. )
  101. let parent = protos.count > 1 ? protos.count - Int.random(in: 1 ..< 3) : protos.count - 1
  102. protos[parent].dependency.append(fileDescriptorProtoA.name)
  103. protos[parent].dependency.append(fileDescriptorProtoB.name)
  104. protos.append(fileDescriptorProtoA)
  105. protos.append(fileDescriptorProtoB)
  106. }
  107. return protos
  108. }
  109. extension Sequence where Element == Google_Protobuf_FileDescriptorProto {
  110. var serviceNames: [String] {
  111. self.flatMap { $0.service.map { $0.name } }
  112. }
  113. }
  114. extension Sequence where Element == Google_Protobuf_EnumDescriptorProto {
  115. var names: [String] {
  116. self.map { $0.name }
  117. }
  118. }