-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.args.go
104 lines (88 loc) · 1.91 KB
/
source.args.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
package dyanmic_params
import (
"log"
"regexp"
"strings"
)
const SrcNameArgs = "source.args"
type argsParamCollection map[string]interface{}
type SourceArgs struct {
storage argsParamCollection
}
// args must be in this format: --key=value
func NewSourceArgs(args interface{}) *SourceArgs {
var argsTyped, v = args.([]string)
if !v {
log.Println("Args param must be in []string type")
return nil
}
return &SourceArgs{
storage: createMapFromArgs(argsTyped),
}
}
// converts arguments in --name=value format to a map of names and values,
// with names saved without --
func createMapFromArgs(args []string) argsParamCollection {
mc := make(argsParamCollection, 0)
if len(args) > 0 {
rg := regexp.MustCompile(`^\-\-[a-zA-Z\-]+\=.+$`)
for _, v := range args {
if m := rg.Match([]byte(v)); m == true{
spl := strings.SplitN(v, "=", 2)
name := strings.Replace(spl[0], "--", "", 1)
mc[name] = spl[1]
}
}
}
return mc
}
func (s *SourceArgs) Add(name string, value interface{}) ParamsSource {
s.storage[name] = value
return s
}
func (s *SourceArgs) Get(name string) interface{} {
if s.storage == nil {
return ""
} else if val, ok := s.storage[name]; ok {
return val
}
return nil
}
func (s *SourceArgs) Scan(regex string) map[string]interface{} {
if s.Count() > 0 {
mp := make(map[string]interface{}, 0)
rg, err := regexp.Compile(regex)
if err != nil {
return nil
}
for k, v := range s.storage {
if rg.MatchString(k) {
mp[k] = v
}
}
return mp
}
return nil
}
func (s *SourceArgs) Iterate(fn func(k string, v interface{})) {
if s.Count() > 0 {
for k, v := range s.storage {
fn(k, v)
}
}
return
}
func (s *SourceArgs) Has(name string) bool {
if s.storage == nil {
return false
} else if _, ok := s.storage[name]; ok {
return true
}
return false
}
func (s *SourceArgs) Count() int64 {
if s.storage == nil {
return 0
}
return int64(len(s.storage))
}