mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-05 20:15:36 +00:00
Added flag to fix #2110
This commit is contained in:
parent
b9d9e2fbad
commit
6e8cc00030
@ -196,6 +196,7 @@ yq -P -oy sample.json
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
|
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.FixMergeAnchorToSpec, "yaml-fix-merge-anchor-to-spec", "", false, "Fix merge anchor to match YAML spec. Will default to true in late 2025")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter. The necessary directories will be created.")
|
rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter. The necessary directories will be created.")
|
||||||
if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil {
|
if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil {
|
||||||
|
|||||||
@ -272,7 +272,10 @@ func overrideEntry(node *CandidateNode, key *CandidateNode, value *CandidateNode
|
|||||||
log.Debugf("checking new content %v:%v", keyNode.Value, valueEl.Value.(*CandidateNode).Value)
|
log.Debugf("checking new content %v:%v", keyNode.Value, valueEl.Value.(*CandidateNode).Value)
|
||||||
if keyNode.Value == key.Value && keyNode.Alias == nil && key.Alias == nil {
|
if keyNode.Value == key.Value && keyNode.Alias == nil && key.Alias == nil {
|
||||||
log.Debugf("overridign new content")
|
log.Debugf("overridign new content")
|
||||||
valueEl.Value = value
|
if !ConfiguredYamlPreferences.FixMergeAnchorToSpec {
|
||||||
|
log.Warning("--yaml-fix-merge-anchor-to-spec is false; causing the merge anchor to override the existing value at %v which isn't to the yaml spec. This flag will default to true in late 2025.", keyNode.GetNicePath())
|
||||||
|
valueEl.Value = value
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
newEl = valueEl // move forward twice
|
newEl = valueEl // move forward twice
|
||||||
|
|||||||
@ -53,7 +53,54 @@ foobar:
|
|||||||
thing: foobar_thing
|
thing: foobar_thing
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var explodeWhenKeysExistDocument = `objects:
|
||||||
|
- &circle
|
||||||
|
name: circle
|
||||||
|
shape: round
|
||||||
|
- name: ellipse
|
||||||
|
!!merge <<: *circle
|
||||||
|
- !!merge <<: *circle
|
||||||
|
name: egg
|
||||||
|
`
|
||||||
|
|
||||||
|
var explodeWhenKeysExistLegacy = `D0, P[], (!!map)::objects:
|
||||||
|
- name: circle
|
||||||
|
shape: round
|
||||||
|
- name: circle
|
||||||
|
shape: round
|
||||||
|
- shape: round
|
||||||
|
name: egg
|
||||||
|
`
|
||||||
|
|
||||||
|
var explodeWhenKeysExistExpected = `D0, P[], (!!map)::objects:
|
||||||
|
- name: circle
|
||||||
|
shape: round
|
||||||
|
- name: ellipse
|
||||||
|
shape: round
|
||||||
|
- shape: round
|
||||||
|
name: egg
|
||||||
|
`
|
||||||
|
|
||||||
|
var fixedAnchorOperatorScenarios = []expressionScenario{
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "merge anchor after existing keys",
|
||||||
|
subdescription: "legacy: overrides existing keys",
|
||||||
|
document: explodeWhenKeysExistDocument,
|
||||||
|
expression: "explode(.)",
|
||||||
|
expected: []string{explodeWhenKeysExistExpected},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var anchorOperatorScenarios = []expressionScenario{
|
var anchorOperatorScenarios = []expressionScenario{
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "merge anchor after existing keys",
|
||||||
|
subdescription: "legacy: overrides existing keys",
|
||||||
|
document: explodeWhenKeysExistDocument,
|
||||||
|
expression: "explode(.)",
|
||||||
|
expected: []string{explodeWhenKeysExistLegacy},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
description: "merge anchor not map",
|
description: "merge anchor not map",
|
||||||
@ -291,3 +338,11 @@ func TestAnchorAliasOperatorScenarios(t *testing.T) {
|
|||||||
}
|
}
|
||||||
documentOperatorScenarios(t, "anchor-and-alias-operators", anchorOperatorScenarios)
|
documentOperatorScenarios(t, "anchor-and-alias-operators", anchorOperatorScenarios)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAnchorAliasOperatorAlignedToSpecScenarios(t *testing.T) {
|
||||||
|
ConfiguredYamlPreferences.FixMergeAnchorToSpec = true
|
||||||
|
for _, tt := range fixedAnchorOperatorScenarios {
|
||||||
|
testScenario(t, &tt)
|
||||||
|
}
|
||||||
|
ConfiguredYamlPreferences.FixMergeAnchorToSpec = false
|
||||||
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ type YamlPreferences struct {
|
|||||||
PrintDocSeparators bool
|
PrintDocSeparators bool
|
||||||
UnwrapScalar bool
|
UnwrapScalar bool
|
||||||
EvaluateTogether bool
|
EvaluateTogether bool
|
||||||
|
FixMergeAnchorToSpec bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultYamlPreferences() YamlPreferences {
|
func NewDefaultYamlPreferences() YamlPreferences {
|
||||||
@ -17,6 +18,7 @@ func NewDefaultYamlPreferences() YamlPreferences {
|
|||||||
PrintDocSeparators: true,
|
PrintDocSeparators: true,
|
||||||
UnwrapScalar: true,
|
UnwrapScalar: true,
|
||||||
EvaluateTogether: false,
|
EvaluateTogether: false,
|
||||||
|
FixMergeAnchorToSpec: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,6 +30,7 @@ func (p *YamlPreferences) Copy() YamlPreferences {
|
|||||||
PrintDocSeparators: p.PrintDocSeparators,
|
PrintDocSeparators: p.PrintDocSeparators,
|
||||||
UnwrapScalar: p.UnwrapScalar,
|
UnwrapScalar: p.UnwrapScalar,
|
||||||
EvaluateTogether: p.EvaluateTogether,
|
EvaluateTogether: p.EvaluateTogether,
|
||||||
|
FixMergeAnchorToSpec: p.FixMergeAnchorToSpec,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user