adjusted query to check if type is null - added test for the case (#114)

Signed-off-by: Anders Kvanvig <anders.kvanvig@basefarm-orange.com>
Co-authored-by: Anders Kvanvig <anders.kvanvig@basefarm-orange.com>
This commit is contained in:
Akvanvig 2024-05-23 08:23:37 +02:00 committed by GitHub
parent c643cb5e73
commit a5a931958b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 2 deletions

View File

@ -489,9 +489,9 @@ func (c Config) Query(s string) (res string, err error) {
if err := json.Unmarshal(b, &dat); err != nil {
panic(err)
}
// Adding "// empty" to the query so the output does not include a "null" if the value is empty
// Adding some jq options to the query so the output does not include "null" if the value is empty
// This is not a json parse feature but a string one, so we should return normal values not json specific ones
query, err := gojq.Parse(s + "// empty")
query, err := gojq.Parse(s + " | if ( . | type) == \"null\" then empty else . end")
if err != nil {
return res, err
}

View File

@ -1095,6 +1095,7 @@ name: Mario
err = os.WriteFile(path.Join(tmpDir, "local_config.yaml"), []byte(`#cloud-config
local_key_1: local_value_1
local_key_2: false
some:
other:
key: 3
@ -1126,6 +1127,9 @@ some:
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal("3\n"))
Expect(c.Query("options")).To(Equal("foo: bar\n"))
v, err = c.Query("local_key_2")
Expect(err).ToNot(HaveOccurred())
Expect(v).To(Equal("false\n"))
})
})
})