Skip to content

Commit

Permalink
Suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Jul 13, 2021
1 parent 74e8678 commit b86d0a6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 49 deletions.
35 changes: 2 additions & 33 deletions kyaml/kio/byteio_readwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,12 @@ spec:
`,
},
{
name: "round_trip with mixed indentations in same resource, wide wins",
name: "round_trip with mixed indentations in same resource, wide wins as it is first",
input: `
apiVersion: apps/v1
kind: Deployment
spec:
- foo
- bar
- baz
env:
- foo
- bar
Expand All @@ -442,22 +440,18 @@ apiVersion: apps/v1
kind: Deployment
spec:
- foo
- bar
- baz
env:
- foo
- bar
`,
},
{
name: "round_trip with mixed indentations in same resource, compact wins",
name: "round_trip with mixed indentations in same resource, compact wins as it is first",
input: `
apiVersion: apps/v1
kind: Deployment
spec:
- foo
- bar
- baz
env:
- foo
- bar
Expand All @@ -467,31 +461,6 @@ apiVersion: apps/v1
kind: Deployment
spec:
- foo
- bar
- baz
env:
- foo
- bar
`,
},
{
name: "round_trip with mixed indentations in same resource, compact in case of a tie",
input: `
apiVersion: apps/v1
kind: Deployment
spec:
- foo
- bar
env:
- foo
- bar
`,
expectedOutput: `
apiVersion: apps/v1
kind: Deployment
spec:
- foo
- bar
env:
- foo
- bar
Expand Down
2 changes: 1 addition & 1 deletion kyaml/kio/byteio_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (w ByteWriter) Write(inputNodes []*yaml.RNode) error {
// don't wrap the elements
if w.WrappingKind == "" {
for i := range nodes {
if seqIndentsForNodes[i] == string(yaml.WideSeqIndent) {
if seqIndentsForNodes[i] == string(yaml.WideSequenceStyle) {
encoder.DefaultSeqIndent()
} else {
encoder.CompactSeqIndent()
Expand Down
12 changes: 6 additions & 6 deletions kyaml/yaml/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions kyaml/yaml/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
36 changes: 31 additions & 5 deletions kyaml/yaml/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
},
Expand Down

0 comments on commit b86d0a6

Please sign in to comment.