From: qydysky Date: Sun, 11 Oct 2020 01:54:13 +0000 (+0800) Subject: 85 X-Git-Tag: v0.0.7 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=b4673eeceffd82236b0eaa9fbf9055e87b4738b0;p=part%2F.git 85 --- diff --git a/setting/Setting.go b/setting/Setting.go new file mode 100644 index 0000000..11a9d8d --- /dev/null +++ b/setting/Setting.go @@ -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