forked from zpsean/go4api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
144 lines (130 loc) · 4.03 KB
/
validation.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* go4api - an api testing tool written in Go
* Created by: Ping Zhu 2018
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package assertion
import (
"fmt"
"strings"
"reflect"
"encoding/json"
)
// use the pakcage reflect to get the type, and determine if they are comparable, then compare
// ----
// JSON Schema defines the following basic types:
// string
// Numeric -> float64
// boolean
// null
// object (raw)
// array (raw)
func ValidateCallName (name string) bool {
if _, ok := assertionMapping[name]; ok {
return true
} else {
return false
}
}
func ValidateCallParams (name string, params []interface{}) bool {
if len(params) != 2 {
fmt.Println("!! Warning, the number of params is not adapted, false", len(params), params)
return false
}
// (1). nil, if match
if params[0] == nil || params[1] == nil {
if params[0] != nil || params[1] != nil {
// only one nil
return false
} else {
// both nil
return true
}
}
// (2). no nil, if two type assertable
typeAct := reflect.TypeOf(params[0]).Kind().String()
typeExp := reflect.TypeOf(params[1]).Kind().String()
// fmt.Println("typeAct, typeExp: ", typeAct, typeExp)
// consider the type int, float64, they are comparable
if typeAct == "int" && typeExp == "float64" {
return true
} else if typeAct == "int64" && typeExp == "float64" {
return true
} else if typeAct == "float64" && typeExp == "int" {
return true
} else if typeAct == "float64" && typeExp == "int64" {
return true
}
// consider slice
switch typeAct {
case "slice":
switch typeExp {
case "slice":
return true
case "string", "int", "int64", "float64", "bool":
lowerName := strings.ToLower(name)
if lowerName == "has" || lowerName == "nothas" {
return true
} else {
return false
}
default:
return false
}
case "string", "int", "int64", "float64", "bool":
switch typeExp {
case "slice":
lowerName := strings.ToLower(name)
if lowerName == "in" || lowerName == "notin" {
return true
} else {
return false
}
case "string", "int", "int64", "float64", "bool":
if typeAct != typeExp {
return false
}
default:
return false
}
}
// (3). no nil, if type matches with the mapping
ifMatch := false
for _, value := range assertionMapping[name].ApplyTypes {
if strings.ToLower(typeAct) == value {
ifMatch = true
break
}
}
return ifMatch
}
func ifBothNil (params []interface{}) bool {
// (1). if nil
// Note: As get Go nil, for JSON null, need special care, two possibilities:
// p1: expResult -> null, but can not find out actualValue, go set it to nil, i.e. null (assertion -> false)
// p2: expResult -> null, actualValue can be founc, and its value --> null (assertion -> true)
// but here can not distinguish them
if params[0] == nil || params[1] == nil {
if params[0] != nil || params[1] != nil {
// only one nil
return false
} else {
// both nil
return true
}
} else {
return false
}
}
func GetRawJsonResult (value interface{}) string {
// to get the raw json string using json.Marshal
byteValue, err := json.Marshal(value)
if err != nil {
return ""
}
return string(byteValue)
}