Skip to content

Commit

Permalink
allow override by 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 706338c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
6 changes: 6 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
configDetails.ConfigFiles[i] = file
}

configDict = markNullOverrides(configDict)

if !opts.SkipValidation {
if err := schema.Validate(configDict); err != nil {
return nil, err
Expand Down Expand Up @@ -333,6 +335,7 @@ func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
if !opts.SkipInterpolation {
return interp.Interpolate(yml, *opts.Interpolate)
}

return yml, err
}

Expand All @@ -341,6 +344,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
45 changes: 45 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,47 @@ 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: null # this should reset build section
`
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)
}
83 changes: 83 additions & 0 deletions loader/null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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"
)

func markNullOverrides(dict map[string]interface{}) map[string]interface{} {
for key, value := range dict {
if value == nil {
dict[key] = map[string]interface{}{
"x-null": true, // we use this as a marker for elements to be reset during merge
}
}
if d, ok := value.(map[string]interface{}); ok {
dict[key] = markNullOverrides(d)
}
}
return dict
}

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

func applyNullOverrides(i interface{}) error {
val := reflect.ValueOf(i)
val = reflect.Indirect(val)
typ := val.Type()
fmt.Println(typ.Name())

fields := typ.NumField()
for i := 0; i < fields; 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).Interface())
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)
elem := val.Field(i).Elem()
if !elem.CanSet() {
return fmt.Errorf("can't override attribute %s", name)
}
elem.SetZero()
continue
}
}
err := applyNullOverrides(val.Field(i).Interface())
if err != nil {
return err
}
}
}
return nil
}

0 comments on commit 706338c

Please sign in to comment.