ProvidesDeprecationComment.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Sources/SwiftProtobufPluginLibrary/ProvidesDeprecationComment.swift
  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. /// Protocol that all the Descriptors conform to provide deprecation comments
  12. public protocol ProvidesDeprecationComment {
  13. /// Returns the deprecation comment to be used.
  14. func deprecationComment(commentPrefix: String) -> String
  15. }
  16. /// Protocol that a Descriptor can confirm to when only the Type controls depecation.
  17. public protocol SimpleProvidesDeprecationComment: ProvidesDeprecationComment {
  18. /// Name used in the generated message.
  19. var typeName: String { get }
  20. /// If the type is deprecated.
  21. var isDeprecated: Bool { get }
  22. }
  23. extension SimpleProvidesDeprecationComment {
  24. /// Default implementation to provide the depectation comment.
  25. public func deprecationComment(commentPrefix: String) -> String {
  26. guard isDeprecated else { return String() }
  27. return "\(commentPrefix) NOTE: This \(typeName) was marked as deprecated in the .proto file\n"
  28. }
  29. }
  30. /// Protocol that a Descriptor can confirm to when the Type or the File controls depecation.
  31. public protocol TypeOrFileProvidesDeprecationComment: ProvidesDeprecationComment {
  32. /// Name used in the generated message.
  33. var typeName: String { get }
  34. /// If the type is deprecated.
  35. var isDeprecated: Bool { get }
  36. /// Returns the File this conforming object is in.
  37. var file: FileDescriptor! { get }
  38. }
  39. extension TypeOrFileProvidesDeprecationComment {
  40. /// Default implementation to provide the depectation comment.
  41. public func deprecationComment(commentPrefix: String) -> String {
  42. if isDeprecated {
  43. return "\(commentPrefix) NOTE: This \(typeName) was marked as deprecated in the .proto file.\n"
  44. }
  45. guard file.options.deprecated else { return String() }
  46. return "\(commentPrefix) NOTE: The whole .proto file that defined this \(typeName) was marked as deprecated.\n"
  47. }
  48. }
  49. extension ProvidesDeprecationComment where Self: ProvidesSourceCodeLocation {
  50. /// Helper to get the protoSourceComments combined with any depectation comment.
  51. public func protoSourceCommentsWithDeprecation(
  52. commentPrefix: String = "///",
  53. leadingDetachedPrefix: String? = nil
  54. ) -> String {
  55. let protoSourceComments = protoSourceComments(
  56. commentPrefix: commentPrefix,
  57. leadingDetachedPrefix: leadingDetachedPrefix
  58. )
  59. let deprecationComments = deprecationComment(commentPrefix: commentPrefix)
  60. if deprecationComments.isEmpty {
  61. return protoSourceComments
  62. }
  63. if protoSourceComments.isEmpty {
  64. return deprecationComments
  65. }
  66. return "\(protoSourceComments)\(commentPrefix)\n\(deprecationComments)"
  67. }
  68. }