-
Notifications
You must be signed in to change notification settings - Fork 274
/
endpoint.go
162 lines (145 loc) · 3.94 KB
/
endpoint.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
)
type Endpoints struct {
GlobalEndpoints map[string]string `json:"global_endpoints"`
LocationCodeMapping map[string]string `json:"location_code_mapping"`
RegionalEndpointPattern map[string]string `json:"regional_endpoint_pattern"`
Regions []string `json:"regions"`
RegionalEndpoints map[string]map[string]string `json:"regional_endpoints"`
DocumentID map[string]string `json:"document_id"`
}
type RealEndpoints struct {
Products []Product `json:"products"`
}
type Product struct {
Code string `json:"code"`
DocumentID string `json:"document_id"`
LocationServiceCode string `json:"location_service_code"`
RegionalEndpoints []RegionalEndpoint `json:"regional_endpoints"`
GlobalEndpoint string `json:"global_endpoint"`
RegionalEndpointPattern string `json:"regional_endpoint_pattern"`
}
type RegionalEndpoint struct {
Region string `json:"region"`
Endpoint string `json:"endpoint"`
}
//EndpointHandle process with endpoint
func endpointHandle(args []string) error {
if len(os.Args) < 2 {
return nil
}
switch args[1] {
case "parse":
if len(args) != 4 {
return errors.New("The parameter is incorrect")
}
data, err := endpointParse(args[2])
if err != nil {
return err
}
err = generatEndpointsConfigFile(data, args[3])
if err != nil {
return err
}
}
return nil
}
//endpointParse parse endpoint from java json to golang json
func endpointParse(srcpath string) (string, error) {
_, err := os.Stat(srcpath)
if err != nil {
return "", errors.New("Source file error")
}
data, err := ioutil.ReadFile(srcpath)
if err != nil {
return "", err
}
endponit := &Endpoints{}
err = json.Unmarshal(data, endponit)
if err != nil || len(endponit.DocumentID) == 0 {
return "", err
}
realEndpoints := &RealEndpoints{}
for key := range endponit.GlobalEndpoints {
if endponit.DocumentID[key] == "" {
endponit.DocumentID[key] = "sdk"
}
}
for key := range endponit.RegionalEndpointPattern {
if endponit.DocumentID[key] == "" {
endponit.DocumentID[key] = "sdk"
}
}
for key := range endponit.RegionalEndpoints {
if endponit.DocumentID[key] == "" {
endponit.DocumentID[key] = "sdk"
}
}
for key, value := range endponit.DocumentID {
realEndpoint := Product{
Code: key,
LocationServiceCode: key,
DocumentID: value,
GlobalEndpoint: endponit.GlobalEndpoints[key],
RegionalEndpointPattern: endponit.RegionalEndpointPattern[key],
}
if realEndpoint.DocumentID == "sdk" {
realEndpoint.DocumentID = ""
}
for key1, value1 := range endponit.LocationCodeMapping {
if value1 == key {
realEndpoint.Code = key1
}
}
for key2, value2 := range endponit.RegionalEndpoints[key] {
regionalEndpoint := RegionalEndpoint{
Region: key2,
Endpoint: value2,
}
realEndpoint.RegionalEndpoints = append(realEndpoint.RegionalEndpoints, regionalEndpoint)
}
realEndpoints.Products = append(realEndpoints.Products, realEndpoint)
}
byte, err := json.MarshalIndent(realEndpoints, "", "\t")
if err != nil {
return "", err
}
return string(byte), err
}
func generatEndpointsConfigFile(data string, path string) error {
lastData := `
package endpoints
import (
"encoding/json"
"fmt"
"sync"
)
const endpointsJson =` + "`" + data + "`" + `
var initOnce sync.Once
var data interface{}
func getEndpointConfigData() interface{} {
initOnce.Do(func() {
err := json.Unmarshal([]byte(endpointsJson), &data)
if err != nil {
panic(fmt.Sprintf("init endpoint config data failed. %s", err))
}
})
return data
}
`
desfile, err := os.Create(path)
if err != nil {
return err
}
defer desfile.Close()
_, err = desfile.WriteString(lastData)
if err != nil {
return err
}
return nil
}