mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-15 01:34:26 +02:00
Return an error if document is not a dictionary
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package yaml_utils
|
package yaml_utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@ -24,6 +25,10 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
|
|||||||
|
|
||||||
body := node.Content[0]
|
body := node.Content[0]
|
||||||
|
|
||||||
|
if body.Kind != yaml.MappingNode {
|
||||||
|
return yamlBytes, errors.New("yaml document is not a dictionary")
|
||||||
|
}
|
||||||
|
|
||||||
updateYamlNode(body, path, value)
|
updateYamlNode(body, path, value)
|
||||||
|
|
||||||
// Convert the updated YAML node back to YAML bytes.
|
// Convert the updated YAML node back to YAML bytes.
|
||||||
|
@ -56,6 +56,16 @@ func TestUpdateYamlValue(t *testing.T) {
|
|||||||
expectedOut: "foo:\n bar: qux\n",
|
expectedOut: "foo:\n bar: qux\n",
|
||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Error cases
|
||||||
|
{
|
||||||
|
name: "existing document is not a dictionary",
|
||||||
|
in: "42\n",
|
||||||
|
path: []string{"foo"},
|
||||||
|
value: "bar",
|
||||||
|
expectedOut: "42\n",
|
||||||
|
expectedErr: "yaml document is not a dictionary",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
Reference in New Issue
Block a user