blueprintload: enable strict checking for toml
Add strict checking for toml keys in blueprints. This allows us to error early if there are unknown keys in a toml blueprint and helps our users by spotting e.g. typos early. This is similar to https://github.com/osbuild/bootc-image-builder/pull/549 (thanks Ondrej!).
This commit is contained in:
parent
bcdfda9b95
commit
d41bd9aa5b
2 changed files with 19 additions and 3 deletions
|
|
@ -17,10 +17,13 @@ func decodeToml(r io.Reader, what string) (*blueprint.Blueprint, error) {
|
||||||
dec := toml.NewDecoder(r)
|
dec := toml.NewDecoder(r)
|
||||||
|
|
||||||
var conf blueprint.Blueprint
|
var conf blueprint.Blueprint
|
||||||
_, err := dec.Decode(&conf)
|
metadata, err := dec.Decode(&conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot decode %q: %w", what, err)
|
return nil, fmt.Errorf("cannot decode %q: %w", what, err)
|
||||||
}
|
}
|
||||||
|
if len(metadata.Undecoded()) > 0 {
|
||||||
|
return nil, fmt.Errorf("cannot decode %q: unknown keys found: %v", what, metadata.Undecoded())
|
||||||
|
}
|
||||||
|
|
||||||
return &conf, nil
|
return &conf, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,17 @@ var testBlueprintTOML = `
|
||||||
name = "alice"
|
name = "alice"
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var testBlueprintJSONunknownKeys = `
|
||||||
|
{
|
||||||
|
"birds": {"name": "robin"}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
var testBlueprintTOMLunknownKeys = `
|
||||||
|
[[birds]]
|
||||||
|
name = "robin"
|
||||||
|
`
|
||||||
|
|
||||||
var expectedBlueprint = &blueprint.Blueprint{
|
var expectedBlueprint = &blueprint.Blueprint{
|
||||||
Customizations: &blueprint.Customizations{
|
Customizations: &blueprint.Customizations{
|
||||||
User: []blueprint.UserCustomization{
|
User: []blueprint.UserCustomization{
|
||||||
|
|
@ -55,9 +66,11 @@ func TestBlueprintLoadJSON(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{"bp.json", testBlueprintJSON, expectedBlueprint, ""},
|
{"bp.json", testBlueprintJSON, expectedBlueprint, ""},
|
||||||
{"bp.toml", testBlueprintTOML, expectedBlueprint, ""},
|
{"bp.toml", testBlueprintTOML, expectedBlueprint, ""},
|
||||||
{"bp.toml", "wrong-content", nil, `cannot decode .*/bp.toml": toml: `},
|
{"bp.toml", "wrong-content", nil, `cannot decode ".*/bp.toml": toml: `},
|
||||||
{"bp.json", "wrong-content", nil, `cannot decode .*/bp.json": invalid `},
|
{"bp.json", "wrong-content", nil, `cannot decode ".*/bp.json": invalid `},
|
||||||
{"bp", "wrong-content", nil, `unsupported file extension for "/.*/bp"`},
|
{"bp", "wrong-content", nil, `unsupported file extension for "/.*/bp"`},
|
||||||
|
{"bp.toml", testBlueprintTOMLunknownKeys, nil, `cannot decode ".*/bp.toml": unknown keys found: \[birds birds.name\]`},
|
||||||
|
{"bp.json", testBlueprintJSONunknownKeys, nil, `cannot decode ".*/bp.json": json: unknown field "birds"`},
|
||||||
} {
|
} {
|
||||||
blueprintPath := makeTestBlueprint(t, tc.fname, tc.content)
|
blueprintPath := makeTestBlueprint(t, tc.fname, tc.content)
|
||||||
bp, err := blueprintload.Load(blueprintPath)
|
bp, err := blueprintload.Load(blueprintPath)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue