forked from sbstjn/allot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter.go
80 lines (64 loc) · 1.63 KB
/
parameter.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
package allot
import (
"regexp"
"strings"
)
var regexpMapping = map[string]string{
"string": "([^\\s]+)",
"string?": "([^\\s]+)?",
"integer": "([0-9]+)",
}
// Expression returns the regexp for a data type
func Expression(data string) *regexp.Regexp {
if exp, ok := regexpMapping[data]; ok {
return regexp.MustCompile(exp)
}
return nil
}
// ParameterInterface describes how to access a Parameter
type ParameterInterface interface {
Equals(param ParameterInterface) bool
Expression() *regexp.Regexp
Name() string
Data() string
}
// Parameter is the Parameter definition
type Parameter struct {
name string
data string
expr *regexp.Regexp
}
// Expression returns the regexp behind the type
func (p Parameter) Expression() *regexp.Regexp {
return p.expr
}
// Name returns the Parameter name
func (p Parameter) Name() string {
return p.name
}
// Data returns the Parameter name
func (p Parameter) Data() string {
return p.data
}
// Equals checks if two parameter are equal
func (p Parameter) Equals(param ParameterInterface) bool {
return p.Name() == param.Name() && p.Expression().String() == param.Expression().String()
}
// NewParameterWithType returns
func NewParameterWithType(name string, data string) Parameter {
return Parameter{name, data, Expression(data)}
}
// Parse parses parameter info
func Parse(text string) Parameter {
var splits []string
var name, data string
name = strings.Replace(text, "<", "", -1)
name = strings.Replace(name, ">", "", -1)
data = "string"
if strings.Contains(name, ":") {
splits = strings.Split(name, ":")
name = splits[0]
data = splits[1]
}
return NewParameterWithType(name, data)
}