Templates and defaults #2295
-
For the purposes of this example consider the following definition: #A: {foo: {[string]: string}} This is common in third-party definitions which have mapping, for example Kubernetes Now consider a situation where I would want to to create a definition based on this one, providing some defaults. I would first be inclined to write it like: #A: {foo: {[string]: string}}
#B: #A & {
foo: _ | *{
"a": "1"
"b": "2"
}
}
b: #B & {
foo: "c": "3"
} However this adds {
"b": {
"foo": {
"c": "3",
"a": "1",
"b": "2"
}
}
} The same goes for the following: #A: {foo: {[string]: string}}
#B: #A & {
foo: {[string]: string} | *{
"a": "1"
"b": "2"
}
}
b: #B & {
foo: "c": "3"
} {
"b": {
"foo": {
"c": "3",
"a": "1",
"b": "2"
}
}
} However, the following works as I would want/expect: #A: {foo: {[string]: string}}
#B: #A & {
foo: [string]: string | *{
"a": "1"
"b": "2"
}
}
b: #B & {
foo: "c": "3"
} {
"b": {
"foo": {
"c": "3"
}
}
} This feels like a bug, but I'm uncertain. If it isn't I think it should be documented somewhere. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I think the last example is structured as follows, with the parentheses. It does not seem possible to get the default value to be used. #A: {foo: {[string]: string}}
#B: #A & {
foo: [string]: (string | *{
"a": "1"
"b": "2"
})
}
b: #B & {} |
Beta Was this translation helpful? Give feedback.
Your answer was and is close :]
What was happening is that the extra field did not prevent the a&b from being included in the result. Open structs in disjunctions are often a source of confusion or indecision (on CUE's part). By closing the disjunction element, it only applies on an exact match, or in this case when nothing is presented.