-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.go
112 lines (96 loc) · 2.65 KB
/
configure.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package unikv
import (
"io/ioutil"
"os"
"gopkg.in/yaml.v3"
)
// Configure is unikv's configure structure
type Configure struct {
Separator string `yaml:"separator" json:"separator"`
Default *ConfigureDefault `yaml:"default" json:"default"`
Namespaces map[string]*ConfigureNamespaces `yaml:"namespaces" json:"namespaces"`
}
var loadedConfigure bool = false
// GetNamespaceList returns the list of namespaces
func (c *Configure) GetNamespaceList() []string {
var rslt []string
for k := range c.Namespaces {
rslt = append(rslt, k)
}
return rslt
}
// GetNamespace returns a specificated namespace from the list
func (c *Configure) GetNamespace(name string) (*ConfigureNamespaces, bool) {
value, ok := c.Namespaces[name]
return value, ok
}
// ConfigureDefault is the default segment
// in unikv's configure
type ConfigureDefault struct {
Driver string `yaml:"driver" json:"driver"`
Context DriverContextRaw `yaml:"context" json:"context"`
}
// ConfigureNamespaces is the namespace segment
// in unikv's configure
type ConfigureNamespaces struct {
Prefix string `yaml:"prefix" json:"prefix"`
Buckets map[string]*ConfigureBuckets `yaml:"buckets" json:"buckets"`
}
// GetBucketList returns bucket list
func (c *ConfigureNamespaces) GetBucketList() []string {
var rslt []string
for k := range c.Buckets {
rslt = append(rslt, k)
}
return rslt
}
// GetBucket returns the bucket
func (c *ConfigureNamespaces) GetBucket(name string) (*ConfigureBuckets, bool) {
b, ok := c.Buckets[name]
return b, ok
}
// ConfigureBuckets is the bucket segment
// in unikv's configure
type ConfigureBuckets struct {
Prefix string `yaml:"prefix" json:"prefix"`
Driver string `yaml:"driver" json:"driver"`
Context DriverContextRaw `yaml:"context" json:"context"`
}
func determineConfigureFilePath() string {
rslt, ok := os.LookupEnv(ConfigureEnvName)
if ok == false {
rslt = DefaultConfigureFile
}
return rslt
}
var configure *Configure
func loadConfigure() {
content, err := ioutil.ReadFile(determineConfigureFilePath())
if err != nil {
panic(err)
}
configure = &Configure{
Separator: PrefixSeparator,
Default: &ConfigureDefault{
Driver: DefaultDriver,
Context: make(DriverContextRaw),
},
Namespaces: make(map[string]*ConfigureNamespaces),
}
err = yaml.Unmarshal(content, configure)
if err != nil {
panic(err)
}
loadedConfigure = true
}
// GetConfigure returns the configure structure
func GetConfigure() *Configure {
if !loadedConfigure {
loadConfigure()
}
return configure
}
// ReloadConfigure reloads the configure
func ReloadConfigure() {
loadConfigure()
}