]> 127.0.0.1 Git - part/.git/commitdiff
85 v0.0.7
authorqydysky <qydysky@foxmail.com>
Sun, 11 Oct 2020 01:54:13 +0000 (09:54 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 11 Oct 2020 01:54:13 +0000 (09:54 +0800)
setting/Setting.go [new file with mode: 0644]

diff --git a/setting/Setting.go b/setting/Setting.go
new file mode 100644 (file)
index 0000000..11a9d8d
--- /dev/null
@@ -0,0 +1,46 @@
+package part
+
+import (
+       "os"
+       "github.com/thedevsaddam/gojsonq/v2"
+)
+
+type Setting struct {
+       B map[string]interface{}
+}
+
+func New() *Setting {
+       b := new(Setting)
+       b.B = make(map[string]interface{})
+       return b
+} 
+
+func (i *Setting) Get(key string) (interface{},bool) {
+       v,ok := i.B[key]
+       return v,ok
+}
+
+func (i *Setting) Set(key string,val interface{}) bool {
+       switch val.(type) {
+       case string,bool,int,float64:
+               i.B[key] = val
+               return true
+       default:
+       }
+       return false
+}
+
+func (i *Setting) Save(Source string) error {
+       js := gojsonq.New().FromInterface(i.B)
+       fileObj,err := os.OpenFile(Source,os.O_RDWR|os.O_EXCL,0644)
+       if err != nil {return err}
+       defer fileObj.Close()
+       js.Writer(fileObj)
+       return nil
+}
+
+func (i *Setting) Load(Source string) {
+       if b := gojsonq.New().File(Source).Get();b != nil{
+               i.B = b.(map[string]interface {})
+       }
+}
\ No newline at end of file