-
Notifications
You must be signed in to change notification settings - Fork 4
/
json_processor.go
75 lines (64 loc) · 1.45 KB
/
json_processor.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 main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
)
var respMap map[string]interface{}
func ParseMockJson(file string) error {
jsonFile, err := os.Open(file)
if err != nil {
return err
}
fmt.Println("✔ Successfully opened:", file)
defer func(jsonFile *os.File) {
err := jsonFile.Close()
if err != nil {
log.Fatal(err)
}
}(jsonFile)
byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &respMap)
if err != nil {
return err
}
fmt.Println("✔ Successfully parsed:", file)
return nil
}
func VerifyMockJson() error {
for k := range respMap {
err := checkFieldPresent(respMap[k], k, "statusCode")
if err != nil {
return err
}
err = checkFieldPresent(respMap[k], k, "responseBody")
if err != nil {
return err
}
// TODO: Potential checks later for new features
//checkFieldPresent(respMap[k], k, "method")
//method := reflect.ValueOf(respMap[k]).MapIndex(reflect.ValueOf("method")).String()
//if method == "POST" {
// checkFieldPresent(respMap[k], k, "requestBody")
//}
}
return nil
}
func checkFieldPresent(i interface{}, key, fName string) error {
dict := reflect.ValueOf(i)
val := dict.MapIndex(reflect.ValueOf(fName))
if val == reflect.ValueOf(nil) {
return errors.New(fmt.Sprintf("Missing `%v` field in %v", fName, key))
}
return nil
}
func printPaths() {
fmt.Println("Available paths: ")
for k := range respMap {
fmt.Println("=>", k)
}
}