forked from kavanahuang/log
/
toml.go
99 lines (82 loc) · 2.49 KB
/
toml.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
/*
Author: Kernel.Huang
Mail: kernelman79@gmail.com
File: config.go
Date: 8/9/22 1:57 PM
*/
package log
import (
goToml "github.com/pelletier/go-toml"
"log"
)
type TomlConfig struct {
keyName string
value interface{}
Structured interface{}
cfg *goToml.Tree
}
var Toml = new(TomlConfig)
func (tf *TomlConfig) NewToml(dirname string, filename string) *TomlConfig {
name := GetCustomConfigPath(dirname, filename)
conf, err := goToml.LoadFile(name)
if err != nil {
log.Println("Load toml file error: ", err)
}
tf.cfg = conf
return tf
}
// Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Get("key").To()
func (tf *TomlConfig) Zone(key string) *TomlConfig {
tf.keyName = key
return tf
}
// Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Get("key").To()
func (tf *TomlConfig) Get(key string) *TomlConfig {
tf.keyName = tf.keyName + "." + key
return tf
}
/**
* Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Get("key").To()
*/
func (tf *TomlConfig) To() interface{} {
return tf.cfg.Get(tf.keyName)
}
// Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Get("key").AtStr()
func (tf *TomlConfig) AtStr() string {
tf.value = tf.cfg.Get(tf.keyName)
return tf.value.(string)
}
// Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Get("key").AtInt()
func (tf *TomlConfig) AtInt() int {
tf.value = tf.cfg.Get(tf.keyName)
return tf.value.(int)
}
// Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Get("key").AtBool()
func (tf *TomlConfig) AtBool() bool {
tf.value = tf.cfg.Get(tf.keyName)
return tf.value.(bool)
}
// Example: result := Tome.NewToml(dirname, filename).Zone("zoneName").Fetch("key").ToStr()
func (tf *TomlConfig) Fetch(key string) *TomlConfig {
tf.keyName = tf.keyName + "." + key
tf.value = tf.cfg.Get(tf.keyName)
return tf
}
// Example: result := Tome.NewToml(dirname, filename).Read("zoneName.key").ToStr() or ToInt()
func (tf *TomlConfig) Read(key string) *TomlConfig {
tf.keyName = key
tf.value = tf.To()
return tf
}
// Example: result := Tome.NewToml(dirname, filename).Read("zoneName.key").ToStr()
func (tf *TomlConfig) ToStr() string {
return tf.value.(string)
}
// Example: result := Tome.NewToml(dirname, filename).Read("zoneName.key").ToInt()
func (tf *TomlConfig) ToInt() int {
return tf.value.(int)
}
// Example: result := Tome.NewToml(dirname, filename).Read("zoneName.key").ToBool()
func (tf *TomlConfig) ToBool() bool {
return tf.value.(bool)
}