-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcontract.go
75 lines (68 loc) · 1.85 KB
/
contract.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
package validator
import (
"github.com/viant/assertly"
"github.com/viant/toolbox"
)
// AssertRequest represent assert request
type AssertRequest struct {
TagID string
Name string
Description string
Actual interface{} `required:"true" description:"actual value/data structure"`
Expect interface{} `required:"true" description:"expected value/data structure"`
Source interface{} //optional validation source
Ignore interface{}
OmitEmpty bool
NormalizeKVPairs bool //flag to normalize kv pairs into map if possible (i.e, when using yaml)
}
func (r *AssertRequest) IgnoreKeys() []interface{} {
var keys = make([]interface{}, 0)
switch actual := r.Ignore.(type) {
case []interface{}:
for i := range actual {
keys = append(keys, actual[i])
}
case map[string]interface{}:
for k := range actual {
keys = append(keys, k)
}
case map[interface{}]interface{}:
for k := range actual {
keys = append(keys, k)
}
}
return keys
}
// AssertResponse represent validation response
type AssertResponse struct {
*assertly.Validation
}
func (r *AssertRequest) Init() error {
if r.Expect == nil {
return nil
}
if r.NormalizeKVPairs {
if normalized, err := toolbox.NormalizeKVPairs(r.Expect); err == nil {
r.Expect = normalized
}
}
return nil
}
// Assertion returns validation slice
func (r *AssertResponse) Assertion() []*assertly.Validation {
if r == nil {
return []*assertly.Validation{}
}
return []*assertly.Validation{r.Validation}
}
// NewAssertRequest creates a new assertRequest
func NewAssertRequest(tagID string, name string, description string, source, expected, actual interface{}) *AssertRequest {
return &AssertRequest{
Source: source,
TagID: tagID,
Name: name,
Description: description,
Expect: expected,
Actual: actual,
}
}