endpoint.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "os"
  7. )
  8. type Endpoints struct {
  9. GlobalEndpoints map[string]string `json:"global_endpoints"`
  10. LocationCodeMapping map[string]string `json:"location_code_mapping"`
  11. RegionalEndpointPattern map[string]string `json:"regional_endpoint_pattern"`
  12. Regions []string `json:"regions"`
  13. RegionalEndpoints map[string]map[string]string `json:"regional_endpoints"`
  14. DocumentID map[string]string `json:"document_id"`
  15. }
  16. type RealEndpoints struct {
  17. Products []Product `json:"products"`
  18. }
  19. type Product struct {
  20. Code string `json:"code"`
  21. DocumentID string `json:"document_id"`
  22. LocationServiceCode string `json:"location_service_code"`
  23. RegionalEndpoints []RegionalEndpoint `json:"regional_endpoints"`
  24. GlobalEndpoint string `json:"global_endpoint"`
  25. RegionalEndpointPattern string `json:"regional_endpoint_pattern"`
  26. }
  27. type RegionalEndpoint struct {
  28. Region string `json:"region"`
  29. Endpoint string `json:"endpoint"`
  30. }
  31. //EndpointHandle process with endpoint
  32. func endpointHandle(args []string) error {
  33. if len(os.Args) < 2 {
  34. return nil
  35. }
  36. switch args[1] {
  37. case "parse":
  38. if len(args) != 4 {
  39. return errors.New("The parameter is incorrect")
  40. }
  41. data, err := endpointParse(args[2])
  42. if err != nil {
  43. return err
  44. }
  45. err = generatEndpointsConfigFile(data, args[3])
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. return nil
  51. }
  52. //endpointParse parse endpoint from java json to golang json
  53. func endpointParse(srcpath string) (string, error) {
  54. _, err := os.Stat(srcpath)
  55. if err != nil {
  56. return "", errors.New("Source file error")
  57. }
  58. data, err := ioutil.ReadFile(srcpath)
  59. if err != nil {
  60. return "", err
  61. }
  62. endponit := &Endpoints{}
  63. err = json.Unmarshal(data, endponit)
  64. if err != nil || len(endponit.DocumentID) == 0 {
  65. return "", err
  66. }
  67. realEndpoints := &RealEndpoints{}
  68. for key := range endponit.GlobalEndpoints {
  69. if endponit.DocumentID[key] == "" {
  70. endponit.DocumentID[key] = "sdk"
  71. }
  72. }
  73. for key := range endponit.RegionalEndpointPattern {
  74. if endponit.DocumentID[key] == "" {
  75. endponit.DocumentID[key] = "sdk"
  76. }
  77. }
  78. for key := range endponit.RegionalEndpoints {
  79. if endponit.DocumentID[key] == "" {
  80. endponit.DocumentID[key] = "sdk"
  81. }
  82. }
  83. for key, value := range endponit.DocumentID {
  84. realEndpoint := Product{
  85. Code: key,
  86. LocationServiceCode: key,
  87. DocumentID: value,
  88. GlobalEndpoint: endponit.GlobalEndpoints[key],
  89. RegionalEndpointPattern: endponit.RegionalEndpointPattern[key],
  90. }
  91. if realEndpoint.DocumentID == "sdk" {
  92. realEndpoint.DocumentID = ""
  93. }
  94. for key1, value1 := range endponit.LocationCodeMapping {
  95. if value1 == key {
  96. realEndpoint.Code = key1
  97. }
  98. }
  99. for key2, value2 := range endponit.RegionalEndpoints[key] {
  100. regionalEndpoint := RegionalEndpoint{
  101. Region: key2,
  102. Endpoint: value2,
  103. }
  104. realEndpoint.RegionalEndpoints = append(realEndpoint.RegionalEndpoints, regionalEndpoint)
  105. }
  106. realEndpoints.Products = append(realEndpoints.Products, realEndpoint)
  107. }
  108. byte, err := json.MarshalIndent(realEndpoints, "", "\t")
  109. if err != nil {
  110. return "", err
  111. }
  112. return string(byte), err
  113. }
  114. func generatEndpointsConfigFile(data string, path string) error {
  115. lastData := `
  116. package endpoints
  117. import (
  118. "encoding/json"
  119. "fmt"
  120. "sync"
  121. )
  122. const endpointsJson =` + "`" + data + "`" + `
  123. var initOnce sync.Once
  124. var data interface{}
  125. func getEndpointConfigData() interface{} {
  126. initOnce.Do(func() {
  127. err := json.Unmarshal([]byte(endpointsJson), &data)
  128. if err != nil {
  129. panic(fmt.Sprintf("init endpoint config data failed. %s", err))
  130. }
  131. })
  132. return data
  133. }
  134. `
  135. desfile, err := os.Create(path)
  136. if err != nil {
  137. return err
  138. }
  139. defer desfile.Close()
  140. _, err = desfile.WriteString(lastData)
  141. if err != nil {
  142. return err
  143. }
  144. return nil
  145. }