Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support merging boolean pointers #324

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion loader/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ var serviceSpecials = &specials{
}

func (s *specials) Transformer(t reflect.Type) func(dst, src reflect.Value) error {
// TODO this is a workaround waiting for imdario/mergo#131
if t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Bool {
return func(dst, src reflect.Value) error {
if dst.CanSet() && !src.IsNil() {
dst.Set(src)
}
return nil
}
}
if fn, ok := s.m[t]; ok {
return fn
}
Expand Down Expand Up @@ -113,7 +122,10 @@ func mergeServices(base, override []types.ServiceConfig) ([]types.ServiceConfig,
}

func _merge(baseService *types.ServiceConfig, overrideService *types.ServiceConfig) (*types.ServiceConfig, error) {
if err := mergo.Merge(baseService, overrideService, mergo.WithAppendSlice, mergo.WithOverride, mergo.WithTransformers(serviceSpecials)); err != nil {
if err := mergo.Merge(baseService, overrideService,
mergo.WithAppendSlice,
mergo.WithOverride,
mergo.WithTransformers(serviceSpecials)); err != nil {
return nil, err
}
if overrideService.Command != nil {
Expand Down
39 changes: 39 additions & 0 deletions loader/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,45 @@ func TestMergeUlimitsConfig(t *testing.T) {
)
}

func TestInitOverride(t *testing.T) {
var (
bt = true
bf = false
)
cases := []struct {
base *bool
override *bool
expect bool
}{
{
base: &bt,
override: &bf,
expect: false,
},
{
base: nil,
override: &bt,
expect: true,
},
{
base: &bt,
override: nil,
expect: true,
},
}
for _, test := range cases {
base := types.ServiceConfig{
Init: test.base,
}
override := types.ServiceConfig{
Init: test.override,
}
config, err := _merge(&base, &override)
assert.NilError(t, err)
assert.Check(t, *config.Init == test.expect)
}
}

func TestMergeServiceNetworkConfig(t *testing.T) {
specials := &specials{
m: map[reflect.Type]func(dst, src reflect.Value) error{
Expand Down