Skip to content

Commit

Permalink
#81 remove duplicate code and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 15, 2024
1 parent bfcbd7e commit 071289d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
45 changes: 1 addition & 44 deletions pkg/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ type DoguConfig struct {
Absent []common.DoguConfigKey
}

type SensitiveDoguConfig struct {
Present map[common.SensitiveDoguConfigKey]common.SensitiveDoguConfigValue
Absent []common.SensitiveDoguConfigKey
}
type SensitiveDoguConfig = DoguConfig

type GlobalConfig struct {
Present map[common.GlobalConfigKey]common.GlobalConfigValue
Expand Down Expand Up @@ -183,46 +180,6 @@ func (config DoguConfig) validate(referencedDoguName common.SimpleDoguName) erro
return errors.Join(errs...)
}

func (config SensitiveDoguConfig) validate(referencedDoguName common.SimpleDoguName) error {
var errs []error
for configKey := range config.Present {
err := configKey.Validate()
if err != nil {
errs = append(errs, fmt.Errorf("present dogu config key invalid: %w", err))
}

// validate that all keys are of the same dogu
if referencedDoguName != configKey.DoguName {
errs = append(errs, fmt.Errorf("present %s does not match superordinate dogu name %q", configKey, referencedDoguName))
}
}

for _, configKey := range config.Absent {
err := configKey.Validate()
if err != nil {
errs = append(errs, fmt.Errorf("absent dogu config key invalid: %w", err))
}

// absent keys cannot be present
_, isPresent := config.Present[configKey]
if isPresent {
errs = append(errs, fmt.Errorf("%s cannot be present and absent at the same time", configKey))
}

// validate that all keys are of the same dogu
if referencedDoguName != configKey.DoguName {
errs = append(errs, fmt.Errorf("absent %s does not match superordinate dogu name %q", configKey, referencedDoguName))
}
}

absentDuplicates := util.GetDuplicates(config.Absent)
if len(absentDuplicates) > 0 {
errs = append(errs, fmt.Errorf("absent dogu config should not contain duplicate keys: %v", absentDuplicates))
}

return errors.Join(errs...)
}

func (config GlobalConfig) validate() error {
var errs []error
for configKey := range config.Present {
Expand Down
62 changes: 62 additions & 0 deletions pkg/domain/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,65 @@ func TestCombinedDoguConfig_validate(t *testing.T) {

assert.ErrorContains(t, err, "dogu config key key \"my/key1\" of dogu \"dogu1\" cannot be in normal and sensitive configuration at the same time")
}

func TestConfig_GetDogusWithChangedConfig(t *testing.T) {
presentConfig := map[common.DoguConfigKey]common.DoguConfigValue{
dogu1Key1: "val",
}
AbsentConfig := []common.DoguConfigKey{
dogu1Key1,
}

normalConfig := DoguConfig{
Present: presentConfig,
Absent: AbsentConfig,
}

combinedConfig := CombinedDoguConfig{
DoguName: dogu1,
Config: normalConfig,
SensitiveConfig: struct {
Present map[common.DoguConfigKey]common.DoguConfigValue
Absent []common.DoguConfigKey
}{},
}

config := Config{
Dogus: map[common.SimpleDoguName]CombinedDoguConfig{
dogu1: combinedConfig,
},
Global: GlobalConfig{},
}
assert.Equal(t, []common.SimpleDoguName{dogu1}, config.GetDogusWithChangedConfig())
}

func TestConfig_GetDogusWithChangedSensitiveConfig(t *testing.T) {
presentConfig := map[common.DoguConfigKey]common.DoguConfigValue{
dogu1Key1: "val",
}
AbsentConfig := []common.DoguConfigKey{
dogu1Key1,
}

normalConfig := DoguConfig{
Present: presentConfig,
Absent: AbsentConfig,
}

combinedConfig := CombinedDoguConfig{
DoguName: dogu1,
Config: normalConfig,
SensitiveConfig: struct {
Present map[common.DoguConfigKey]common.DoguConfigValue
Absent []common.DoguConfigKey
}{},
}

config := Config{
Dogus: map[common.SimpleDoguName]CombinedDoguConfig{
dogu1: combinedConfig,
},
Global: GlobalConfig{},
}
assert.Equal(t, []common.SimpleDoguName{dogu1}, config.GetDogusWithChangedConfig())
}

0 comments on commit 071289d

Please sign in to comment.