summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-01-14 19:23:06 +0700
committerShulhan <ms@kilabit.info>2024-01-14 19:23:06 +0700
commit3f26ff7582515fb13c27b366ebebcae0f88551f9 (patch)
treec6d33733204707120e45cd77a7981e22d3a14d3e
parent1b3f5599ee2e5ef7360ca6eea6d30550fdb29512 (diff)
downloadpakakeh.go-3f26ff7582515fb13c27b366ebebcae0f88551f9.tar.xz
lib/ini: add method Keys
The Keys method return sorted list of all section, subsection, and variables as string where each of them separated by ":", for example "section:sub:var".
-rw-r--r--lib/ini/ini.go15
-rw-r--r--lib/ini/ini_example_test.go43
2 files changed, 58 insertions, 0 deletions
diff --git a/lib/ini/ini.go b/lib/ini/ini.go
index ee9bfdc1..92600ea1 100644
--- a/lib/ini/ini.go
+++ b/lib/ini/ini.go
@@ -613,6 +613,21 @@ func (in *Ini) GetsUniq(secName, subName, key string, caseSensitive bool) (out [
return libstrings.Uniq(in.Gets(secName, subName, key), caseSensitive)
}
+// Keys return sorted list of all section, subsection, and variables as
+// string where each of them separated by ":", for example
+// "section:sub:var".
+func (in *Ini) Keys() (keys []string) {
+ var (
+ mapKeyValue = in.AsMap(``, ``)
+ key string
+ )
+ for key = range mapKeyValue {
+ keys = append(keys, key)
+ }
+ sort.Strings(keys)
+ return keys
+}
+
// Prune remove all empty lines, comments, and merge all section and
// subsection with the same name into one group.
func (in *Ini) Prune() {
diff --git a/lib/ini/ini_example_test.go b/lib/ini/ini_example_test.go
index d61055ae..4bd3b98f 100644
--- a/lib/ini/ini_example_test.go
+++ b/lib/ini/ini_example_test.go
@@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
+ "strings"
"time"
)
@@ -1048,3 +1049,45 @@ func ExampleIni_Vars() {
//
// key = value3
}
+
+func ExampleIni_Keys() {
+ const iniContent = `
+[section1]
+key3 = 3
+key2 = 2
+
+[section2 "sub 1"]
+key3 = 3
+key2 = 2
+
+[section1]
+key1 = 1
+
+[section2 "sub 1"]
+key2 = 2.2
+key1 = 1
+`
+
+ var (
+ ini *Ini
+ err error
+ )
+
+ ini, err = Parse([]byte(iniContent))
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var (
+ keys = ini.Keys()
+ gotkeys = strings.Join(keys, "\n")
+ )
+ fmt.Println(gotkeys)
+ // Output:
+ // section1::key1
+ // section1::key2
+ // section1::key3
+ // section2:sub 1:key1
+ // section2:sub 1:key2
+ // section2:sub 1:key3
+}