From 9670eb3d296a8b32a9a1b71c1ef71e29a6815735 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 30 Nov 2022 16:33:50 +0100 Subject: [PATCH] handle bool pointer override Signed-off-by: Nicolas De Loof --- loader/merge.go | 14 +++++++++++++- loader/merge_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/loader/merge.go b/loader/merge.go index ce7c1dcf..019ebac7 100644 --- a/loader/merge.go +++ b/loader/merge.go @@ -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 } @@ -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 { diff --git a/loader/merge_test.go b/loader/merge_test.go index 4f173b11..bbe32cd2 100644 --- a/loader/merge_test.go +++ b/loader/merge_test.go @@ -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{