正在显示
6 个修改的文件
包含
340 行增加
和
0 行删除
.gitignore
0 → 100644
1 | +# Binaries for programs and plugins | ||
2 | +*.exe | ||
3 | +*.dll | ||
4 | +*.so | ||
5 | +*.dylib | ||
6 | +*.vscode | ||
7 | + | ||
8 | +# Test binary, build with `go test -c` | ||
9 | +*.test | ||
10 | + | ||
11 | +# Output of the go coverage tool, specifically when used with LiteIDE | ||
12 | +*.out | ||
13 | + | ||
14 | +*.log | ||
15 | +*debug | ||
16 | +*wasm | ||
17 | + | ||
18 | +*.idea | ||
19 | + | ||
20 | +*.tmp | ||
21 | + | ||
22 | +*.sum | ||
23 | +.out |
cmd/cmd.go
0 → 100644
1 | +package cmd | ||
2 | + | ||
3 | +import | ||
4 | +( | ||
5 | + "bytes" | ||
6 | + "fmt" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/gencode/tmpl" | ||
8 | + "github.com/urfave/cli" | ||
9 | + "html/template" | ||
10 | + "io/ioutil" | ||
11 | + "log" | ||
12 | + "os" | ||
13 | + "strings" | ||
14 | +) | ||
15 | + | ||
16 | + | ||
17 | +type Cmd interface { | ||
18 | + App() *cli.App | ||
19 | + Init(opts ...Option)error | ||
20 | + // Options set within this command | ||
21 | + Options() Options | ||
22 | +} | ||
23 | +type Option func(o *Options) | ||
24 | +type Options struct { | ||
25 | + // For the Command Line itself | ||
26 | + Name string | ||
27 | + Description string | ||
28 | + Version string | ||
29 | +} | ||
30 | +func Name(s string)Option{ | ||
31 | + return func(o *Options){ | ||
32 | + o.Name = s | ||
33 | + } | ||
34 | +} | ||
35 | +func Description(s string)Option{ | ||
36 | + return func(o *Options){ | ||
37 | + o.Description = s | ||
38 | + } | ||
39 | +} | ||
40 | +func Version(s string)Option{ | ||
41 | + return func(o *Options){ | ||
42 | + o.Version = s | ||
43 | + } | ||
44 | +} | ||
45 | + | ||
46 | + | ||
47 | +type cmd struct { | ||
48 | + opts Options | ||
49 | + app *cli.App | ||
50 | +} | ||
51 | + | ||
52 | +func(c *cmd)App()*cli.App{ | ||
53 | + return cli.NewApp() | ||
54 | +} | ||
55 | +func (c *cmd)Init(opts ...Option)error{ | ||
56 | + for _, o := range opts { | ||
57 | + o(&c.opts) | ||
58 | + } | ||
59 | + c.app.Name = c.opts.Name | ||
60 | + c.app.Version = c.opts.Version | ||
61 | + c.app.HideVersion = len(c.opts.Version) == 0 | ||
62 | + c.app.Usage = c.opts.Description | ||
63 | + | ||
64 | + c.app.Commands = append(c.app.Commands,Commands()...) | ||
65 | + return nil | ||
66 | +} | ||
67 | +func(c *cmd)Options()Options{ | ||
68 | + return c.opts | ||
69 | +} | ||
70 | + | ||
71 | + | ||
72 | +var DefaultCmd *cmd | ||
73 | +func newCmd()*cmd{ | ||
74 | + return &cmd{} | ||
75 | +} | ||
76 | +func Init(opts ...Option){ | ||
77 | + DefaultCmd=newCmd() | ||
78 | + DefaultCmd.app = cli.NewApp() | ||
79 | + DefaultCmd.Init(opts...) | ||
80 | + err:= DefaultCmd.app.Run(os.Args) | ||
81 | + if err!=nil{ | ||
82 | + panic(err) | ||
83 | + } | ||
84 | +} | ||
85 | + | ||
86 | +func Commands() []cli.Command{ | ||
87 | + return []cli.Command{ | ||
88 | + { | ||
89 | + Name:"new", | ||
90 | + Usage: "Create a service template; example: gencode new -c Auth -m Login", | ||
91 | + Flags:[]cli.Flag{ | ||
92 | + cli.StringFlag{ | ||
93 | + Name:"c", | ||
94 | + Usage:"controller name for the service", | ||
95 | + Value:"Auth", | ||
96 | + }, | ||
97 | + cli.StringFlag{ | ||
98 | + Name:"m", | ||
99 | + Usage:"controller handler name", | ||
100 | + Value:"Login", | ||
101 | + }, | ||
102 | + }, | ||
103 | + Action:run, | ||
104 | + }, | ||
105 | + { | ||
106 | + Name:"param", | ||
107 | + Usage: "crate model: gencode param -n VersionNo -t int -v Require,Mobile", | ||
108 | + Flags:[]cli.Flag{ | ||
109 | + cli.StringFlag{ | ||
110 | + Name:"n", | ||
111 | + Usage:"name of a param", | ||
112 | + Value:"Xxx", | ||
113 | + }, | ||
114 | + cli.StringFlag{ | ||
115 | + Name:"t", | ||
116 | + Usage:"type of a param", | ||
117 | + Value:"string", | ||
118 | + }, | ||
119 | + cli.StringFlag{ | ||
120 | + Name:"v", | ||
121 | + Usage:"valid param: Require,Mobile,Email", | ||
122 | + Value:"Require", | ||
123 | + }, | ||
124 | + }, | ||
125 | + Action:runParam, | ||
126 | + }, | ||
127 | + } | ||
128 | +} | ||
129 | + | ||
130 | +func runParam(ctx *cli.Context) { | ||
131 | + var ( | ||
132 | + name string = ctx.String("n") | ||
133 | + tType string = ctx.String("t") | ||
134 | + validStr string = ctx.String("v") | ||
135 | + ) | ||
136 | + tP, err := template.New("controller").Parse(tmpl.Param) | ||
137 | + if err != nil { | ||
138 | + log.Fatal(err) | ||
139 | + } | ||
140 | + | ||
141 | + buf :=bytes.NewBuffer(nil) | ||
142 | + m :=make(map[string]string) | ||
143 | + m["Name"]=name | ||
144 | + m["NameLowcase"]=LowFirstCase(name) | ||
145 | + m["TypeName"]=tType | ||
146 | + m["ValidString"]=strings.Replace(validStr,",",";",-1) | ||
147 | + tP.Execute(buf,m) | ||
148 | + fmt.Println(buf.String()) | ||
149 | +} | ||
150 | + | ||
151 | + | ||
152 | +func run(ctx *cli.Context){ | ||
153 | + var ( | ||
154 | + controller string=ctx.String("c") | ||
155 | + method string =ctx.String("m") | ||
156 | + ) | ||
157 | + tC,err := template.New("controller").Parse(tmpl.ControllerMethod) | ||
158 | + if err!=nil{ | ||
159 | + log.Fatal(err) | ||
160 | + } | ||
161 | + //param -c Auth -m Login | ||
162 | + //Controller Auth | ||
163 | + //ControllerLowcase auth | ||
164 | + //Method Login | ||
165 | + //MethodRequest LoginRequest | ||
166 | + m :=make(map[string]string) | ||
167 | + m["Controller"]=controller | ||
168 | + m["ControllerLowcase"]=LowFirstCase(controller) | ||
169 | + m["Method"] = method | ||
170 | + m["MethodLowcase"]=LowFirstCase(method) | ||
171 | + buf :=bytes.NewBuffer(nil) | ||
172 | + tC.Execute(buf,m) | ||
173 | + | ||
174 | + tP,err :=template.New("protocol").Parse(tmpl.ProtocolModel) | ||
175 | + tP.Execute(buf,m) | ||
176 | + | ||
177 | + tH,err :=template.New("protocol").Parse(tmpl.Handler) | ||
178 | + tH.Execute(buf,m) | ||
179 | + | ||
180 | + tR,err :=template.New("route").Parse(tmpl.Router) | ||
181 | + tR.Execute(buf,m) | ||
182 | + //log.Println(buf.String()) | ||
183 | + ioutil.WriteFile("gencode.out",buf.Bytes(),os.ModePerm) | ||
184 | +} | ||
185 | + | ||
186 | +//单词首字母小写 | ||
187 | +func LowFirstCase(input string)string{ | ||
188 | + array :=[]byte(input) | ||
189 | + if len(array)==0{ | ||
190 | + return "" | ||
191 | + } | ||
192 | + rspArray :=make([]byte,len(array)) | ||
193 | + copy(rspArray[:1],strings.ToLower(string(array[:1]))) | ||
194 | + copy(rspArray[1:],array[1:]) | ||
195 | + return string(rspArray) | ||
196 | +} | ||
197 | + | ||
198 | + | ||
199 | + | ||
200 | + | ||
201 | + | ||
202 | + |
cmd/cmd_test.go
0 → 100644
1 | +package cmd | ||
2 | + | ||
3 | +import ( | ||
4 | + "log" | ||
5 | + "strings" | ||
6 | + "testing" | ||
7 | +) | ||
8 | + | ||
9 | +func TestLowFirstCase(t *testing.T){ | ||
10 | + input :=[]struct{ | ||
11 | + In string | ||
12 | + Out string | ||
13 | + }{ | ||
14 | + {In:"Lower",Out:"lower"}, | ||
15 | + {In:"First",Out:"first"}, | ||
16 | + {In:"Test",Out:"test"}, | ||
17 | + {In:"",Out:""}, | ||
18 | + {In:"T",Out:"t"}, | ||
19 | + {In:"Te",Out:"te"}, | ||
20 | + } | ||
21 | + for i:=range input{ | ||
22 | + result :=LowFirstCase(input[i].In) | ||
23 | + if strings.Compare(result,input[i].Out)!=0{ | ||
24 | + log.Fatal("Result:",result," not equal Out:",input[i].Out) | ||
25 | + }else{ | ||
26 | + log.Println(input[i].In,result,input[i].Out) | ||
27 | + } | ||
28 | + } | ||
29 | +} |
go.mod
0 → 100644
main.go
0 → 100644
tmpl/tmpl.go
0 → 100644
1 | +package tmpl | ||
2 | + | ||
3 | +//param -c Auth -m Login | ||
4 | +//Controller Auth | ||
5 | +//ControllerLowcase auth | ||
6 | +//Method Login | ||
7 | +//MethodLowcase login | ||
8 | +var ControllerMethod =` | ||
9 | +//{{.Method}} | ||
10 | +func(this *{{.Controller}}Controller){{.Method}}(){ | ||
11 | + var msg *mybeego.Message | ||
12 | + defer func(){ | ||
13 | + this.Resp(msg) | ||
14 | + }() | ||
15 | + var request *protocol.{{.Method}}Request | ||
16 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
17 | + log.Error(err) | ||
18 | + msg = mybeego.NewMessage(1) | ||
19 | + return | ||
20 | + } | ||
21 | + if b,m :=this.Valid(request);!b{ | ||
22 | + msg = m | ||
23 | + return | ||
24 | + } | ||
25 | + msg = this.GenMessage({{.ControllerLowcase}}.{{.Method}}(request)) | ||
26 | +} | ||
27 | +` | ||
28 | + | ||
29 | +var ProtocolModel =` | ||
30 | +/*{{.Method}} */ | ||
31 | +type {{.Method}}Request struct { | ||
32 | + Xxx string` +"`json:\"xxx\" valid:\"Required\"`" +` | ||
33 | +} | ||
34 | +type {{.Method}}Response struct { | ||
35 | +} | ||
36 | +` | ||
37 | + | ||
38 | +//Method Login | ||
39 | +var Handler =` | ||
40 | + func {{.Method}}(request *protocol.{{.Method}}Request)(rsp *protocol.{{.Method}}Response,err error){ | ||
41 | + var ( | ||
42 | + | ||
43 | + ) | ||
44 | + rsp =&protocol.{{.Method}}Response{} | ||
45 | + return | ||
46 | +} | ||
47 | +` | ||
48 | + | ||
49 | +var Router =` | ||
50 | +/*{{.MethodLowcase}} controller*/ | ||
51 | +{ | ||
52 | + {{.ControllerLowcase}} :=&v1.{{.Controller}}Controller{} | ||
53 | + nsV1.Router("/{{.ControllerLowcase}}/{{.MethodLowcase}}",{{.ControllerLowcase}},"post:{{.Method}}") | ||
54 | +} | ||
55 | +` | ||
56 | + | ||
57 | +//Name Phone | ||
58 | +//NameLowcase phone | ||
59 | +//TypeName string | ||
60 | +//ValidString Required;Mobile | ||
61 | +var Param =` | ||
62 | +{{.Name}} {{.TypeName}} `+"`json:\"{{.NameLowcase}}\" valid:\"{{.ValidString}}\"`" | ||
63 | + |
-
请 注册 或 登录 后发表评论