forked from go-kratos/kratos
/
graphql.go
123 lines (111 loc) · 2.91 KB
/
graphql.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
package graphql
import (
"fmt"
"log"
"os"
"path"
"strings"
"github.com/emicklei/proto"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// CmdServer the service command.
var CmdServer = &cobra.Command{
Use: "graphql",
Short: "Generate the proto Server implementations",
Long: "Generate the proto Server implementations. Example: kratos proto server api/xxx.proto -target-dir=internal/service",
Run: run,
}
var targetDir string
// client or server
var modeType bool
func init() {
CmdServer.Flags().StringVarP(&targetDir, "target-dir", "t", "internal/service/graph", "generate target directory")
CmdServer.Flags().BoolVarP(&modeType, "mode-type", "m", false, "generate mode type")
}
func run(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "Please specify the proto file. Example: kratos proto server api/xxx.proto")
return
}
reader, err := os.Open(args[0])
if err != nil {
log.Fatal(err)
}
defer reader.Close()
parser := proto.NewParser(reader)
definition, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
var (
pkg string
res []*Service
)
proto.Walk(definition,
proto.WithOption(func(o *proto.Option) {
if o.Name == "go_package" {
pkg = strings.Split(o.Constant.Source, ";")[0]
}
}),
proto.WithService(func(s *proto.Service) {
cs := &Service{
Package: pkg,
Service: serviceName(s.Name),
}
for _, e := range s.Elements {
r, ok := e.(*proto.RPC)
if !ok {
continue
}
cs.Methods = append(cs.Methods, &Method{
Service: serviceName(s.Name), Name: serviceName(r.Name), Request: r.RequestType,
Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns),
ModeType: modeType,
})
}
res = append(res, cs)
}),
)
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
fmt.Printf("Target directory: %s does not exsit\n", targetDir)
return
}
for _, s := range res {
to := path.Join(targetDir, strings.ToLower(s.Service)+".graphql.go")
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "%s already exists: %s\n", s.Service, to)
continue
}
s.ModeType = modeType
b, err := s.execute()
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile(to, b, 0o644); err != nil {
log.Fatal(err)
}
fmt.Println(to)
}
}
func getMethodType(streamsRequest, streamsReturns bool) MethodType {
if !streamsRequest && !streamsReturns {
return unaryType
} else if streamsRequest && streamsReturns {
return twoWayStreamsType
} else if streamsRequest {
return requestStreamsType
} else if streamsReturns {
return returnsStreamsType
}
return unaryType
}
func serviceName(name string) string {
return toUpperCamelCase(strings.Split(name, ".")[0])
}
func toUpperCamelCase(s string) string {
s = strings.ReplaceAll(s, "_", " ")
s = cases.Title(language.Und, cases.NoLower).String(s)
return strings.ReplaceAll(s, " ", "")
}