Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a syntactic "Add Codable structs from JSON" code action #1205

Merged
merged 3 commits into from
May 7, 2024

Commits on May 6, 2024

  1. Add a syntactic "Add Codable structs from JSON" code action

    Add a syntactic action that takes JSON pasted into a Swift file or
    placed in a string literal, then turns it into a set of Codable
    structs that can represent the JSON. Our typical example starts like
    this:
    
    ```
    {
        "name": "Produce",
        "shelves": [
            {
                "name": "Discount Produce",
                "product": {
                    "name": "Banana",
                    "points": 200,
                    "description": "A banana that's perfectly ripe."
                }
            }
        ]
    }
    ```
    
    and turns into this:
    
    ```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
            }
        }
    }
    ```
    
    When converting to JSON, we attempt to reason about multiple JSON
    objects on the same level to detect when there are optional fields,
    due to either an explicit null or due to the absence of fields in some
    of the JSON objects that are conceptually stored together.
    
    The refactoring itself would live down in the swift-syntax package if
    not for its dependency on Foundation. We'll move it when appropriate.
    DougGregor committed May 6, 2024
    Configuration menu
    Copy the full SHA
    e54c99e View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    037e55e View commit details
    Browse the repository at this point in the history

Commits on May 7, 2024

  1. Address review comments

    DougGregor committed May 7, 2024
    Configuration menu
    Copy the full SHA
    d4bbf9c View commit details
    Browse the repository at this point in the history