diff options
| author | Shulhan <ms@kilabit.info> | 2019-06-07 11:11:22 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-06-07 11:11:22 +0700 |
| commit | 9ac8ed292f48dd204b7eb6ab3b41903bc16c00da (patch) | |
| tree | da805ca07eeab587a37912d8e63b3e92215992bf | |
| parent | 04646412490f056157eff2e0f5b008db8e332919 (diff) | |
| download | pakakeh.go-9ac8ed292f48dd204b7eb6ab3b41903bc16c00da.tar.xz | |
ini: add method to get section object by section and/or subsection name
| -rw-r--r-- | lib/ini/ini.go | 31 | ||||
| -rw-r--r-- | lib/ini/ini_example_test.go | 34 | ||||
| -rw-r--r-- | lib/ini/section.go | 13 |
3 files changed, 78 insertions, 0 deletions
diff --git a/lib/ini/ini.go b/lib/ini/ini.go index d282337b..e27bf874 100644 --- a/lib/ini/ini.go +++ b/lib/ini/ini.go @@ -87,6 +87,37 @@ func (in *Ini) Add(secName, subName, key, value string) bool { } // +// Section given section and/or subsection name, return the Section object +// that match with it. +// If section name is empty, it will return nil. +// If ini contains duplicate section (or subsection) it will merge all +// of its variables into one section. +// +func (in *Ini) Section(secName, subName string) (sec *Section) { + if len(secName) == 0 { + return nil + } + + sec = &Section{ + name: secName, + sub: subName, + } + + secName = strings.ToLower(secName) + for x := 0; x < len(in.secs); x++ { + if secName != in.secs[x].nameLower { + continue + } + if subName != in.secs[x].sub { + continue + } + + sec.merge(in.secs[x]) + } + return +} + +// // Set the last variable's value in section-subsection that match with the // key. // If key found it will return true; otherwise it will return false. diff --git a/lib/ini/ini_example_test.go b/lib/ini/ini_example_test.go index 3ee93851..1bdb5c3d 100644 --- a/lib/ini/ini_example_test.go +++ b/lib/ini/ini_example_test.go @@ -205,6 +205,40 @@ func ExampleIni_Rebase() { // key = value1 } +func ExampleIni_Section() { + input := []byte(` +[section] +key=value1 # comment +key2= ; another comment + +[section "sub"] +key=value1 + +[section] ; here is comment on section +key=value2 +key2=false + +[section "sub"] +key=value2 +key=value1 +`) + + ini, err := Parse(input) + if err != nil { + log.Fatal(err) + } + + sec := ini.Section("section", "") + for _, v := range sec.vars { + fmt.Println(v.key, "=", v.value) + } + // Output: + // key = value1 + // key2 = true + // key = value2 + // key2 = false +} + func ExampleIni_Set() { input := []byte(` [section] diff --git a/lib/ini/section.go b/lib/ini/section.go index d30a6e3d..d4eb6748 100644 --- a/lib/ini/section.go +++ b/lib/ini/section.go @@ -274,6 +274,19 @@ func (sec *Section) gets(key string, defs []string) (vals []string, ok bool) { } // +// merge other Section variables on this section, ignoring empty or comment +// mode. +// +func (sec *Section) merge(other *Section) { + for x := 0; x < len(other.vars); x++ { + if !isLineModeVar(other.vars[x].mode) { + continue + } + sec.vars = append(sec.vars, other.vars[x]) + } +} + +// // replaceAll change the value of variable reference with `key` into new // `value`. This is basically `unsetAll` and `Add`. // |
