This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
122 lines (101 loc) · 2.77 KB
/
map.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
package blood_contracts_go
import (
"encoding/json"
"errors"
"reflect"
"regexp"
)
type MapFunction func(interface{}) (interface{}, error)
var regexExpectedTypes = []reflect.Kind{
reflect.String,
}
var RegexMismatchError = errors.New("regex mismatch")
func createRegexMapFunc(regexp *regexp.Regexp) MapFunction {
return func(value interface{}) (interface{}, error) {
toMatch, isString := value.(string)
if !isString {
return nil, NewMismatchTypeError(regexExpectedTypes, reflect.TypeOf(value).Kind())
}
if regexp.MatchString(toMatch) {
return value, nil
} else {
return nil, RegexMismatchError
}
}
}
func createMinMapFunc(container Container, include bool) MapFunction {
return func(value interface{}) (interface{}, error) {
toCompare, err := NewContainer(value)
if err != nil {
return nil, err
}
comparator := Compare(container, toCompare)
if include && comparator == EqualValue || comparator == LessValue {
return value, nil
}
var comparatorType ComparerType
if include {
comparatorType = BiggerOrEqualComparer
} else {
comparatorType = BiggerComparer
}
return nil, NewNumberCompareError(container, comparatorType, comparator, toCompare)
}
}
func createMaxMapFunc(container Container, include bool) MapFunction {
return func(value interface{}) (interface{}, error) {
toCompare, err := NewContainer(value)
if err != nil {
return nil, err
}
comparator := Compare(container, toCompare)
if include && comparator == EqualValue || comparator == BiggerValue {
return value, nil
}
var comparatorType ComparerType
if include {
comparatorType = LessOrEqualComparer
} else {
comparatorType = LessComparer
}
return nil, NewNumberCompareError(container, comparatorType, comparator, toCompare)
}
}
func createEqualMapFunc(container Container) MapFunction {
return func(value interface{}) (interface{}, error) {
toCompare, err := NewContainer(value)
if err != nil {
return nil, err
}
comparator := Compare(container, toCompare)
if comparator == EqualValue {
return value, nil
}
return nil, NewNumberCompareError(container, EqualComparer, comparator, toCompare)
}
}
var jsonExpectedTypes = []reflect.Kind{
reflect.String, reflect.Array,
}
func createJsonUnmarshalMapFunc(jsonType interface{}) MapFunction {
return func(value interface{}) (interface{}, error) {
var data []byte
switch typedValue := value.(type) {
case string:
data = []byte(typedValue)
break
case []byte:
data = typedValue
break
default:
return nil, NewMismatchTypeErrorFromInterface(jsonExpectedTypes, data)
}
clone := reflect.New(reflect.ValueOf(jsonType).Elem().Type()).Interface()
err := json.Unmarshal(data, clone)
// TODO Map to own error
if err != nil {
return nil, err
}
return clone, nil
}
}