Google_Protobuf_FieldMask_Extensions.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // ProtobufRuntime/Sources/Protobuf/Google_Protobuf_FieldMask_Extensions.swift - Fieldmask extensions
  2. //
  3. // This source file is part of the Swift.org open source project
  4. //
  5. // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See http://swift.org/LICENSE.txt for license information
  9. // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  10. //
  11. // -----------------------------------------------------------------------------
  12. ///
  13. /// Extend the generated FieldMask message with customized JSON coding and
  14. /// convenience methods.
  15. ///
  16. // -----------------------------------------------------------------------------
  17. import Swift
  18. // TODO: We should have utilities to apply a fieldmask to an arbitrary
  19. // message, intersect two fieldmasks, etc.
  20. private func ProtoToJSON(name: String) -> String? {
  21. var jsonPath = ""
  22. var chars = name.characters.makeIterator()
  23. while let c = chars.next() {
  24. switch c {
  25. case "_":
  26. if let toupper = chars.next() {
  27. switch toupper {
  28. case "a"..."z":
  29. jsonPath.append(String(toupper).uppercased())
  30. default:
  31. return nil
  32. }
  33. } else {
  34. return nil
  35. }
  36. case "A"..."Z":
  37. return nil
  38. default:
  39. jsonPath.append(c)
  40. }
  41. }
  42. return jsonPath
  43. }
  44. private func JSONToProto(name: String) -> String? {
  45. var path = ""
  46. for c in name.characters {
  47. switch c {
  48. case "_":
  49. return nil
  50. case "A"..."Z":
  51. path.append(Character("_"))
  52. path.append(String(c).lowercased())
  53. default:
  54. path.append(c)
  55. }
  56. }
  57. return path
  58. }
  59. private func parseJSONFieldNames(names: String) -> [String]? {
  60. var fieldNameCount = 0
  61. var fieldName = ""
  62. var split = [String]()
  63. for c: Character in names.characters {
  64. switch c {
  65. case ",":
  66. if fieldNameCount == 0 {
  67. return nil
  68. }
  69. if let pbName = JSONToProto(name: fieldName) {
  70. split.append(pbName)
  71. } else {
  72. return nil
  73. }
  74. fieldName = ""
  75. fieldNameCount = 0
  76. default:
  77. fieldName.append(c)
  78. fieldNameCount += 1
  79. }
  80. }
  81. if fieldNameCount == 0 { // Last field name can't be empty
  82. return nil
  83. }
  84. if let pbName = JSONToProto(name: fieldName) {
  85. split.append(pbName)
  86. } else {
  87. return nil
  88. }
  89. return split
  90. }
  91. public extension Google_Protobuf_FieldMask {
  92. /// Initialize a FieldMask object with an array of paths.
  93. /// The paths should match the names used in the proto file (which
  94. /// will be different than the corresponding Swift property names).
  95. public init(protoPaths: [String]) {
  96. self.init()
  97. paths = protoPaths
  98. }
  99. /// Initialize a FieldMask object with the provided paths.
  100. /// The paths should match the names used in the proto file (which
  101. /// will be different than the corresponding Swift property names).
  102. public init(protoPaths: String...) {
  103. self.init(protoPaths: protoPaths)
  104. }
  105. /// Initialize a FieldMask object with the provided paths.
  106. /// The paths should match the names used in the JSON serialization
  107. /// (which will be different than the field names in the proto file
  108. /// or the corresponding Swift property names).
  109. public init?(jsonPaths: String...) {
  110. // TODO: This should fail if any of the conversions from JSON fails
  111. self.init(protoPaths: jsonPaths.flatMap(JSONToProto))
  112. }
  113. // It would be nice if to have an initializer that accepted Swift property
  114. // names, but translating between swift and protobuf/json property
  115. // names is not entirely deterministic.
  116. mutating public func decodeFromJSONToken(token: ProtobufJSONToken) throws {
  117. switch token {
  118. case .string(let s):
  119. if let names = parseJSONFieldNames(names: s) {
  120. paths = names
  121. } else {
  122. throw ProtobufDecodingError.fieldMaskConversion
  123. }
  124. default:
  125. throw ProtobufDecodingError.schemaMismatch
  126. }
  127. }
  128. // Custom hand-rolled JSON serializer
  129. public func serializeJSON() throws -> String {
  130. // Note: Proto requires alphanumeric field names, so there
  131. // cannot be a ',' or '"' character to mess up this formatting.
  132. var jsonPaths = [String]()
  133. for p in paths {
  134. if let jsonPath = ProtoToJSON(name: p) {
  135. jsonPaths.append(jsonPath)
  136. } else {
  137. throw ProtobufEncodingError.fieldMaskConversion
  138. }
  139. }
  140. return "\"" + jsonPaths.joined(separator: ",") + "\""
  141. }
  142. public func serializeAnyJSON() throws -> String {
  143. let value = try serializeJSON()
  144. return "{\"@type\":\"\(anyTypeURL)\",\"value\":\(value)}"
  145. }
  146. }