| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/go-rod/rod/lib/launcher" |
| 12 |
"github.com/go-rod/rod/lib/utils" |
| 13 |
"github.com/ysmood/gson" |
| 14 |
) |
| 15 |
|
| 16 |
func getSchema() gson.JSON { |
| 17 |
l := launcher.New().Bin(launcher.NewBrowser().MustGet()) |
| 18 |
defer l.Kill() |
| 19 |
|
| 20 |
u := l.MustLaunch() |
| 21 |
parsed, err := url.Parse(u) |
| 22 |
utils.E(err) |
| 23 |
parsed.Scheme = "http" |
| 24 |
parsed.Path = "/json/protocol" |
| 25 |
|
| 26 |
res, err := http.Get(parsed.String()) //nolint: noctx |
| 27 |
utils.E(err) |
| 28 |
defer func() { _ = res.Body.Close() }() |
| 29 |
|
| 30 |
data, err := io.ReadAll(res.Body) |
| 31 |
utils.E(err) |
| 32 |
|
| 33 |
obj := gson.New(data) |
| 34 |
|
| 35 |
utils.E(utils.OutputFile("tmp/proto.json", obj.JSON("", " "))) |
| 36 |
|
| 37 |
return obj |
| 38 |
} |
| 39 |
|
| 40 |
func mapType(n string) string { |
| 41 |
return map[string]string{ |
| 42 |
"boolean": "bool", |
| 43 |
"number": "float64", |
| 44 |
"integer": "int", |
| 45 |
"string": "string", |
| 46 |
"binary": "[]byte", |
| 47 |
"object": "map[string]gson.JSON", |
| 48 |
"any": "gson.JSON", |
| 49 |
}[n] |
| 50 |
} |
| 51 |
|
| 52 |
func typeName(domain *domain, schema gson.JSON) string { |
| 53 |
typeName := "" |
| 54 |
if schema.Has("type") { |
| 55 |
typeName = schema.Get("type").Str() |
| 56 |
} |
| 57 |
|
| 58 |
if typeName == "array" { //nolint: nestif |
| 59 |
item := schema.Get("items") |
| 60 |
|
| 61 |
if item.Has("type") { |
| 62 |
typeName = "[]" + mapType(item.Get("type").Str()) |
| 63 |
} else { |
| 64 |
ref := item.Get("$ref").Str() |
| 65 |
if domain.ref(ref) { |
| 66 |
typeName = "[]*" + refName(domain.name, ref) |
| 67 |
} else { |
| 68 |
typeName = "[]" + refName(domain.name, ref) |
| 69 |
} |
| 70 |
} |
| 71 |
} else if schema.Has("$ref") { |
| 72 |
ref := schema.Get("$ref").Str() |
| 73 |
if domain.ref(ref) { |
| 74 |
typeName += "*" |
| 75 |
} |
| 76 |
typeName += refName(domain.name, ref) |
| 77 |
} else { |
| 78 |
typeName = mapType(typeName) |
| 79 |
} |
| 80 |
|
| 81 |
switch typeName { |
| 82 |
case "NetworkTimeSinceEpoch", "InputTimeSinceEpoch": |
| 83 |
typeName = "TimeSinceEpoch" |
| 84 |
case "NetworkMonotonicTime": |
| 85 |
typeName = "MonotonicTime" |
| 86 |
} |
| 87 |
|
| 88 |
return typeName |
| 89 |
} |
| 90 |
|
| 91 |
func enumList(schema gson.JSON) []string { |
| 92 |
var enum []string |
| 93 |
if schema.Has("enum") { |
| 94 |
enum = []string{} |
| 95 |
for _, v := range schema.Get("enum").Arr() { |
| 96 |
if _, ok := v.Val().(string); !ok { |
| 97 |
panic("enum type error") |
| 98 |
} |
| 99 |
enum = append(enum, v.Str()) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return enum |
| 104 |
} |
| 105 |
|
| 106 |
func jsonTag(name string, optional bool) string { |
| 107 |
jsonTagValue := name |
| 108 |
if optional { |
| 109 |
jsonTagValue += ",omitempty" |
| 110 |
} |
| 111 |
return fmt.Sprintf("`json:\"%s\"`", jsonTagValue) |
| 112 |
} |
| 113 |
|
| 114 |
func refName(domain, id string) string { |
| 115 |
if strings.Contains(id, ".") { |
| 116 |
return symbol(id) |
| 117 |
} |
| 118 |
return domain + symbol(id) |
| 119 |
} |
| 120 |
|
| 121 |
// make sure golint works fine. |
| 122 |
func symbol(n string) string { |
| 123 |
if n == "" { |
| 124 |
return "" |
| 125 |
} |
| 126 |
|
| 127 |
n = strings.ReplaceAll(n, ".", "") |
| 128 |
|
| 129 |
dashed := regexp.MustCompile(`[-_]`).Split(n, -1) |
| 130 |
if len(dashed) > 1 { |
| 131 |
converted := []string{} |
| 132 |
for _, part := range dashed { |
| 133 |
converted = append(converted, strings.ToUpper(part[:1])+part[1:]) |
| 134 |
} |
| 135 |
n = strings.Join(converted, "") |
| 136 |
} |
| 137 |
|
| 138 |
n = strings.ToUpper(n[:1]) + n[1:] |
| 139 |
|
| 140 |
n = replaceLower(n, "Id") |
| 141 |
n = replaceLower(n, "Css") |
| 142 |
n = replaceLower(n, "Url") |
| 143 |
n = replaceLower(n, "Uuid") |
| 144 |
n = replaceLower(n, "Xml") |
| 145 |
n = replaceLower(n, "Http") |
| 146 |
n = replaceLower(n, "Dns") |
| 147 |
n = replaceLower(n, "Cpu") |
| 148 |
n = replaceLower(n, "Mime") |
| 149 |
n = replaceLower(n, "Json") |
| 150 |
n = replaceLower(n, "Html") |
| 151 |
n = replaceLower(n, "Guid") |
| 152 |
n = replaceLower(n, "Sql") |
| 153 |
n = replaceLower(n, "Eof") |
| 154 |
n = replaceLower(n, "Api") |
| 155 |
n = replaceLower(n, "Ui") |
| 156 |
n = replaceLower(n, "Https") |
| 157 |
|
| 158 |
n = strings.Replace(n, "Ids", "IDs", -1) |
| 159 |
|
| 160 |
return n |
| 161 |
} |
| 162 |
|
| 163 |
func replaceLower(n, word string) string { |
| 164 |
return regexp.MustCompile(word+`([A-Z-_]|$)`).ReplaceAllStringFunc(n, strings.ToUpper) |
| 165 |
} |
| 166 |
|
| 167 |
var ( |
| 168 |
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") |
| 169 |
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") |
| 170 |
) |
| 171 |
|
| 172 |
func toSnakeCase(str string) string { |
| 173 |
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") |
| 174 |
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") |
| 175 |
return strings.ToLower(snake) |
| 176 |
} |