Skip to content

Commit

Permalink
internal/config: fix CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
seilagamo committed Sep 19, 2024
1 parent aea5fc8 commit df53eef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
17 changes: 5 additions & 12 deletions internal/config/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ import (
"dario.cat/mergo"
)

// Merger has the ability to merge two configurations.
type Merger interface {
Merge(dst, src Config) (Config, error)
}

// LavaMerger represents a merger for the Lava configuration.
type LavaMerger struct{}

// Merge merges two configurations. The values of the configuration
// passed as first parameter will override those in the configuration
// passed as second parameter.
func (lm LavaMerger) Merge(dst, src Config) (Config, error) {
// merge merges two configurations. The values of the configuration
// passed as first parameter will be overridden by those in the
// configuration passed as second parameter avoiding overriding with
// nil values.
func merge(dst, src Config) (Config, error) {
merged := Config{}
mergeOpts := []func(*mergo.Config){
mergo.WithOverride,
Expand Down
31 changes: 15 additions & 16 deletions internal/config/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import (
func TestLavaMerger_Merge(t *testing.T) {
tests := []struct {
name string
cfg1 Config
cfg2 Config
dst Config
src Config
want Config
wantErr bool
}{
{
name: "Two empty configurations",
cfg1: Config{},
cfg2: Config{},
dst: Config{},
src: Config{},
want: Config{},
wantErr: false,
},
{
name: "Simple case",
cfg1: Config{},
cfg2: Config{
dst: Config{},
src: Config{
LavaVersion: ptr("v1.0.0"),
},
want: Config{
Expand All @@ -37,7 +37,7 @@ func TestLavaMerger_Merge(t *testing.T) {
},
{
name: "Settings with default values won't override",
cfg1: Config{
dst: Config{
LavaVersion: ptr("v1.0.0"),
AgentConfig: AgentConfig{
PullPolicy: ptr(agentconfig.PullPolicyAlways),
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestLavaMerger_Merge(t *testing.T) {
Metrics: ptr("metrics.json"),
},
},
cfg2: Config{},
src: Config{},
want: Config{
LavaVersion: ptr("v1.0.0"),
AgentConfig: AgentConfig{
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestLavaMerger_Merge(t *testing.T) {
},
{
name: "Override value",
cfg1: Config{
dst: Config{
LavaVersion: ptr("v1.0.0"),
AgentConfig: AgentConfig{
PullPolicy: ptr(agentconfig.PullPolicyAlways),
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestLavaMerger_Merge(t *testing.T) {
Metrics: ptr("metrics2.json"),
},
},
cfg2: Config{
src: Config{
LavaVersion: ptr("v1.0.1"),
AgentConfig: AgentConfig{
PullPolicy: ptr(agentconfig.PullPolicyNever),
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestLavaMerger_Merge(t *testing.T) {
},
{
name: "Append Exclusions",
cfg1: Config{
dst: Config{
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
{
Expand All @@ -214,7 +214,7 @@ func TestLavaMerger_Merge(t *testing.T) {
},
},
},
cfg2: Config{
src: Config{
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
{
Expand All @@ -239,7 +239,7 @@ func TestLavaMerger_Merge(t *testing.T) {
},
{
name: "Duplicated Exclusions",
cfg1: Config{
dst: Config{
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
{
Expand All @@ -248,7 +248,7 @@ func TestLavaMerger_Merge(t *testing.T) {
},
},
},
cfg2: Config{
src: Config{
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
{
Expand All @@ -270,8 +270,7 @@ func TestLavaMerger_Merge(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lm := LavaMerger{}
got, err := lm.Merge(tt.cfg1, tt.cfg2)
got, err := merge(tt.dst, tt.src)
if (err != nil) != tt.wantErr {
t.Errorf("unexpected error value: %v", err)
}
Expand Down

0 comments on commit df53eef

Please sign in to comment.