aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-05-25 05:24:05 +0700
committerShulhan <ms@kilabit.info>2019-05-25 05:24:25 +0700
commit3dcaabfd97520d927bcef5074868631ee4b44747 (patch)
tree3e29c65c378b3fdd6e493d726a29e603886d9d55
parentb986ada275d7682feee761d678f0d79a6c43f75d (diff)
downloadpakakeh.go-3dcaabfd97520d927bcef5074868631ee4b44747.tar.xz
ini: add method to Rebase other INI object
Basically, Rebase merge each sections in other INI object on top of current INI object.
-rw-r--r--lib/ini/ini.go9
-rw-r--r--lib/ini/ini_example_test.go52
2 files changed, 61 insertions, 0 deletions
diff --git a/lib/ini/ini.go b/lib/ini/ini.go
index 01807d5b..7c94a0ac 100644
--- a/lib/ini/ini.go
+++ b/lib/ini/ini.go
@@ -303,6 +303,15 @@ func mergeSection(secs []*Section, newSec *Section) []*Section {
}
//
+// Rebase merge the other INI sections into this INI sections.
+//
+func (in *Ini) Rebase(other *Ini) {
+ for _, otherSec := range other.secs {
+ in.secs = mergeSection(in.secs, otherSec)
+ }
+}
+
+//
// Save the current parsed Ini into file `filename`. It will overwrite the
// destination file if it's exist.
//
diff --git a/lib/ini/ini_example_test.go b/lib/ini/ini_example_test.go
index 8ea5efbd..328ce780 100644
--- a/lib/ini/ini_example_test.go
+++ b/lib/ini/ini_example_test.go
@@ -106,3 +106,55 @@ key=value1
// key = value2
// key = value1
}
+
+func ExampleIni_Rebase() {
+ input := []byte(`
+ [section]
+ key=value1
+ key2=
+
+ [section "sub"]
+ key=value1
+`)
+
+ other := []byte(`
+ [section]
+ key=value2
+ key2=false
+
+ [section "sub"]
+ key=value2
+ key=value1
+`)
+
+ in, err := Parse(input)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ in2, err := Parse(other)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ in.Prune()
+ in2.Prune()
+
+ in.Rebase(in2)
+
+ for _, sec := range in.secs {
+ fmt.Printf("%s", sec)
+ for _, v := range sec.Vars {
+ fmt.Printf("%s", v)
+ }
+ }
+ // Output:
+ // [section]
+ // key = value1
+ // key2 = true
+ // key = value2
+ // key2 = false
+ // [section "sub"]
+ // key = value2
+ // key = value1
+}