Skip to content

Commit d4879f6

Browse files
committed
Upgrade additionalDisk entries from string to map when adding fields
Signed-off-by: Jan Dubois <[email protected]>
1 parent 770d8ac commit d4879f6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pkg/limatmpl/embed.go

+14
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ func (tmpl *Template) deleteListEntry(list string, idx int) {
272272
tmpl.expr.WriteString(fmt.Sprintf("| del($a.%s[%d], $b.%s[%d])\n", list, idx, list, idx))
273273
}
274274

275+
// upgradeListEntryStringToMapField turns list[idx] from a string to a {field: list[idx]} map
276+
func (tmpl *Template) upgradeListEntryStringToMapField(list string, idx int, field string) {
277+
// TODO the head_comment on the string becomes duplicated as a foot_comment on the new field; could be a yq bug?
278+
tmpl.expr.WriteString(fmt.Sprintf("| ($a.%s[%d] | select(type == \"!!str\")) |= {\"%s\": .}\n", list, idx, field))
279+
}
280+
275281
// combineListEntries combines entries based on a shared unique key.
276282
// If two entries share the same key, then any missing fields in the earlier entry are
277283
// filled in from the latter one. The latter one is then deleted.
@@ -322,18 +328,26 @@ func (tmpl *Template) combineAdditionalDisks() {
322328
}
323329
}
324330
for dst := from; dst <= to; dst++ {
331+
// upgrade additionalDisks[dst] from "disk" name string to {"name": "disk"} map so we can add fields
332+
upgradeDiskToMap := sync.OnceFunc(func() {
333+
tmpl.upgradeListEntryStringToMapField(additionalDisks, dst, "name")
334+
})
335+
325336
dest := &tmpl.Config.AdditionalDisks[dst]
326337
if dest.Format == nil && disk.Format != nil {
338+
upgradeDiskToMap()
327339
tmpl.copyListEntryField(additionalDisks, dst, src, "format")
328340
dest.Format = disk.Format
329341
}
330342
// TODO: Does it make sense to merge "fsType" and "fsArgs" independently of each other?
331343
if dest.FSType == nil && disk.FSType != nil {
344+
upgradeDiskToMap()
332345
tmpl.copyListEntryField(additionalDisks, dst, src, "fsType")
333346
dest.FSType = disk.FSType
334347
}
335348
// "fsArgs" are inherited all-or-nothing; they are not appended
336349
if len(dest.FSArgs) == 0 && len(disk.FSArgs) != 0 {
350+
upgradeDiskToMap()
337351
tmpl.copyListEntryField(additionalDisks, dst, src, "fsArgs")
338352
dest.FSArgs = disk.FSArgs
339353
}

pkg/limatmpl/embed_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ additionalDisks:
230230
- name: disk5
231231
`,
232232
},
233+
{
234+
// This test fails because the yq commands don't handle comments properly; may need to be fixed in yq
235+
"TODO additionalDisks will be upgraded from string to map",
236+
`#
237+
additionalDisks:
238+
# my head comment
239+
- mine # my line comment
240+
`,
241+
`
242+
# head comment
243+
additionalDisks: # line comment
244+
- name: "*"
245+
format: true # formatting is good for you
246+
`,
247+
`
248+
# head comment
249+
additionalDisks: # line comment
250+
# my head comment
251+
- name: mine # my line comment
252+
format: true # formatting is good for you
253+
`,
254+
},
255+
{
256+
// This entry can be deleted when the previous one no longer fails
257+
"additionalDisks will be upgraded from string to map (no comments version)",
258+
`additionalDisks: [mine]`,
259+
`additionalDisks: [{name: "*", format: true}]`,
260+
`additionalDisks: [{name: mine, format: true}]`,
261+
},
233262
{
234263
"networks without interface name are not merged",
235264
`

0 commit comments

Comments
 (0)