I believe that result builders can greatly enhance simplicity, convenience, correctness and the overall developer experience, even though my ideas on this topic are still evolving.
One concept I've been thinking of is having JSONPath working like Regex, enabling developers to craft intricate queries more naturally. Additionally, the ability to validate JSONPath at compile-time would be great.
A good use case could involve employing ResultBuilders for creating JSON Transformers. These transformers, which are essentially functions capable of reshaping JSON data according to predefined rules, could streamline the process significantly. Instead of performing the Codable dance of decoding, processing, and encoding, we could achieve this in a single pass.
In my current project, I find myself frequently in need of performing various JSON transformations.
Currently, accomplishing such tasks involves the Codable paradigm, which can be somewhat limiting. For example, when dealing with numerous JSON variations, I'd rather not create 150 different JSON models. Instead, I prefer to work in a more abstract manner, where I understand that some part of the data structure will remain consistent sometimes. While Swift Macros can alleviate some of these limitations, I firmly believe that directly handling JSON during the parsing phase can lead to more efficient and intuitive workflows.
Ultimately, I'm excited about the potential of result builders in enhancing JSON manipulation.
This is a exemple of my transformer config file
These are some of the rules that I have that are perfomed of a lot of various json files.
[
{
"action": "replace-key",
"parent_node": "$.vp[?(@.web)]",
"from": "web",
"to": "apple"
},
{
"action": "replace-key",
"parent_node": "$.vp..*.color.border[?(@.active)]",
"from": "active",
"to": "focus"
},
{
"action": "replace-key",
"parent_node": "$.vp..*.color.content[?(@.default)]",
"from": "default",
"to": "text"
},
{
"action": "replace-key",
"parent_node": "$.vp..*.color[?(@['primary-label'])]",
"from": "primary-label",
"to": "text"
},
{
"action": "replace-key",
"parent_node": "$.vp..*.color[?(@['question-icon'])]",
"from": "question-icon",
"to": "icon"
},
{
"action": "replace-key",
"parent_node": "$.vp..button.color..*[?(@.default)]",
"from": "default",
"to": "enabled"
},
{
"action": "replace-key",
"parent_node": "$.vp..button.color..*[?(@.active)]",
"from": "active",
"to": "pressed"
},
{
"action": "replace-key",
"parent_node": "$.vp..link.color..*[?(@.default)]",
"from": "default",
"to": "enabled"
},
{
"action": "replace-key",
"parent_node": "$.vp..link.color..*[?(@.active)]",
"from": "active",
"to": "pressed"
},
{
"action": "replace-key",
"parent_node": "$.vp..radio.color..*[?(@.default)]",
"from": "default",
"to": "enabled"
},
{
"action": "replace-key",
"parent_node": "$.vp..radio.color..*[?(@.active)]",
"from": "active",
"to": "pressed"
},
{
"action": "replace-key",
"parent_node": "$.vp..checkbox.color..*[?(@.default)]",
"from": "default",
"to": "enabled"
},
{
"action": "replace-key",
"parent_node": "$.vp..checkbox.color..*[?(@.active)]",
"from": "active",
"to": "pressed"
},
{
"action": "replace-key",
"parent_node": "$.vp..['icon-button'].color..*[?(@.default)]",
"from": "default",
"to": "enabled"
},
{
"action": "replace-key",
"parent_node": "$.vp..['icon-button'].color..*[?(@.active)]",
"from": "active",
"to": "pressed"
},
{
"action": "replace-key",
"parent_node": "$.vp..color..*[?(@.container)]",
"from": "container",
"to": "background"
},
{
"action": "replace-key",
"parent_node": "$.vp..color..*[?(@.content)]",
"from": "content",
"to": "foreground"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['border-radius'])]..*[?(@['bottom-left'])]",
"from": "bottom-left",
"to": "bottom-leading"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['border-radius'])]..*[?(@['bottom-right'])]",
"from": "bottom-right",
"to": "bottom-trailing"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['border-radius'])]..*[?(@['top-left'])]",
"from": "top-left",
"to": "top-leading"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['border-radius'])]..*[?(@['top-right'])]",
"from": "top-right",
"to": "top-trailing"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['block-end'])]",
"from": "block-end",
"to": "bottom"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['inline-start'])]",
"from": "inline-start",
"to": "leading"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['block-start'])]",
"from": "block-start",
"to": "top"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['inline-end'])]",
"from": "inline-end",
"to": "trailing"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['bottom'])]",
"from": "bottom",
"to": "bottom"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['end'])]",
"from": "end",
"to": "trailing"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['start'])]",
"from": "start",
"to": "leading"
},
{
"action": "replace-key",
"parent_node": "$.vp..*[?(@['padding'])]..*[?(@['top'])]",
"from": "top",
"to": "top"
},
{
"action": "add-node",
"parent_node": "$.vp..component.badge",
"key": "border-width",
"value": {
"value": "{vp.semantic.border-width.none}",
"type": "borderWidth"
}
},
{
"action": "add-node",
"parent_node": "$.vp..component.badge.color",
"key": "border",
"value": {
"value": "{vp.semantic.color.border.brand}",
"type": "color"
}
},
{
"action": "add-object-duplicate-child-in-each",
"parent_node": "$.vp..component.text-input.sizing",
"objects": [
"small",
"medium",
"large"
]
},
{
"action": "add-object-duplicate-child-in-each",
"parent_node": "$.vp..subcomponent.form.error.sizing",
"objects": [
"small",
"medium",
"large"
]
},
{
"action": "add-object-duplicate-child-in-each",
"parent_node": "$.vp..subcomponent.form.error.sizing",
"objects": [
"small",
"medium",
"large"
]
},
{
"action": "add-missing-padding",
"parent_node": "$..padding",
"forms": [
["bottom", "end", "start", "top"],
["block-end", "inline-start", "block-start", "inline-end"],
],
"value": {
"value" : "{vp.core.spacing.0}",
"type" : "spacing",
}
},
{
"action": "replace",
"path": "$..subcomponent.form.error.spacing.icon",
"value": {
"alignement" : {
"value" : ".firstTextBaseline",
"type" : "other",
}
}
},
{
"action": "remove",
"path": "$.node.to.remove"
}
]