Suggested changes

This commit is contained in:
Phani Teja Marupaka
2021-07-12 23:27:09 -07:00
parent 74e867833a
commit 29be7fabe4
6 changed files with 54 additions and 116 deletions

View File

@@ -11,19 +11,19 @@ import (
)
const (
WideSeqIndent SeqIndentType = "wide"
CompactSeqIndent SeqIndentType = "compact"
DefaultIndent = 2
WideSequenceStyle SequenceIndentStyle = "wide"
CompactSequenceStyle SequenceIndentStyle = "compact"
DefaultIndent = 2
)
// SeqIndentType holds the indentation style for sequence nodes
type SeqIndentType string
type SequenceIndentStyle string
// EncoderOptions are options that can be used to configure the encoder,
// do not expose new options without considerable justification
type EncoderOptions struct {
// SeqIndent is the indentation style for YAML Sequence nodes
SeqIndent SeqIndentType
SeqIndent SequenceIndentStyle
}
// Expose the yaml.v3 functions so this package can be used as a replacement
@@ -69,7 +69,7 @@ func MarshalWithOptions(in interface{}, opts *EncoderOptions) ([]byte, error) {
func NewEncoderWithOptions(w io.Writer, opts *EncoderOptions) *yaml.Encoder {
encoder := NewEncoder(w)
encoder.SetIndent(DefaultIndent)
if opts.SeqIndent == WideSeqIndent {
if opts.SeqIndent == WideSequenceStyle {
encoder.DefaultSeqIndent()
} else {
encoder.CompactSeqIndent()

View File

@@ -29,20 +29,21 @@ func DeriveSeqIndentStyle(originalYAML string) string {
numSpacesBeforeKeyElem := len(keyLine) - len(strings.TrimLeft(keyLine, " "))
trimmedKeyLine := strings.Trim(keyLine, " ")
if strings.HasSuffix(trimmedKeyLine, "|") || strings.HasSuffix(trimmedKeyLine, "|-") {
if strings.Count(trimmedKeyLine, ":") != 1 || !strings.HasSuffix(trimmedKeyLine, ":") {
// if the key line doesn't contain only one : that too at the end,
// this is not a sequence node, it is a wrapped sequence node string
// ignore it
continue
}
if numSpacesBeforeSeqElem == numSpacesBeforeKeyElem {
return string(CompactSeqIndent)
return string(CompactSequenceStyle)
}
if numSpacesBeforeSeqElem-numSpacesBeforeKeyElem == 2 {
return string(WideSeqIndent)
return string(WideSequenceStyle)
}
}
return string(CompactSeqIndent)
return string(CompactSequenceStyle)
}

View File

@@ -79,16 +79,42 @@ env:
expectedOutput: `compact`,
},
{
name: "skip wrapped sequence strings",
name: "skip wrapped sequence strings, pipe hyphen",
input: `apiVersion: apps/v1
kind: Deployment
spec: |-
- foo
- bar
env:
- foo
- bar
- baz
`,
expectedOutput: `compact`,
},
{
name: "skip wrapped sequence strings, pipe",
input: `apiVersion: apps/v1
kind: Deployment
spec: |
- foo
- bar
`,
expectedOutput: `compact`,
},
{
name: "skip wrapped sequence strings, right angle bracket",
input: `apiVersion: apps/v1
kind: Deployment
spec: >
- foo
- bar
`,
expectedOutput: `compact`,
},
{
name: "skip wrapped sequence strings, plus",
input: `apiVersion: apps/v1
kind: Deployment
spec: +
- foo
- bar
`,
expectedOutput: `compact`,
},