Last change
on this file since 16620 was
16620,
checked in by hmaislin, 5 years ago
|
#2929: Reorganized folder structure for make script, removed explicit marshalling, erased go-side logging
|
File size:
1.0 KB
|
Rev | Line | |
---|
[16620] | 1 | package config |
---|
| 2 | |
---|
| 3 | import ( |
---|
| 4 | "bytes" |
---|
| 5 | "io" |
---|
| 6 | "strings" |
---|
| 7 | ) |
---|
| 8 | |
---|
| 9 | func ParseConfig(contents []byte, mapper func(field, value string, config interface{}) (err error), config interface{}) error { |
---|
| 10 | data := bytes.NewBuffer(contents) |
---|
| 11 | |
---|
| 12 | var field, value string |
---|
| 13 | var err error |
---|
| 14 | |
---|
| 15 | lineNum := 0 |
---|
| 16 | for { |
---|
| 17 | l, buferr := data.ReadString('\n') // parse line-by-line |
---|
| 18 | lineNum++ |
---|
| 19 | l = strings.TrimSpace(l) |
---|
| 20 | |
---|
| 21 | if buferr != nil { |
---|
| 22 | if buferr != io.EOF { |
---|
| 23 | return buferr |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | if len(l) == 0 { |
---|
| 27 | break |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | if len(l) == 0 { |
---|
| 32 | continue |
---|
| 33 | } // empty line |
---|
| 34 | |
---|
| 35 | // strip comments |
---|
| 36 | if p := strings.Index(l, "#"); p >= 0 { // comment |
---|
| 37 | l = l[:p] |
---|
| 38 | } |
---|
| 39 | // for comments that take a whole line |
---|
| 40 | l = strings.TrimSpace(l) |
---|
| 41 | if len(l) == 0 { |
---|
| 42 | continue |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | // fill field and value strings |
---|
| 46 | if p := strings.Index(l, "="); p >= 0 { // comment |
---|
| 47 | field = strings.TrimSpace(l[:p-1]) |
---|
| 48 | value = strings.TrimSpace(l[p+1:]) |
---|
| 49 | } else { |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | err = mapper(field, value, config) |
---|
| 53 | |
---|
| 54 | if err != nil { |
---|
| 55 | return err |
---|
| 56 | } |
---|
| 57 | // Reached end of data |
---|
| 58 | if buferr == io.EOF { |
---|
| 59 | break |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | return nil |
---|
| 63 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.