Skip to content

Commit 6082ed6

Browse files
committed
refactor: extract annotation setting logic
1 parent ea00827 commit 6082ed6

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

kyaml/kio/byteio_reader.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,24 +322,10 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode
322322
r.SetAnnotations = map[string]string{}
323323
}
324324
if !r.OmitReaderAnnotations {
325-
err := kioutil.CopyLegacyAnnotations(n)
325+
err := r.setResourceAnnotations(n, index, originalYAML)
326326
if err != nil {
327327
return nil, err
328328
}
329-
r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index)
330-
r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index)
331-
332-
if r.PreserveSeqIndent {
333-
// derive and add the seqindent annotation
334-
seqIndentStyle := yaml.DeriveSeqIndentStyle(originalYAML)
335-
if seqIndentStyle != "" {
336-
r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle
337-
}
338-
}
339-
340-
if index == 0 && r.PreserveInitialDocSep && node.Kind == yaml.DocumentNode {
341-
r.SetAnnotations[kioutil.InitialDocSepAnnotation] = "true"
342-
}
343329
}
344330
var keys []string
345331
for k := range r.SetAnnotations {
@@ -354,3 +340,21 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode
354340
}
355341
return n, nil
356342
}
343+
344+
func (r *ByteReader) setResourceAnnotations(n *yaml.RNode, index int, originalYAML string) error {
345+
err := kioutil.CopyLegacyAnnotations(n)
346+
if err != nil {
347+
return err
348+
}
349+
r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index)
350+
r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index)
351+
352+
if r.PreserveSeqIndent {
353+
// derive and add the seqindent annotation
354+
seqIndentStyle := yaml.DeriveSeqIndentStyle(originalYAML)
355+
if seqIndentStyle != "" {
356+
r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle
357+
}
358+
}
359+
return nil
360+
}

0 commit comments

Comments
 (0)