Alert when I have two concrete types with different set of properties #2660
-
Is there a way to ask cue to help me dry this up?
I have so many pets that I didn't realize I defined sam twice and I was hoping cue could notify me and then I'd manually update pets.cue to consolidate. I was hoping I've not played with go api but maybe count initial list and |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This particular example is meant to work in CUE since multiple declarations get unified into one; One solution, although quite repetitive, would be to close your values so that one declaration omitting an optional field doesn't allow another to add it later via unification: https://tip.cuelang.org/play/?id=wJD76Ii2lzX#cue@export@cue You could indeed use the Go API to ensure that the fields in the top-level object are unique. For example, with the Go API, you could parse the syntax tree, iterate over the top-level fields, and error if any two of them are identifiers with the same name. This would limit your options in terms of what you can do at the top level scope, but if that prevents bugs in a very large config of yours, it might be worthwhile. Note that it wouldn't be foolproof though, e.g. it would be hard to detect that this has duplicates as well:
Personally I wouldn't go the Go API route, since it would go a bit against the language principles, and it wouldn't catch any non-trivial mistakes like the one above. This might be a bit of an XY problem - what is it you're trying to solve? For example, if what you want is to consolidate your config by deduplicating declarations, you could look at |
Beta Was this translation helpful? Give feedback.
-
gives me
which solves my aggregate-all-sams-into-one-place-in-order-to-clean-up-my-data-file problem. But then I'm a little surprised --simplify and trim doesn't squash this into one #Pet.
My use case: I'm not wrangling Kubernetes clusters but I am excited about knowing how to wrangle cue properly so pets and shopping lists happen to be my data files at the moment. Your close() and warning are helpful tips, thank you Daniel. |
Beta Was this translation helpful? Give feedback.
This particular example is meant to work in CUE since multiple declarations get unified into one;
foo: string
andfoo: "bar"
is the same asfoo: string & "bar"
. CUE is not imperative and fields get unified, so there's no such thing as "sam
has already been declared before so it can't be declared again".One solution, although quite repetitive, would be to close your values so that one declaration omitting an optional field doesn't allow another to add it later via unification: https://tip.cuelang.org/play/?id=wJD76Ii2lzX#cue@export@cue
You could indeed use the Go API to ensure that the fields in the top-level object are unique. For example, with the Go API, you could parse the syntax tree…