aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-06-18 14:09:32 +0700
committerShulhan <ms@kilabit.info>2019-06-18 14:09:32 +0700
commit9b95ec0755219146afbe7a854aa0d2cc65e15f97 (patch)
treed65cb625173cd68ac93a0c9ff0ee5170222a03a6
parent45432e60ecbc4c2bab43a9912ee7de108befd13b (diff)
downloadpakakeh.go-9b95ec0755219146afbe7a854aa0d2cc65e15f97.tar.xz
ini: create new section or variable if not exist on Set
Previous behaviour of Set() method will return false if the section or subsection of key to be set not found on database. This commit change the behaviour of Set(). If no section or subsection found on database, the new section with key-value will be created. If no key found, the new key-value will be added to the specific section.
-rw-r--r--lib/ini/ini.go18
-rw-r--r--lib/ini/ini_example_test.go3
-rw-r--r--lib/ini/section.go20
3 files changed, 32 insertions, 9 deletions
diff --git a/lib/ini/ini.go b/lib/ini/ini.go
index 3ea35114..1aca965e 100644
--- a/lib/ini/ini.go
+++ b/lib/ini/ini.go
@@ -120,7 +120,12 @@ func (in *Ini) Section(secName, subName string) (sec *Section) {
//
// 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.
+// If section or subsection is not found, the new section-subsection will be
+// created.
+// If key not found, the new key-value variable will be added to the section.
+//
+// It will return true if new key added or updated; otherwise it will return
+// false.
//
func (in *Ini) Set(secName, subName, key, value string) bool {
if len(secName) == 0 || len(key) == 0 {
@@ -131,7 +136,16 @@ func (in *Ini) Set(secName, subName, key, value string) bool {
sec := in.getSection(secName, subName)
if sec == nil {
- return false
+ sec = newSection(secName, subName)
+ v := &variable{
+ mode: lineModeValue,
+ key: key,
+ keyLower: strings.ToLower(key),
+ value: value,
+ }
+ sec.vars = append(sec.vars, v)
+ in.secs = append(in.secs, sec)
+ return true
}
key = strings.ToLower(key)
diff --git a/lib/ini/ini_example_test.go b/lib/ini/ini_example_test.go
index 1bdb5c3d..be51fa49 100644
--- a/lib/ini/ini_example_test.go
+++ b/lib/ini/ini_example_test.go
@@ -285,9 +285,12 @@ key=value1
//key=value4
//key2=false
//
+ //keynotexist = value4
//[section "sub"]
//key=value2
//key=value3
+ //[sectionnotexist "sub"]
+ //key = value3
}
func ExampleIni_Subs() {
diff --git a/lib/ini/section.go b/lib/ini/section.go
index d4eb6748..e482b5d3 100644
--- a/lib/ini/section.go
+++ b/lib/ini/section.go
@@ -306,20 +306,26 @@ func (sec *Section) replaceAll(key, value string) {
// If value is empty, it will be set to true.
//
func (sec *Section) set(key, value string) bool {
- if len(sec.vars) == 0 || len(key) == 0 {
+ if len(key) == 0 {
return false
}
- key = strings.ToLower(key)
-
- _, v := sec.getVariable(key)
- if v == nil {
- return false
- }
+ keyLower := strings.ToLower(key)
if len(value) == 0 {
value = varValueTrue
}
+ _, v := sec.getVariable(keyLower)
+ if v == nil {
+ v := &variable{
+ mode: lineModeValue,
+ key: key,
+ keyLower: strings.ToLower(key),
+ value: value,
+ }
+ sec.vars = append(sec.vars, v)
+ return true
+ }
v.value = value
return true