Skip to content

Commit

Permalink
introduce OmitEmpty in yaml processing pipeline
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof authored and glours committed Oct 24, 2024
1 parent 74c1d59 commit 156e22d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
2 changes: 2 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ func loadYamlFile(ctx context.Context, file types.ConfigFile, opts *Options, wor
return err
}

dict = OmitEmpty(dict)

// Canonical transformation can reveal duplicates, typically as ports can be a range and conflict with an override
dict, err = override.EnforceUnicity(dict)
return err
Expand Down
11 changes: 11 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3581,3 +3581,14 @@ services:
},
})
}

func TestOmitEmptyDNS(t *testing.T) {
p, err := loadYAML(`
name: load-empty-dsn
services:
test:
dns: ${UNSET_VAR}
`)
assert.NilError(t, err)
assert.Equal(t, len(p.Services["test"].DNS), 0)
}
2 changes: 1 addition & 1 deletion loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func Normalize(dict map[string]any, env types.Mapping) (map[string]any, error) {
}
services[name] = service
}

dict["services"] = services
}

setNameFromKey(dict)

return dict, nil
Expand Down
74 changes: 74 additions & 0 deletions loader/omitEmpty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package loader

import "github.com/compose-spec/compose-go/v2/tree"

var omitempty = []tree.Path{
"services.*.dns"}

// OmitEmpty removes empty attributes which are irrelevant when unset
func OmitEmpty(yaml map[string]any) map[string]any {
cleaned := omitEmpty(yaml, tree.NewPath())
return cleaned.(map[string]any)
}

func omitEmpty(data any, p tree.Path) any {
switch v := data.(type) {
case map[string]any:
for k, e := range v {
if isEmpty(e) && mustOmit(p) {
delete(v, k)
continue
}

v[k] = omitEmpty(e, p.Next(k))
}
return v
case []any:
var c []any
for _, e := range v {
if isEmpty(e) && mustOmit(p) {
continue
}

c = append(c, omitEmpty(e, p.Next("[]")))
}
return c
default:
return data
}
}

func mustOmit(p tree.Path) bool {
for _, pattern := range omitempty {
if p.Matches(pattern) {
return true
}
}
return false
}

func isEmpty(e any) bool {
if e == nil {
return true
}
if v, ok := e.(string); ok && v == "" {
return true
}
return false
}
10 changes: 10 additions & 0 deletions transform/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
transformers["services.*.extends"] = transformExtends
transformers["services.*.networks"] = transformServiceNetworks
transformers["services.*.volumes.*"] = transformVolumeMount
transformers["services.*.dns"] = transformStringOrList
transformers["services.*.devices.*"] = transformDeviceMapping
transformers["services.*.secrets.*"] = transformFileMount
transformers["services.*.configs.*"] = transformFileMount
Expand All @@ -48,6 +49,15 @@ func init() {
transformers["include.*"] = transformInclude
}

func transformStringOrList(data any, _ tree.Path, _ bool) (any, error) {
switch t := data.(type) {
case string:
return []any{t}, nil
default:
return data, nil
}
}

// Canonical transforms a compose model into canonical syntax
func Canonical(yaml map[string]any, ignoreParseError bool) (map[string]any, error) {
canonical, err := transform(yaml, tree.NewPath(), ignoreParseError)
Expand Down

0 comments on commit 156e22d

Please sign in to comment.