正在显示
4 个修改的文件
包含
304 行增加
和
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 | +*.sum | ||
20 | + | ||
21 | +# protoc | ||
22 | +#*.pb.go |
go.mod
0 → 100644
main.go
0 → 100644
1 | +package main | ||
2 | + | ||
3 | +import ( | ||
4 | + "bytes" | ||
5 | + "flag" | ||
6 | + "fmt" | ||
7 | + "github.com/tal-tech/go-zero/core/mapping" | ||
8 | + "github.com/tiptok/gocomm/common" | ||
9 | + "github.com/tiptok/gocomm/gs" | ||
10 | + "gopkg.in/yaml.v2" | ||
11 | + "html/template" | ||
12 | + "io/fs" | ||
13 | + "io/ioutil" | ||
14 | + "os" | ||
15 | + "path/filepath" | ||
16 | + "sort" | ||
17 | + "strings" | ||
18 | +) | ||
19 | + | ||
20 | +const ( | ||
21 | + ModulePrintModel ="print" | ||
22 | + ModulePrintGateway ="gateway" | ||
23 | +) | ||
24 | + | ||
25 | +var projectPath string | ||
26 | +var useCase string | ||
27 | + | ||
28 | +func init(){ | ||
29 | + flag.StringVar(&useCase,"m","print","module 1.print(打印模型) 2.gateway(输出网关接口)") | ||
30 | + flag.StringVar(&projectPath,"p","F:\\go\\src\\mmm-go-pp\\terms\\document\\terms","项目描述文件根目录") | ||
31 | +} | ||
32 | +var attrMap map[string]*MetaDataItem = make(map[string]*MetaDataItem) | ||
33 | +var schemaMap map[string]Schema = make(map[string]Schema) | ||
34 | +var apiMap map[string]ApiItem= make(map[string]ApiItem) | ||
35 | +var apiKey []string | ||
36 | + | ||
37 | +func main(){ | ||
38 | + flag.Parse() | ||
39 | + schemaPath:=filepath.Join(projectPath,"schemas") | ||
40 | + attributesPath:=filepath.Join(projectPath,"attributes") | ||
41 | + apiPath:=filepath.Join(projectPath,"api") | ||
42 | + valid() | ||
43 | + initSchema(schemaPath) | ||
44 | + initAttr(attributesPath) | ||
45 | + initApi(apiPath) | ||
46 | + switch useCase { | ||
47 | + case ModulePrintModel: | ||
48 | + printSchema() | ||
49 | + case ModulePrintGateway: | ||
50 | + printGateWay() | ||
51 | + } | ||
52 | +} | ||
53 | + | ||
54 | +func valid(){ | ||
55 | + if _,err:=os.Stat(projectPath);err!=nil{ | ||
56 | + fmt.Println(err) | ||
57 | + } | ||
58 | +} | ||
59 | + | ||
60 | +func initSchema(schemaPath string){ | ||
61 | + schemaFiles,_ :=ioutil.ReadDir(schemaPath) | ||
62 | + for _,f :=range schemaFiles{ | ||
63 | + var schema Schema | ||
64 | + if f.IsDir(){ | ||
65 | + continue | ||
66 | + } | ||
67 | + fr,_ :=os.Open(filepath.Join(schemaPath,f.Name())) | ||
68 | + if err :=mapping.UnmarshalYamlReader(fr,&schema);err!=nil{ | ||
69 | + fmt.Printf("file:%v error:%v \n",f.Name(),err.Error()) | ||
70 | + continue | ||
71 | + } | ||
72 | + schemaMap[schema.MetaData.Name] = schema | ||
73 | + } | ||
74 | +} | ||
75 | + | ||
76 | +func initAttr(attributesPath string){ | ||
77 | + filepath.WalkDir(attributesPath,wrapperWalkDirFunc(readAttr)) | ||
78 | +} | ||
79 | + | ||
80 | +func initApi(apiPath string){ | ||
81 | + filepath.WalkDir(apiPath,wrapperWalkDirFunc(readApi)) | ||
82 | +} | ||
83 | + | ||
84 | +func readAttr(f *os.File)error{ | ||
85 | + var attr Attribute | ||
86 | + if err :=mapping.UnmarshalYamlReader(f,&attr);err!=nil{ | ||
87 | + return err | ||
88 | + } | ||
89 | + attrMap[attr.MetaDataItem.Name]=attr.MetaDataItem | ||
90 | + return nil | ||
91 | +} | ||
92 | + | ||
93 | +func readApi(f *os.File)error{ | ||
94 | + var data gs.MapData | ||
95 | + content, err := ioutil.ReadAll(f) | ||
96 | + if err != nil { | ||
97 | + return err | ||
98 | + } | ||
99 | + if err :=yaml.Unmarshal(content, &data);err!=nil{ | ||
100 | + return err | ||
101 | + } | ||
102 | + service :=data.String("metadata.service") | ||
103 | + paths:=data.String("metadata.path") | ||
104 | + endpoints,_:=data.FindField("metadata.endpoints") | ||
105 | + endpointsMap :=endpoints.([]interface{}) | ||
106 | + for _,p:=range endpointsMap{ | ||
107 | + mapP:=p.(map[interface{}]interface{}) | ||
108 | + method :=mapP["method"].(string) | ||
109 | + httpMethod,route :=mapFirstKv(mapP["route"].(map[interface{}]interface{})) | ||
110 | + apiItem:=ApiItem{ | ||
111 | + Method:common.CamelCase(service,true)+common.CamelCase(method,true), | ||
112 | + Path: paths+route, | ||
113 | + HttpMethod:strings.ToUpper(httpMethod), | ||
114 | + } | ||
115 | + apiItem.Path = strings.Replace(apiItem.Path,"}","",-1) | ||
116 | + apiItem.Path = strings.Replace(apiItem.Path,"/{","/:",-1) | ||
117 | + apiMap[apiItem.Method] = apiItem | ||
118 | + apiKey = append(apiKey,apiItem.Method) | ||
119 | + } | ||
120 | + return nil | ||
121 | +} | ||
122 | + | ||
123 | +func wrapperWalkDirFunc(doFile func(f *os.File)error)fs.WalkDirFunc{ | ||
124 | + return func(path string, d fs.DirEntry, err error)error{ | ||
125 | + defer func(){ | ||
126 | + if p:=recover();p!=nil{ | ||
127 | + fmt.Println(path) | ||
128 | + } | ||
129 | + }() | ||
130 | + if d==nil{ | ||
131 | + return nil | ||
132 | + } | ||
133 | + if d.IsDir(){ | ||
134 | + return nil | ||
135 | + } | ||
136 | + if f,err:=os.Open(path);err!=nil{ | ||
137 | + return err | ||
138 | + }else { | ||
139 | + err=doFile(f) | ||
140 | + } | ||
141 | + return err | ||
142 | + } | ||
143 | +} | ||
144 | + | ||
145 | +func mapFirstOne(mts map[string]interface{})string{ | ||
146 | + for k,v:=range mts{ | ||
147 | + value:=common.AssertString(v) | ||
148 | + if strings.ToUpper(k)=="ARRAY"{ | ||
149 | + return "[]"+value | ||
150 | + } | ||
151 | + return value | ||
152 | + } | ||
153 | + return "" | ||
154 | +} | ||
155 | + | ||
156 | +func mapFirstKv(mts map[interface{}]interface{})(string,string){ | ||
157 | + for k,v:=range mts{ | ||
158 | + return k.(string),v.(string) | ||
159 | + } | ||
160 | + return "","" | ||
161 | +} | ||
162 | + | ||
163 | +func printSchema(){ | ||
164 | + var keys []string | ||
165 | + for k,_:=range schemaMap{ | ||
166 | + keys = append(keys,k) | ||
167 | + } | ||
168 | + sort.Strings(keys) | ||
169 | + | ||
170 | + | ||
171 | + for _,k :=range keys{ | ||
172 | + schema:=schemaMap[k] | ||
173 | + fmt.Println("\n\n") | ||
174 | + fmt.Printf("%v %v\n\n",schema.MetaData.Name,schema.MetaData.Description) | ||
175 | + for _,v :=range schema.MetaData.Attributes{ | ||
176 | + if len(v.Ref)>0{ | ||
177 | + if attr,ok:=attrMap[v.Ref];ok{ | ||
178 | + fmt.Printf("%-15s %-10s %v\n",attr.Name,mapFirstOne(attr.Type),attr.Description) | ||
179 | + } | ||
180 | + continue | ||
181 | + } | ||
182 | + fmt.Printf("%-15s %-10s %v\n",v.Name,mapFirstOne(v.Type),v.Description) | ||
183 | + } | ||
184 | + } | ||
185 | +} | ||
186 | + | ||
187 | +func printGateWay(){ | ||
188 | + var keys []string | ||
189 | + for k,_:=range apiMap{ | ||
190 | + keys = append(keys,k) | ||
191 | + } | ||
192 | + sort.Strings(keys) | ||
193 | + | ||
194 | + var apiList []ApiItem | ||
195 | + for _,k:=range keys{ | ||
196 | + apiList = append(apiList,apiMap[k]) | ||
197 | + } | ||
198 | + | ||
199 | + t:=template.New("gateway") | ||
200 | + gt,err:= t.Parse(gatewayTemplate) | ||
201 | + if err!=nil{ | ||
202 | + fmt.Println(err) | ||
203 | + } | ||
204 | + buf :=bytes.NewBuffer(nil) | ||
205 | + gt.Execute(buf,map[string]interface{}{"Routers":apiList}) | ||
206 | + fmt.Println("print gateway") | ||
207 | + fmt.Println(buf.String()) | ||
208 | +} | ||
209 | + |
models.go
0 → 100644
1 | +package main | ||
2 | + | ||
3 | +type Schema struct { | ||
4 | + MetaData *MetaData `json:"metadata,optional"` | ||
5 | +} | ||
6 | +type MetaData struct { | ||
7 | + Name string `json:"name,optional"` | ||
8 | + Description string `json:"description,optional"` | ||
9 | + Attributes []*Attributes `json:"attributes,optional"` | ||
10 | +} | ||
11 | +type Attributes struct { | ||
12 | + Ref string `json:"ref,optional"` | ||
13 | + Name string `json:"name,optional"` | ||
14 | + Description string `json:"description,optional"` | ||
15 | + Type map[string]interface{} `json:"type,optional"` | ||
16 | +} | ||
17 | + | ||
18 | +type Attribute struct { | ||
19 | + MetaDataItem *MetaDataItem `json:"metadata,optional"` | ||
20 | +} | ||
21 | +type MetaDataItem struct { | ||
22 | + Name string `json:"name,optional"` | ||
23 | + Description string `json:"description,optional"` | ||
24 | + Type map[string]interface{} `json:"type,optional"` | ||
25 | +} | ||
26 | + | ||
27 | +type ApiItem struct { | ||
28 | + Method string | ||
29 | + Path string | ||
30 | + //Router string | ||
31 | + HttpMethod string | ||
32 | +} | ||
33 | + | ||
34 | +const gatewayTemplate =` | ||
35 | +package gateway | ||
36 | + | ||
37 | +const ( | ||
38 | + {{range .Routers}} | ||
39 | + {{.Method}} = "{{.Method}}"{{end}} | ||
40 | +) | ||
41 | + | ||
42 | +var DefaultService *HttpDefaultServiceGateway | ||
43 | + | ||
44 | +func init() { | ||
45 | + DefaultService = &HttpDefaultServiceGateway{gs.NewManagerService("127.0.0.1", DefaultRouters())} | ||
46 | + DefaultService.WithDebugModel(true) | ||
47 | +} | ||
48 | + | ||
49 | +type HttpDefaultServiceGateway struct { | ||
50 | + *gs.GatewayService | ||
51 | +} | ||
52 | + | ||
53 | +func DefaultRouters() []gs.Router { | ||
54 | + routers := []gs.Router{ | ||
55 | + {{range .Routers}} | ||
56 | + { {{.Method}}, "{{.Path}}", "{{.HttpMethod}}"},{{end}} | ||
57 | + } | ||
58 | + return routers | ||
59 | +} | ||
60 | +` |
-
请 注册 或 登录 后发表评论