Skip to content

Commit

Permalink
allow override to REMOVE elements by using x-null
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Mar 29, 2023
1 parent e0f9c51 commit 7e31c75
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ const extensions = "#extensions" // Using # prefix, we prevent risk to conflict
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
extras := map[string]interface{}{}
for key, value := range dict {
if key == extensions {
continue
}
if strings.HasPrefix(key, "x-") {
extras[key] = value
delete(dict, key)
Expand Down
5 changes: 5 additions & 0 deletions loader/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func merge(configs []*types.Config) (*types.Config, error) {
if err != nil {
return base, errors.Wrapf(err, "cannot merge extensions from %s", override.Filename)
}

err = applyNullOverrides(base)
if err != nil {
return nil, err
}
}
return base, nil
}
Expand Down
46 changes: 46 additions & 0 deletions loader/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"reflect"
"testing"

"github.com/compose-spec/compose-go/consts"
"github.com/imdario/mergo"

"github.com/compose-spec/compose-go/types"
Expand Down Expand Up @@ -1364,3 +1365,48 @@ func TestMergeExtraHosts(t *testing.T) {
},
)
}

func TestLoadWithNullOverride(t *testing.T) {
base := `
name: test
services:
foo:
build:
context: .
dockerfile: foo.Dockerfile
`
override := `
services:
foo:
image: foo
build:
x-null: # this will reset build section to nil
`
configDetails := types.ConfigDetails{
Environment: map[string]string{},
ConfigFiles: []types.ConfigFile{
{Filename: "base.yml", Content: []byte(base)},
{Filename: "override.yml", Content: []byte(override)},
},
}
config, err := loadTestProject(configDetails)
assert.NilError(t, err)
assert.DeepEqual(t, &types.Project{
Name: "test",
WorkingDir: "",
Services: []types.ServiceConfig{
{
Name: "foo",
Image: "foo",
Environment: types.MappingWithEquals{},
Scale: 1,
},
},
Networks: types.Networks{},
Volumes: types.Volumes{},
Secrets: types.Secrets{},
Configs: types.Configs{},
Extensions: types.Extensions{},
Environment: map[string]string{consts.ComposeProjectName: "test"},
}, config)
}
74 changes: 74 additions & 0 deletions loader/null.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 (
"fmt"
"reflect"

"github.com/sirupsen/logrus"
)

var null = reflect.ValueOf("x-null")

func applyNullOverrides(i interface{}) error {
return _applyNullOverrides(reflect.ValueOf(i))
}

func _applyNullOverrides(val reflect.Value) error {
val = reflect.Indirect(val)
typ := val.Type()
if typ.Kind() != reflect.Struct {
return nil
}

for i := 0; i < typ.NumField(); i++ {
v := reflect.Indirect(val.Field(i))

name := typ.Field(i).Name
switch v.Kind() {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
err := _applyNullOverrides(v.Index(i))
if err != nil {
return err
}
}
case reflect.Struct:
// search for Extensions["x-null"]. If set, reset field
extensions := v.FieldByName("Extensions")
if extensions.IsValid() {
xNull := extensions.MapIndex(null)
if xNull.IsValid() {
logrus.Debugf("%s reset to null", name)
f := val.Field(i)
if !f.CanSet() {
return fmt.Errorf("can't override attribute %s", name)
}
// f.SetZero() requires go 1.20
f.Set(reflect.Zero(f.Type()))
continue
}
}
err := _applyNullOverrides(val.Field(i))
if err != nil {
return err
}
}
}
return nil
}

0 comments on commit 7e31c75

Please sign in to comment.