Descriptor+Extensions.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Sources/SwiftProtobufPluginLibrary/Descriptor+Extensions.swift - Additions to Descriptor
  2. //
  3. // Copyright (c) 2014 - 2017 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. import Foundation
  11. import SwiftProtobuf
  12. extension FileDescriptor: ProvidesSourceCodeLocation {
  13. public var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? {
  14. // google/protobuf's descriptor.cc says it should be an empty path.
  15. return sourceCodeInfoLocation(path: IndexPath())
  16. }
  17. }
  18. extension Descriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  19. public func getLocationPath(path: inout IndexPath) {
  20. if let containingType = containingType {
  21. containingType.getLocationPath(path: &path)
  22. path.append(Google_Protobuf_DescriptorProto.FieldNumbers.nestedType)
  23. } else {
  24. path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.messageType)
  25. }
  26. path.append(index)
  27. }
  28. }
  29. extension EnumDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  30. public func getLocationPath(path: inout IndexPath) {
  31. if let containingType = containingType {
  32. containingType.getLocationPath(path: &path)
  33. path.append(Google_Protobuf_DescriptorProto.FieldNumbers.enumType)
  34. } else {
  35. path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.enumType)
  36. }
  37. path.append(index)
  38. }
  39. }
  40. extension EnumValueDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  41. public func getLocationPath(path: inout IndexPath) {
  42. enumType.getLocationPath(path: &path)
  43. path.append(Google_Protobuf_EnumDescriptorProto.FieldNumbers.value)
  44. path.append(index)
  45. }
  46. }
  47. extension OneofDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  48. public func getLocationPath(path: inout IndexPath) {
  49. containingType.getLocationPath(path: &path)
  50. path.append(Google_Protobuf_DescriptorProto.FieldNumbers.oneofDecl)
  51. path.append(index)
  52. }
  53. }
  54. extension FieldDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  55. public func getLocationPath(path: inout IndexPath) {
  56. if isExtension {
  57. if let extensionScope = extensionScope {
  58. extensionScope.getLocationPath(path: &path)
  59. path.append(Google_Protobuf_DescriptorProto.FieldNumbers.extension)
  60. } else {
  61. path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.extension)
  62. }
  63. } else {
  64. containingType.getLocationPath(path: &path)
  65. path.append(Google_Protobuf_DescriptorProto.FieldNumbers.field)
  66. }
  67. path.append(index)
  68. }
  69. /// Returns true if the type can be used for a Packed field.
  70. static func isPackable(type: Google_Protobuf_FieldDescriptorProto.TypeEnum) -> Bool {
  71. // This logic comes from the C++ FieldDescriptor::IsTypePackable() impl.
  72. switch type {
  73. case .string, .group, .message, .bytes:
  74. return false
  75. default:
  76. return true
  77. }
  78. }
  79. /// Is this field packable.
  80. var isPackable: Bool {
  81. // This logic comes from the C++ FieldDescriptor::is_packable() impl.
  82. return label == .repeated && FieldDescriptor.isPackable(type: type)
  83. }
  84. /// Helper to return the name to as the "base" for naming of generated fields.
  85. ///
  86. /// Groups use the underlying message's name. The way groups are declared in
  87. /// proto files, the filed names is derived by lowercasing the Group's name,
  88. /// so there are no underscores, etc. to rebuild a camel case name from.
  89. var namingBase: String {
  90. return type == .group ? messageType.name : name
  91. }
  92. }
  93. extension ServiceDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  94. public func getLocationPath(path: inout IndexPath) {
  95. path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.service)
  96. path.append(index)
  97. }
  98. }
  99. extension MethodDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
  100. public func getLocationPath(path: inout IndexPath) {
  101. service.getLocationPath(path: &path)
  102. path.append(Google_Protobuf_ServiceDescriptorProto.FieldNumbers.method)
  103. path.append(index)
  104. }
  105. }