Skip to content

Commit

Permalink
Store last update to a field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaholaz committed May 13, 2024
1 parent 0646424 commit 0483f5e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func LoadConfig(filename string, config interface{}) error {
return errors.New("config argument must be a pointer to a struct")
}

overwritten := make(map[string]bool)
lastUpdate := make(map[string]uint)
for _, field := range reflect.VisibleFields(configReflect.Type()) {
overwritten[field.Name] = false
lastUpdate[field.Name] = 0
}

f, err := os.Open(filename)
Expand All @@ -113,7 +113,7 @@ func LoadConfig(filename string, config interface{}) error {
defer f.Close()
fh := bufio.NewScanner(f)

lineNr := 0
lineNr := uint(0)
syntaxError := func(message string) error {
return fmt.Errorf("syntax error parsing config (%s:%d): %s", filename, lineNr, message)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func LoadConfig(filename string, config interface{}) error {
switch field.Kind() {
case reflect.Slice:
// Create a empty slice, if no slice exists for this key already.
if field.IsNil() || !overwritten[*key] {
if field.IsNil() || lastUpdate[*key] == 0 {
field.Set(reflect.MakeSlice(field.Type(), 0, 0))
}

Expand All @@ -171,7 +171,7 @@ func LoadConfig(filename string, config interface{}) error {
}
field.Set(v)
}
overwritten[*key] = true
lastUpdate[*key] = lineNr
}

return nil
Expand Down

0 comments on commit 0483f5e

Please sign in to comment.