| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package main
- import (
- "fmt"
- "log"
- "os"
- "path"
- "strings"
- cfile "github.com/cherry-game/cherry/extend/file"
- jsoniter "github.com/json-iterator/go"
- )
- const (
- // ProtoDir = "../../../protocol/"
- ProtoDir = "../../_proto/"
- FileSuffix = "json"
- SaveDir = "../../etc/data/"
- SaveFileName = "proto_route.json"
- )
- type (
- RouteItem struct {
- Enable bool `json:"enable"`
- Name string `json:"name"`
- Route string `json:"route"`
- Request string `json:"request"`
- Response string `json:"response"`
- Push string `json:"push"`
- Notify string `json:"notify"`
- Desc string `json:"desc"`
- IsRequest bool `json:"-"`
- }
- )
- func (p *RouteItem) String() string {
- s, err := jsoniter.MarshalToString(p)
- if err != nil {
- return fmt.Sprintf("err: %v", err)
- }
- return s
- }
- func main() {
- protoJudeDir, found := cfile.JudgePath(ProtoDir)
- if !found {
- log.Panicf("Proto dir not found. dir = %s", ProtoDir)
- }
- filePaths := cfile.WalkFiles(protoJudeDir, FileSuffix)
- if len(filePaths) < 1 {
- log.Panic("Protocol config json not found.")
- }
- var allList []*RouteItem
- for _, filePath := range filePaths {
- if strings.HasSuffix(filePath, "client.json") {
- log.Printf("Skip file = %s", filePath)
- continue
- }
- log.Printf("Read file = %s", filePath)
- items, err := readRouteItems(filePath)
- if err != nil {
- panic(err)
- }
- checkItems(items)
- allList = append(allList, items...)
- }
- writeFile(allList)
- }
- func readRouteItems(path string) ([]*RouteItem, error) {
- bytes, err := os.ReadFile(path)
- if err != nil {
- return nil, err
- }
- var list []*RouteItem
- err = jsoniter.Unmarshal(bytes, &list)
- if err != nil {
- return nil, err
- }
- return list, nil
- }
- func checkItems(allList []*RouteItem) {
- for _, item := range allList {
- item.Route = strings.TrimSpace(item.Route)
- item.Request = strings.TrimSpace(item.Request)
- item.Response = strings.TrimSpace(item.Response)
- item.Push = strings.TrimSpace(item.Push)
- item.Notify = strings.TrimSpace(item.Notify)
- if item.Route == "" {
- log.Panicf("Route item error. item = %v", item)
- }
- if item.Request == "" &&
- item.Response == "" &&
- item.Push == "" &&
- item.Notify == "" {
- log.Panicf("Route item type error. item = %v", item)
- }
- if item.Request != "" {
- item.IsRequest = true
- }
- }
- }
- func writeFile(allList []*RouteItem) {
- bytes, err := jsoniter.MarshalIndent(allList, "", " ")
- if err != nil {
- log.Panic(err)
- }
- saveDir, found := cfile.JudgePath(SaveDir)
- if !found {
- log.Panicf("Save file path not found. dir = %s", saveDir)
- }
- saveFileName := path.Join(saveDir, SaveFileName)
- err = os.WriteFile(saveFileName, bytes, 0644)
- if err != nil {
- log.Panic(err)
- }
- log.Printf("Write file = %s", saveFileName)
- }
|