-
Notifications
You must be signed in to change notification settings - Fork 0
/
zv.go
124 lines (105 loc) · 2.27 KB
/
zv.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
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"strconv"
"strings"
)
type Properties struct {
innerProps map[string]string
}
func NewProperties() *Properties {
p := Properties{
innerProps: make(map[string]string, 50),
}
return &p
}
func (g *Properties) SetProperties(p *Properties) {
if p == nil {
return
}
for k, v := range p.innerProps {
g.Set(strings.ToLower(k), v)
}
}
func (g *Properties) Len() int {
return len(g.innerProps)
}
func (g *Properties) IsNil() bool {
return g == nil || g.innerProps == nil
}
func (g *Properties) GetString(key, def string) string {
v, ok := g.innerProps[strings.ToLower(key)]
if !ok || v == "" {
return def
}
return v
}
func (g *Properties) GetInt(key string, def int, min int, max int) int {
value, ok := g.innerProps[strings.ToLower(key)]
if !ok || value == "" {
return def
}
i, err := strconv.Atoi(value)
if err != nil {
return def
}
if i > max || i < min {
return def
}
return i
}
func (g *Properties) GetBool(key string, def bool) bool {
value, ok := g.innerProps[strings.ToLower(key)]
if !ok || value == "" {
return def
}
b, err := strconv.ParseBool(value)
if err != nil {
return def
}
return b
}
func (g *Properties) GetTrimString(key string, def string) string {
value, ok := g.innerProps[strings.ToLower(key)]
if !ok || value == "" {
return def
} else {
return strings.TrimSpace(value)
}
}
func (g *Properties) GetStringArray(key string, def []string) []string {
value, ok := g.innerProps[strings.ToLower(key)]
if ok || value != "" {
array := strings.Split(value, ",")
if len(array) > 0 {
return array
}
}
return def
}
//func (g *Properties) GetBool(key string) bool {
// i, _ := strconv.ParseBool(g.innerProps[key])
// return i
//}
func (g *Properties) Set(key, value string) {
g.innerProps[strings.ToLower(key)] = value
}
func (g *Properties) SetIfNotExist(key, value string) {
if _, ok := g.innerProps[strings.ToLower(key)]; !ok {
g.Set(key, value)
}
}
// 如果p有g没有的键值对,添加进g中
func (g *Properties) SetDiffProperties(p *Properties) {
if p == nil {
return
}
for k, v := range p.innerProps {
if _, ok := g.innerProps[strings.ToLower(k)]; !ok {
g.innerProps[strings.ToLower(k)] = v
}
}
}