forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urns.go
71 lines (56 loc) · 1.45 KB
/
urns.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
package flows
import (
"fmt"
"strings"
validator "gopkg.in/go-playground/validator.v9"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/utils"
)
func init() {
utils.Validator.RegisterValidation("urn", ValidateURN)
utils.Validator.RegisterValidation("urnscheme", ValidateURNScheme)
}
// ValidateURN validates whether the field value is a valid URN
func ValidateURN(fl validator.FieldLevel) bool {
return urns.URN(fl.Field().String()).Validate()
}
// ValidateURNScheme validates whether the field value is a valid URN scheme
func ValidateURNScheme(fl validator.FieldLevel) bool {
return urns.IsValidScheme(fl.Field().String())
}
// URNList is the list of a contact's URNs
type URNList []urns.URN
func (l URNList) Clone() URNList {
urns := make(URNList, len(l))
copy(urns, l)
return urns
}
func (l URNList) Resolve(key string) interface{} {
scheme := strings.ToLower(key)
// if this isn't a valid scheme, bail
if !urns.IsValidScheme(scheme) {
return fmt.Errorf("unknown URN scheme: %s", key)
}
// This is a specific scheme, look up all matches
var found URNList
for _, u := range l {
if u.Scheme() == scheme {
found = append(found, u)
}
}
return found
}
func (l URNList) Default() interface{} {
if len(l) > 0 {
return l[0]
}
return nil
}
func (l URNList) String() string {
if len(l) > 0 {
return l[0].String()
}
return ""
}
var _ utils.VariableResolver = (urns.URN)("")
var _ utils.VariableResolver = (URNList)(nil)