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 18, 2024
1 parent 2955669 commit aea5fc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
10 changes: 5 additions & 5 deletions internal/config/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
68 changes: 34 additions & 34 deletions internal/config/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -103,69 +103,69 @@ 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",
},
},
},
ReportConfig: ReportConfig{
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",
},
},
},
ReportConfig: ReportConfig{
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),
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestLavaMerger_Merge(t *testing.T) {
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
{
Summary: "Summary 1",
Summary: "Summary 2",
},
},
},
Expand All @@ -218,7 +218,7 @@ func TestLavaMerger_Merge(t *testing.T) {
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
{
Summary: "Summary 2",
Summary: "Summary 1",
},
},
},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aea5fc8

Please sign in to comment.