-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.go
32 lines (28 loc) · 907 Bytes
/
Utils.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
package cff
import (
"errors"
"regexp"
)
// CommonMarshalYAML marshal a string into a string and check if it matches the regex pattern
func CommonMarshalYAML(cffString string, regexPattern *regexp.Regexp, typeOfString string) (interface{}, error) {
var err error
if regexPattern.MatchString(cffString) {
return cffString, err
} else {
return "", errors.New("invalid " + typeOfString + ": " + cffString)
}
}
// CommonUnmarshalYAML unmarshal a string into a string and check if it matches the regex pattern
func CommonUnmarshalYAML(matchAction func(string), regexPattern *regexp.Regexp, unmarshal func(interface{}) error, typeOfString string) error {
var cffString string
err := unmarshal(&cffString)
if err != nil {
return err
}
if regexPattern.MatchString(cffString) {
matchAction(cffString)
} else {
err = errors.New("invalid " + typeOfString + ": " + cffString)
}
return err
}