CUE Lang: Simplifying Labels With Ast/astutil
Hey everyone, let's dive into a cool optimization happening in the CUE language, specifically in how it handles labels. We're talking about the ast/astutil
package and how it's evolving to make your CUE code cleaner and more readable. The main goal? To simplify labels without necessarily printing them out, which is super useful when you're working with things like JSON Schemas. This whole discussion stems from a need to make the CUE language more user-friendly and efficient, especially when dealing with complex data structures. It's all about making the code more streamlined, easier to read, and less cluttered with unnecessary quotes.
The Current Label Simplification Process
Currently, label simplification is handled by cue/format.Simplify
. This method is part of the formatter, which means it kicks in when you're printing out CUE values. It's a neat feature, but it's also a bit limiting. Imagine you're decoding a JSON Schema. You don't always want quotes around your labels, right? They're often unnecessary and just add visual noise. The current setup forces you to print the CUE values to simplify the labels, which isn't ideal if you just need to clean up the AST (Abstract Syntax Tree) without actually printing anything. We want a way to simplify labels directly at the AST level. This is where astutil.Sanitize
comes into play. The goal is to integrate label simplification into astutil.Sanitize
, giving you the power to clean up your labels without going through the formatting process.
Why Move Label Simplification to astutil.Sanitize?
So, why are we making this change? The primary reason is to streamline the process and avoid having the same functionality in two different places. It's a bit odd to have label simplification tied to the formatting process. Plus, it's not always desirable to print CUE code just to simplify labels. Moving this feature to astutil.Sanitize
provides a more direct and efficient way to achieve the desired result. By doing this, we can directly manipulate the AST, cleaning up labels as needed, especially when dealing with scenarios like decoding JSON Schemas. This approach ensures that the resulting schema doesn’t unnecessarily use quotes everywhere, making it more readable and user-friendly. The change is all about improving the overall developer experience by providing a more intuitive and efficient way to work with CUE code and manage labels within the AST.
The Benefits
- Cleaner Code: Removing unnecessary quotes around labels makes your code easier to read and understand.
- Efficiency: Simplifying labels directly at the AST level saves time and resources.
- Flexibility: Allows for more control over the label simplification process, especially when dealing with complex data structures.
The Proposed Solution: astutil.Sanitize
Here's the plan: We're going to make astutil.Sanitize
the go-to function for label simplification. This means you'll be able to use it to clean up your labels without having to print out the CUE values first. This is a much more direct and efficient approach, especially when you're working with JSON Schemas or other complex data structures where clean, readable code is crucial. The idea is to integrate the label simplification functionality directly into astutil.Sanitize
. This will allow users to simplify labels at the AST level without having to go through the formatting process. This will be particularly useful when dealing with scenarios like decoding JSON Schemas, where you don't want unnecessary quotes cluttering up your code. In essence, we're moving the label simplification functionality to a more appropriate and accessible location.
Handling User Concerns
Now, some of you might be thinking, "What if I don't like this change?" We've got you covered! If users aren't thrilled with how astutil.Sanitize
handles label simplification, we'll consider creating a new