File: ConvertJSONToCodableStruct.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (418 lines) | stat: -rw-r--r-- 13,122 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import LanguageServerProtocol
import SwiftBasicFormat
import SwiftRefactor
import SwiftSyntax

/// Convert JSON literals into corresponding Swift structs that conform to the
/// `Codable` protocol.
///
/// ## Before
///
/// ```javascript
/// {
///   "name": "Produce",
///   "shelves": [
///     {
///       "name": "Discount Produce",
///       "product": {
///         "name": "Banana",
///         "points": 200,
///         "description": "A banana that's perfectly ripe."
///       }
///     }
///   ]
/// }
/// ```
///
/// ## After
///
/// ```swift
/// struct JSONValue: Codable {
///   var name: String
///   var shelves: [Shelves]
///
///   struct Shelves: Codable {
///     var name: String
///     var product: Product
///
///     struct Product: Codable {
///       var description: String
///       var name: String
///       var points: Double
///     }
///   }
/// }
/// ```
@_spi(Testing)
public struct ConvertJSONToCodableStruct: EditRefactoringProvider {
  @_spi(Testing)
  public static func textRefactor(
    syntax: Syntax,
    in context: Void
  ) -> [SourceEdit] {
    // Dig out a syntax node that looks like it might be JSON or have JSON
    // in it.
    guard let preflight = preflightRefactoring(syntax) else {
      return []
    }

    // Dig out the text that we think might be JSON.
    let text: String
    switch preflight {
    case let .closure(closure):
      /// The outer structure of the JSON { ... } looks like a closure in the
      /// syntax tree, albeit one with lots of ill-formed syntax in the body.
      /// We're only going to look at the text of the closure to see if we
      /// have JSON in there.
      text = closure.trimmedDescription
    case let .endingClosure(closure, unexpected):
      text = closure.trimmedDescription + unexpected.description

    case .stringLiteral(_, let literalText):
      /// A string literal that could contain JSON within it.
      text = literalText
    }

    // Try to process this as JSON.
    guard
      let data = text.data(using: .utf8),
      let object = try? JSONSerialization.jsonObject(with: data),
      let dictionary = object as? [String: Any]
    else {
      return []
    }

    // Create the top-level object.
    let topLevelObject = JSONObject(dictionary: dictionary)

    // Render the top-level object as a struct.
    let indentation = BasicFormat.inferIndentation(of: syntax)
    let format = BasicFormat(indentationWidth: indentation)
    let decls = topLevelObject.asDeclSyntax(name: "JSONValue")
      .formatted(using: format)

    // Render the change into a set of source edits.
    switch preflight {
    case .closure(let closure):
      // Closures are replaced entirely, since they were invalid code to
      // start with.
      return [
        SourceEdit(
          range: closure.positionAfterSkippingLeadingTrivia..<closure.endPositionBeforeTrailingTrivia,
          replacement: decls.description
        )
      ]
    case .endingClosure(let closure, let unexpected):
      // Closures are replaced entirely, since they were invalid code to
      // start with.
      return [
        SourceEdit(
          range: closure.positionAfterSkippingLeadingTrivia..<unexpected.endPosition,
          replacement: decls.description
        )
      ]
    case .stringLiteral(let literal, _):
      /// Leave the string literal in place (it might be there for testing
      /// purposes), and put the newly-created structs afterward.
      return [
        SourceEdit(
          range: literal.endPosition..<literal.endPosition,
          replacement: "\n" + decls.description
        )
      ]
    }
  }

  /// The result of preflighting a syntax node to try to find potential JSON
  /// in it.
  private enum Preflight {
    /// A closure, which is what a JSON dictionary looks like when pasted
    /// into Swift.
    case closure(ClosureExprSyntax)

    /// A closure with a bunch of unexpected nodes following it, which is what
    /// a big JSON dictionary looks like when pasted into Swift.
    case endingClosure(ClosureExprSyntax, UnexpectedNodesSyntax)

    /// A string literal that may contain JSON.
    case stringLiteral(StringLiteralExprSyntax, String)
  }

  /// Look for either a closure or a string literal that might have JSON in it.
  private static func preflightRefactoring(_ syntax: Syntax) -> Preflight? {
    // Preflight a closure.
    //
    // A blob of JSON dropped into a Swift source file will look like a
    // closure due to the curly braces. The internals might be a syntactic
    // disaster, but we don't actually care.
    if let closure = syntax.as(ClosureExprSyntax.self) {
      if let file = closure.parent?.parent?.parent?.as(SourceFileSyntax.self),
        let unexpected = file.unexpectedBetweenStatementsAndEndOfFileToken
      {
        return .endingClosure(closure, unexpected)
      }
      return .closure(closure)
    }

    // We found a string literal; its contents might be JSON.
    if let stringLiteral = syntax.as(StringLiteralExprSyntax.self) {
      // Look for an enclosing context and prefer that, because we might have
      // a string literal that's inside a closure where the closure itself
      // is the JSON.
      if let parent = syntax.parent,
        let enclosingPreflight = preflightRefactoring(parent)
      {
        return enclosingPreflight
      }

      guard let text = stringLiteral.representedLiteralValue else {
        return nil
      }

      return .stringLiteral(stringLiteral, text)
    }

    // Look further up the syntax tree.
    if let parent = syntax.parent {
      return preflightRefactoring(parent)
    }

    return nil
  }
}

extension ConvertJSONToCodableStruct: SyntaxRefactoringCodeActionProvider {
  static func nodeToRefactor(in scope: SyntaxCodeActionScope) -> Syntax? {
    var node: Syntax? = scope.innermostNodeContainingRange
    while let unwrappedNode = node, ![.codeBlockItem, .memberBlockItem].contains(unwrappedNode.kind) {
      if preflightRefactoring(unwrappedNode) != nil {
        return unwrappedNode
      }
      node = unwrappedNode.parent
    }
    return nil
  }

  static let title = "Create Codable structs from JSON"
}

/// A JSON object, which is has a set of fields, each of which has the given
/// type.
fileprivate struct JSONObject {
  /// The fields of the JSON object.
  var fields: [String: JSONType] = [:]

  /// Form a JSON object from its fields.
  private init(fields: [String: JSONType]) {
    self.fields = fields
  }

  /// Form a JSON object given a dictionary.
  init(dictionary: [String: Any]) {
    fields = dictionary.mapValues { JSONType(value: $0) }
  }

  /// Merge the fields of this JSON object with another JSON object to produce
  /// a JSON object
  func merging(with other: JSONObject) -> JSONObject {
    // Collect the set of all keys from both JSON objects.
    let allKeys = Set(fields.keys).union(other.fields.keys)

    // Form a new JSON object containing the union of the fields
    let newFields = allKeys.map { key in
      let myValue = fields[key] ?? .null
      let otherValue = other.fields[key] ?? .null
      return (key, myValue.merging(with: otherValue))
    }
    return JSONObject(fields: [String: JSONType](uniqueKeysWithValues: newFields))
  }

  /// Render this JSON object into a struct.
  func asDeclSyntax(name: String) -> DeclSyntax {
    /// The list of fields in this object, sorted alphabetically.
    let sortedFields = fields.sorted(by: { $0.key < $1.key })

    // Collect the nested types
    let nestedTypes: [(name: String, type: JSONObject)] = sortedFields.compactMap { (name, type) in
      guard let object = type.innerObject else {
        return nil
      }

      return (name.capitalized, object)
    }

    let members = MemberBlockItemListSyntax {
      // Print the fields of this type.
      for (fieldName, fieldType) in sortedFields {
        MemberBlockItemSyntax(
          leadingTrivia: .newline,
          decl: "var \(raw: fieldName): \(fieldType.asTypeSyntax(name: fieldName))" as DeclSyntax
        )
      }

      // Print any nested types.
      for (typeName, object) in nestedTypes {
        MemberBlockItemSyntax(
          leadingTrivia: (typeName == nestedTypes.first?.name) ? .newlines(2) : .newline,
          decl: object.asDeclSyntax(name: typeName)
        )
      }
    }

    return """
      struct \(raw: name): Codable {
        \(members.trimmed)
      }
      """
  }
}

/// Describes the type of JSON data.
fileprivate enum JSONType {
  /// String data
  case string

  /// Numeric data
  case number

  /// Boolean data
  case boolean

  /// A "null", which implies optionality but without any underlying type
  /// information.
  case null

  /// An array.
  indirect case array(JSONType)

  /// An object.
  indirect case object(JSONObject)

  /// A value that is optional, for example because it is missing or null in
  /// other cases.
  indirect case optional(JSONType)

  /// Determine the type of a JSON value.
  init(value: Any) {
    switch value {
    case let string as String:
      switch string {
      case "true", "false": self = .boolean
      default: self = .string
      }
    case is NSNumber:
      self = .number
    case let array as [Any]:
      // Use null as a fallback for an empty array.
      guard let firstValue = array.first else {
        self = .array(.null)
        return
      }

      // Merge the array elements.
      let elementType: JSONType = array[1...].reduce(
        JSONType(value: firstValue)
      ) { (result, value) in
        result.merging(with: JSONType(value: value))
      }
      self = .array(elementType)

    case is NSNull:
      self = .null
    case let dictionary as [String: Any]:
      self = .object(JSONObject(dictionary: dictionary))
    default:
      self = .string
    }
  }

  /// Merge this JSON type with another JSON type, producing a new JSON type
  /// that abstracts over the two.
  func merging(with other: JSONType) -> JSONType {
    switch (self, other) {
    // Exact matches are easy.
    case (.string, .string): return .string
    case (.number, .number): return .number
    case (.boolean, .boolean): return .boolean
    case (.null, .null): return .null

    case (.array(let inner), .array(.null)), (.array(.null), .array(let inner)):
      // Merging an array with an array of null leaves the array.
      return .array(inner)

    case (.array(let inner), .null), (.null, .array(let inner)):
      // Merging an array with a null just leaves an array.
      return .array(inner)

    case (.array(let left), .array(let right)):
      // Merging two arrays merges the element types
      return .array(left.merging(with: right))

    case (.object(let left), .object(let right)):
      // Merging two arrays merges the element types
      return .object(left.merging(with: right))

    // Merging a string with a Boolean means we misinterpreted "true" or
    // "false" as Boolean when it was meant as a string.
    case (.string, .boolean), (.boolean, .string): return .string

    // Merging 'null' with an optional returns the optional.
    case (.optional(let inner), .null), (.null, .optional(let inner)):
      return .optional(inner)

    // Merging 'null' with anything else makes it an optional.
    case (let inner, .null), (.null, let inner):
      return .optional(inner)

    // Merging two optionals merges the underlying types and makes the
    // result optional.
    case (.optional(let left), .optional(let right)):
      return .optional(left.merging(with: right))

    // Merging an optional with anything else merges the underlying bits and
    // makes them optional.
    case (let outer, .optional(let inner)), (.optional(let inner), let outer):
      return .optional(inner.merging(with: outer))

    // Fall back to the null case when we don't know.
    default:
      return .null
    }
  }

  /// Dig out the JSON inner object referenced by this type.
  var innerObject: JSONObject? {
    switch self {
    case .string, .null, .number, .boolean: nil
    case .optional(let inner): inner.innerObject
    case .array(let inner): inner.innerObject
    case .object(let object): object
    }
  }

  /// Render this JSON type into type syntax.
  func asTypeSyntax(name: String) -> TypeSyntax {
    switch self {
    case .string: "String"
    case .number: "Double"
    case .boolean: "Bool"
    case .null: "Void"
    case .optional(let inner): "\(inner.asTypeSyntax(name: name))?"
    case .array(let inner): "[\(inner.asTypeSyntax(name: name))]"
    case .object(_): "\(raw: name.capitalized)"
    }
  }
}