endpoint.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, value := range endponit.DocumentID {
  69. realEndpoint := Product{
  70. Code: key,
  71. LocationServiceCode: key,
  72. DocumentID: value,
  73. GlobalEndpoint: endponit.GlobalEndpoints[key],
  74. RegionalEndpointPattern: endponit.RegionalEndpointPattern[key],
  75. }
  76. for key1, value1 := range endponit.LocationCodeMapping {
  77. if value1 == key {
  78. realEndpoint.Code = key1
  79. }
  80. }
  81. for key2, value2 := range endponit.RegionalEndpoints[key] {
  82. regionalEndpoint := RegionalEndpoint{
  83. Region: key2,
  84. Endpoint: value2,
  85. }
  86. realEndpoint.RegionalEndpoints = append(realEndpoint.RegionalEndpoints, regionalEndpoint)
  87. }
  88. realEndpoints.Products = append(realEndpoints.Products, realEndpoint)
  89. }
  90. byte, err := json.MarshalIndent(realEndpoints, "", "\t")
  91. if err != nil {
  92. return "", err
  93. }
  94. return string(byte), err
  95. }
  96. func generatEndpointsConfigFile(data string, path string) error {
  97. lastData := `
  98. package endpoints
  99. import (
  100. "encoding/json"
  101. "fmt"
  102. "sync"
  103. )
  104. const endpointsJson =` + "`" + data + "`" + `
  105. var initOnce sync.Once
  106. var data interface{}
  107. func getEndpointConfigData() interface{} {
  108. initOnce.Do(func() {
  109. err := json.Unmarshal([]byte(endpointsJson), &data)
  110. if err != nil {
  111. panic(fmt.Sprintf("init endpoint config data failed. %s", err))
  112. }
  113. })
  114. return data
  115. }
  116. `
  117. desfile, err := os.Create(path)
  118. if err != nil {
  119. return err
  120. }
  121. defer desfile.Close()
  122. _, err = desfile.WriteString(lastData)
  123. if err != nil {
  124. return err
  125. }
  126. return nil
  127. }