From aea5fc83e008cbd7f3f88971479cee1a4740950e Mon Sep 17 00:00:00 2001 From: Seila Gamo Date: Wed, 18 Sep 2024 13:14:38 +0200 Subject: [PATCH] internal/config: fix CR issues --- internal/config/merge.go | 10 +++--- internal/config/merge_test.go | 68 +++++++++++++++++------------------ 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/internal/config/merge.go b/internal/config/merge.go index 35ec054..4c3dedc 100644 --- a/internal/config/merge.go +++ b/internal/config/merge.go @@ -10,7 +10,7 @@ import ( // Merger has the ability to merge two configurations. type Merger interface { - Merge(dest, src Config) (Config, error) + Merge(dst, src Config) (Config, error) } // LavaMerger represents a merger for the Lava configuration. @@ -19,18 +19,18 @@ 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(dest, src Config) (Config, error) { +func (lm LavaMerger) Merge(dst, src Config) (Config, error) { merged := Config{} mergeOpts := []func(*mergo.Config){ mergo.WithOverride, mergo.WithoutDereference, mergo.WithAppendSlice, } + if err := mergo.Merge(&merged, dst, mergeOpts...); err != nil { + return Config{}, fmt.Errorf("merging dst config into new config: %w", err) + } if err := mergo.Merge(&merged, src, mergeOpts...); err != nil { return Config{}, fmt.Errorf("merging src config into new config: %w", err) } - if err := mergo.Merge(&merged, dest, mergeOpts...); err != nil { - return Config{}, fmt.Errorf("merging dest config into new config: %w", err) - } return merged, nil } diff --git a/internal/config/merge_test.go b/internal/config/merge_test.go index e415d2d..0fe6872 100644 --- a/internal/config/merge_test.go +++ b/internal/config/merge_test.go @@ -26,20 +26,19 @@ func TestLavaMerger_Merge(t *testing.T) { }, { name: "Simple case", - cfg1: Config{ - LavaVersion: ptr("1.0.0"), + cfg1: Config{}, + cfg2: Config{ + LavaVersion: ptr("v1.0.0"), }, - cfg2: Config{}, want: Config{ - LavaVersion: ptr("1.0.0"), + LavaVersion: ptr("v1.0.0"), }, wantErr: false, }, { name: "Settings with default values won't override", - cfg1: Config{}, - cfg2: Config{ - LavaVersion: ptr("1.0.0"), + cfg1: Config{ + LavaVersion: ptr("v1.0.0"), AgentConfig: AgentConfig{ PullPolicy: ptr(agentconfig.PullPolicyAlways), Parallel: ptr(4), @@ -67,8 +66,9 @@ func TestLavaMerger_Merge(t *testing.T) { Metrics: ptr("metrics.json"), }, }, + cfg2: Config{}, want: Config{ - LavaVersion: ptr("1.0.0"), + LavaVersion: ptr("v1.0.0"), AgentConfig: AgentConfig{ PullPolicy: ptr(agentconfig.PullPolicyAlways), Parallel: ptr(4), @@ -103,19 +103,19 @@ func TestLavaMerger_Merge(t *testing.T) { { name: "Override value", cfg1: Config{ - LavaVersion: ptr("1.0.1"), + LavaVersion: ptr("v1.0.0"), AgentConfig: AgentConfig{ - PullPolicy: ptr(agentconfig.PullPolicyNever), - Parallel: ptr(3), + PullPolicy: ptr(agentconfig.PullPolicyAlways), + Parallel: ptr(4), Vars: map[string]string{ - "VAR1": "value1", - "VAR2": "value2", + "VAR3": "value3", + "VAR4": "value4", }, RegistryAuths: []RegistryAuth{ { - Server: "server", - Username: "username", - Password: "password", + Server: "server2", + Username: "username2", + Password: "password2", }, }, }, @@ -123,30 +123,30 @@ func TestLavaMerger_Merge(t *testing.T) { Severity: ptr(SeverityCritical), ShowSeverity: ptr(SeverityLow), Format: ptr(OutputFormatJSON), - OutputFile: ptr("outputfile1.json"), - ErrorOnStaleExclusions: ptr(false), + OutputFile: ptr("outputfile2.json"), + ErrorOnStaleExclusions: ptr(true), Exclusions: []Exclusion{ { - Summary: "Summary 1", + Summary: "Summary 2", }, }, Metrics: ptr("metrics2.json"), }, }, cfg2: Config{ - LavaVersion: ptr("1.0.0"), + LavaVersion: ptr("v1.0.1"), AgentConfig: AgentConfig{ - PullPolicy: ptr(agentconfig.PullPolicyAlways), - Parallel: ptr(4), + PullPolicy: ptr(agentconfig.PullPolicyNever), + Parallel: ptr(3), Vars: map[string]string{ - "VAR3": "value3", - "VAR4": "value4", + "VAR1": "value1", + "VAR2": "value2", }, RegistryAuths: []RegistryAuth{ { - Server: "server2", - Username: "username2", - Password: "password2", + Server: "server", + Username: "username", + Password: "password", }, }, }, @@ -154,18 +154,18 @@ func TestLavaMerger_Merge(t *testing.T) { Severity: ptr(SeverityCritical), ShowSeverity: ptr(SeverityLow), Format: ptr(OutputFormatJSON), - OutputFile: ptr("outputfile2.json"), - ErrorOnStaleExclusions: ptr(true), + OutputFile: ptr("outputfile1.json"), + ErrorOnStaleExclusions: ptr(false), Exclusions: []Exclusion{ { - Summary: "Summary 2", + Summary: "Summary 1", }, }, Metrics: ptr("metrics2.json"), }, }, want: Config{ - LavaVersion: ptr("1.0.1"), + LavaVersion: ptr("v1.0.1"), AgentConfig: AgentConfig{ PullPolicy: ptr(agentconfig.PullPolicyNever), Parallel: ptr(3), @@ -209,7 +209,7 @@ func TestLavaMerger_Merge(t *testing.T) { ReportConfig: ReportConfig{ Exclusions: []Exclusion{ { - Summary: "Summary 1", + Summary: "Summary 2", }, }, }, @@ -218,7 +218,7 @@ func TestLavaMerger_Merge(t *testing.T) { ReportConfig: ReportConfig{ Exclusions: []Exclusion{ { - Summary: "Summary 2", + Summary: "Summary 1", }, }, }, @@ -273,7 +273,7 @@ func TestLavaMerger_Merge(t *testing.T) { lm := LavaMerger{} got, err := lm.Merge(tt.cfg1, tt.cfg2) if (err != nil) != tt.wantErr { - t.Errorf("unexpected error: want: %v, got: %v", tt.wantErr, err) + t.Errorf("unexpected error value: %v", err) } if diff := cmp.Diff(tt.want, got); diff != "" { t.Errorf("configs mismatch (-want +got):\n%v", diff)