sourcekit-lsp
sourcekit-lsp copied to clipboard
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:
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
}
}
}
This code action can definitely be improved by doing more work in analyzing the JSON to (for example) abstract over multiple elements in an array to make fields optional (if they only show up in some members) and perhaps even identify when we really have something that's an enum. Such improvements can be done without changing the general shape of this refactor.
The refactoring itself would live down in the swift-syntax package if not for its dependency on Foundation. We'll move as appropriate.
@swift-ci please test
@swift-ci please test
@swift-ci please test
@swift-ci please test
@swift-ci please test Windows
@swift-ci please test Windows
@swift-ci please test Linux
@swift-ci please test Linux
@swift-ci please test
@swift-ci please test Windows
I just went ahead and rewrote this with a completely new structure that does merging of like elements in the whole tree. When merging a field w/ a missing value (or one explicitly marked null), we introduce an optional in Swift. This also handles Boolean values specially.
@swift-ci please test
@swift-ci please test Windows
@swift-ci please test
@swift-ci please test Windows