/
appdata.go
63 lines (50 loc) · 1.25 KB
/
appdata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package appdata
import (
"bytes"
"go-sepak-bola/internal/dboxapi"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type AppData struct {
data []byte
Api dboxapi.DBoxAPI
}
type PostData struct {
Category string `json:"category"`
Action string `json:"action,omitempty"`
Params interface{} `json:"params,omitempty"`
}
var appData AppData
func Instance() *AppData {
return &appData
}
func (appdata *AppData) Data() []byte {
return appdata.data
}
func (appdata *AppData) Get(path string) gjson.Result {
return gjson.Get(string(appdata.data), path)
}
func (appdata *AppData) Set(path string, value interface{}) {
appdata.data, _ = sjson.SetBytes(appdata.data, path, value)
}
func (appdata *AppData) Delete(path string) {
appdata.data, _ = sjson.DeleteBytes(appdata.data, path)
}
func (appdata *AppData) String() string {
return string(appdata.data)
}
func (appdata *AppData) Pull(path string) []byte {
data := appdata.Api.Download(path)
/*
* FIXME: check if data is validate json.
*/
if len(gjson.Get(string(data), "error.\\.tag").Str) == 0 {
appdata.data = data
}
return appdata.data
}
func (appdata *AppData) Push(path string) {
content := bytes.NewReader(appdata.data)
appdata.Api.UploadContent(path, content)
return
}