forked from tooolbox/cybersource-rest-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-spec.go
134 lines (111 loc) · 2.88 KB
/
fix-spec.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/tooolbox/jwalk"
)
func main() {
log.Print("Walking...")
f, err := os.Open("./generator/cybersource-rest-spec.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
i, err := jwalk.Parse(b)
if err != nil {
log.Fatal(err)
}
checkStringValue := func(i interface{}, expect string) bool {
if val, ok := i.(jwalk.Value); ok {
return val.String() == expect
}
return false
}
fixInvalid := func(name string, obj jwalk.ObjectWalker) (string, interface{}, error) {
hasStringType, hasMaximum := false, false
hasBoolType, hasMaxLength := false, false
err := obj.Walk(func(key string, value interface{}) (string, interface{}, error) {
if key == "type" && checkStringValue(value, "string") {
hasStringType = true
}
if key == "maximum" {
hasMaximum = true
}
if key == "type" && checkStringValue(value, "boolean") {
hasBoolType = true
}
if key == "maxLength" {
hasMaxLength = true
}
return key, value, nil
})
if err != nil {
return name, obj, err
}
if hasStringType && hasMaximum {
log.Print("Found a string type with a maximum field")
err = obj.Walk(func(key string, value interface{}) (string, interface{}, error) {
if key == "maximum" {
return "maxLength", value, nil
}
return key, value, nil
})
}
if hasBoolType && hasMaxLength {
log.Print("Found a boolean type with a maxLength field")
err = obj.Walk(func(key string, value interface{}) (string, interface{}, error) {
if key == "maxLength" {
return "", nil, jwalk.RemoveField
}
return key, value, nil
})
}
return name, obj, err
}
var walk func(string, interface{}) (string, interface{}, error)
walk = func(k string, i interface{}) (string, interface{}, error) {
switch v := i.(type) {
case jwalk.ObjectWalker:
kRepl, vRepl, err := fixInvalid(k, v)
if err != nil {
return k, i, err
}
return kRepl, vRepl, vRepl.(jwalk.ObjectWalker).Walk(func(key string, value interface{}) (string, interface{}, error) {
return walk(key, value)
})
// return k, v, v.Walk(func(key string, value interface{}) (string, interface{}, error) {
// return walk(key, value)
// })
case jwalk.ObjectsWalker:
return k, v, v.Walk(func(obj jwalk.ObjectWalker) error {
return obj.Walk(func(key string, value interface{}) (string, interface{}, error) {
return walk(key, value)
})
})
case jwalk.Value:
return k, v, nil
default:
return k, v, nil
}
}
_, result, err := walk("", i)
if err != nil {
log.Fatal(err)
}
n, err := os.Create("./generator/cybersource-rest-spec-fixed.json")
if err != nil {
log.Fatal(err)
}
enc := json.NewEncoder(n)
enc.SetIndent("", " ")
if err := enc.Encode(result); err != nil {
log.Fatal(err)
}
log.Print("Done.")
}