main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path"
  7. "strings"
  8. cfile "github.com/cherry-game/cherry/extend/file"
  9. jsoniter "github.com/json-iterator/go"
  10. )
  11. const (
  12. // ProtoDir = "../../../protocol/"
  13. ProtoDir = "../../_proto/"
  14. FileSuffix = "json"
  15. SaveDir = "../../etc/data/"
  16. SaveFileName = "proto_route.json"
  17. )
  18. type (
  19. RouteItem struct {
  20. Enable bool `json:"enable"`
  21. Name string `json:"name"`
  22. Route string `json:"route"`
  23. Request string `json:"request"`
  24. Response string `json:"response"`
  25. Push string `json:"push"`
  26. Notify string `json:"notify"`
  27. Desc string `json:"desc"`
  28. IsRequest bool `json:"-"`
  29. }
  30. )
  31. func (p *RouteItem) String() string {
  32. s, err := jsoniter.MarshalToString(p)
  33. if err != nil {
  34. return fmt.Sprintf("err: %v", err)
  35. }
  36. return s
  37. }
  38. func main() {
  39. protoJudeDir, found := cfile.JudgePath(ProtoDir)
  40. if !found {
  41. log.Panicf("Proto dir not found. dir = %s", ProtoDir)
  42. }
  43. filePaths := cfile.WalkFiles(protoJudeDir, FileSuffix)
  44. if len(filePaths) < 1 {
  45. log.Panic("Protocol config json not found.")
  46. }
  47. var allList []*RouteItem
  48. for _, filePath := range filePaths {
  49. if strings.HasSuffix(filePath, "client.json") {
  50. log.Printf("Skip file = %s", filePath)
  51. continue
  52. }
  53. log.Printf("Read file = %s", filePath)
  54. items, err := readRouteItems(filePath)
  55. if err != nil {
  56. panic(err)
  57. }
  58. checkItems(items)
  59. allList = append(allList, items...)
  60. }
  61. writeFile(allList)
  62. }
  63. func readRouteItems(path string) ([]*RouteItem, error) {
  64. bytes, err := os.ReadFile(path)
  65. if err != nil {
  66. return nil, err
  67. }
  68. var list []*RouteItem
  69. err = jsoniter.Unmarshal(bytes, &list)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return list, nil
  74. }
  75. func checkItems(allList []*RouteItem) {
  76. for _, item := range allList {
  77. item.Route = strings.TrimSpace(item.Route)
  78. item.Request = strings.TrimSpace(item.Request)
  79. item.Response = strings.TrimSpace(item.Response)
  80. item.Push = strings.TrimSpace(item.Push)
  81. item.Notify = strings.TrimSpace(item.Notify)
  82. if item.Route == "" {
  83. log.Panicf("Route item error. item = %v", item)
  84. }
  85. if item.Request == "" &&
  86. item.Response == "" &&
  87. item.Push == "" &&
  88. item.Notify == "" {
  89. log.Panicf("Route item type error. item = %v", item)
  90. }
  91. if item.Request != "" {
  92. item.IsRequest = true
  93. }
  94. }
  95. }
  96. func writeFile(allList []*RouteItem) {
  97. bytes, err := jsoniter.MarshalIndent(allList, "", " ")
  98. if err != nil {
  99. log.Panic(err)
  100. }
  101. saveDir, found := cfile.JudgePath(SaveDir)
  102. if !found {
  103. log.Panicf("Save file path not found. dir = %s", saveDir)
  104. }
  105. saveFileName := path.Join(saveDir, SaveFileName)
  106. err = os.WriteFile(saveFileName, bytes, 0644)
  107. if err != nil {
  108. log.Panic(err)
  109. }
  110. log.Printf("Write file = %s", saveFileName)
  111. }