parser.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package parser
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "unicode"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/ast"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/gen/api"
  8. "git.i2edu.net/i2/go-zero/tools/goctl/api/spec"
  9. )
  10. type parser struct {
  11. ast *ast.Api
  12. spec *spec.ApiSpec
  13. }
  14. // Parse parses the api file
  15. func Parse(filename string) (*spec.ApiSpec, error) {
  16. astParser := ast.NewParser(ast.WithParserPrefix(filepath.Base(filename)))
  17. ast, err := astParser.Parse(filename)
  18. if err != nil {
  19. return nil, err
  20. }
  21. spec := new(spec.ApiSpec)
  22. p := parser{ast: ast, spec: spec}
  23. err = p.convert2Spec()
  24. if err != nil {
  25. return nil, err
  26. }
  27. return spec, nil
  28. }
  29. // ParseContent parses the api content
  30. func ParseContent(content string, filename ...string) (*spec.ApiSpec, error) {
  31. astParser := ast.NewParser()
  32. ast, err := astParser.ParseContent(content, filename...)
  33. if err != nil {
  34. return nil, err
  35. }
  36. spec := new(spec.ApiSpec)
  37. p := parser{ast: ast, spec: spec}
  38. err = p.convert2Spec()
  39. if err != nil {
  40. return nil, err
  41. }
  42. return spec, nil
  43. }
  44. func (p parser) convert2Spec() error {
  45. p.fillInfo()
  46. p.fillSyntax()
  47. p.fillImport()
  48. err := p.fillTypes()
  49. if err != nil {
  50. return err
  51. }
  52. return p.fillService()
  53. }
  54. func (p parser) fillInfo() {
  55. properties := make(map[string]string, 0)
  56. if p.ast.Info != nil {
  57. p.spec.Info = spec.Info{}
  58. for _, kv := range p.ast.Info.Kvs {
  59. properties[kv.Key.Text()] = kv.Value.Text()
  60. }
  61. }
  62. p.spec.Info.Properties = properties
  63. }
  64. func (p parser) fillSyntax() {
  65. if p.ast.Syntax != nil {
  66. p.spec.Syntax = spec.ApiSyntax{
  67. Version: p.ast.Syntax.Version.Text(),
  68. Doc: p.stringExprs(p.ast.Syntax.DocExpr),
  69. Comment: p.stringExprs([]ast.Expr{p.ast.Syntax.CommentExpr}),
  70. }
  71. }
  72. }
  73. func (p parser) fillImport() {
  74. if len(p.ast.Import) > 0 {
  75. for _, item := range p.ast.Import {
  76. p.spec.Imports = append(p.spec.Imports, spec.Import{
  77. Value: item.Value.Text(),
  78. Doc: p.stringExprs(item.DocExpr),
  79. Comment: p.stringExprs([]ast.Expr{item.CommentExpr}),
  80. })
  81. }
  82. }
  83. }
  84. func (p parser) fillTypes() error {
  85. for _, item := range p.ast.Type {
  86. switch v := (item).(type) {
  87. case *ast.TypeStruct:
  88. var members []spec.Member
  89. for _, item := range v.Fields {
  90. members = append(members, p.fieldToMember(item))
  91. }
  92. p.spec.Types = append(p.spec.Types, spec.DefineStruct{
  93. RawName: v.Name.Text(),
  94. Members: members,
  95. Docs: p.stringExprs(v.Doc()),
  96. })
  97. default:
  98. return fmt.Errorf("unknown type %+v", v)
  99. }
  100. }
  101. var types []spec.Type
  102. for _, item := range p.spec.Types {
  103. switch v := (item).(type) {
  104. case spec.DefineStruct:
  105. var members []spec.Member
  106. for _, member := range v.Members {
  107. switch v := member.Type.(type) {
  108. case spec.DefineStruct:
  109. tp, err := p.findDefinedType(v.RawName)
  110. if err != nil {
  111. return err
  112. }
  113. member.Type = *tp
  114. }
  115. members = append(members, member)
  116. }
  117. v.Members = members
  118. types = append(types, v)
  119. default:
  120. return fmt.Errorf("unknown type %+v", v)
  121. }
  122. }
  123. p.spec.Types = types
  124. return nil
  125. }
  126. func (p parser) findDefinedType(name string) (*spec.Type, error) {
  127. for _, item := range p.spec.Types {
  128. if _, ok := item.(spec.DefineStruct); ok {
  129. if item.Name() == name {
  130. return &item, nil
  131. }
  132. }
  133. }
  134. return nil, fmt.Errorf("type %s not defined", name)
  135. }
  136. func (p parser) fieldToMember(field *ast.TypeField) spec.Member {
  137. name := ""
  138. tag := ""
  139. if !field.IsAnonymous {
  140. name = field.Name.Text()
  141. if field.Tag == nil {
  142. panic(fmt.Sprintf("error: line %d:%d field %s has no tag", field.Name.Line(), field.Name.Column(),
  143. field.Name.Text()))
  144. }
  145. tag = field.Tag.Text()
  146. }
  147. return spec.Member{
  148. Name: name,
  149. Type: p.astTypeToSpec(field.DataType),
  150. Tag: tag,
  151. Comment: p.commentExprs(field.Comment()),
  152. Docs: p.stringExprs(field.Doc()),
  153. IsInline: field.IsAnonymous,
  154. }
  155. }
  156. func (p parser) astTypeToSpec(in ast.DataType) spec.Type {
  157. switch v := (in).(type) {
  158. case *ast.Literal:
  159. raw := v.Literal.Text()
  160. if api.IsBasicType(raw) {
  161. return spec.PrimitiveType{
  162. RawName: raw,
  163. }
  164. }
  165. return spec.DefineStruct{
  166. RawName: raw,
  167. }
  168. case *ast.Interface:
  169. return spec.InterfaceType{RawName: v.Literal.Text()}
  170. case *ast.Map:
  171. return spec.MapType{RawName: v.MapExpr.Text(), Key: v.Key.Text(), Value: p.astTypeToSpec(v.Value)}
  172. case *ast.Array:
  173. return spec.ArrayType{RawName: v.ArrayExpr.Text(), Value: p.astTypeToSpec(v.Literal)}
  174. case *ast.Pointer:
  175. raw := v.Name.Text()
  176. if api.IsBasicType(raw) {
  177. return spec.PointerType{RawName: v.PointerExpr.Text(), Type: spec.PrimitiveType{RawName: raw}}
  178. }
  179. return spec.PointerType{RawName: v.PointerExpr.Text(), Type: spec.DefineStruct{RawName: raw}}
  180. }
  181. panic(fmt.Sprintf("unspported type %+v", in))
  182. }
  183. func (p parser) stringExprs(docs []ast.Expr) []string {
  184. var result []string
  185. for _, item := range docs {
  186. if item == nil {
  187. continue
  188. }
  189. result = append(result, item.Text())
  190. }
  191. return result
  192. }
  193. func (p parser) commentExprs(comment ast.Expr) string {
  194. if comment == nil {
  195. return ""
  196. }
  197. return comment.Text()
  198. }
  199. func (p parser) fillService() error {
  200. var groups []spec.Group
  201. for _, item := range p.ast.Service {
  202. var group spec.Group
  203. p.fillAtServer(item, &group)
  204. for _, astRoute := range item.ServiceApi.ServiceRoute {
  205. route := spec.Route{
  206. AtServerAnnotation: spec.Annotation{},
  207. Method: astRoute.Route.Method.Text(),
  208. Path: astRoute.Route.Path.Text(),
  209. Doc: p.stringExprs(astRoute.Route.DocExpr),
  210. Comment: p.stringExprs([]ast.Expr{astRoute.Route.CommentExpr}),
  211. }
  212. if astRoute.AtHandler != nil {
  213. route.Handler = astRoute.AtHandler.Name.Text()
  214. route.HandlerDoc = append(route.HandlerDoc, p.stringExprs(astRoute.AtHandler.DocExpr)...)
  215. route.HandlerComment = append(route.HandlerComment, p.stringExprs([]ast.Expr{astRoute.AtHandler.CommentExpr})...)
  216. }
  217. err := p.fillRouteAtServer(astRoute, &route)
  218. if err != nil {
  219. return err
  220. }
  221. if astRoute.Route.Req != nil {
  222. route.RequestType = p.astTypeToSpec(astRoute.Route.Req.Name)
  223. }
  224. if astRoute.Route.Reply != nil {
  225. route.ResponseType = p.astTypeToSpec(astRoute.Route.Reply.Name)
  226. }
  227. if astRoute.AtDoc != nil {
  228. properties := make(map[string]string, 0)
  229. for _, kv := range astRoute.AtDoc.Kv {
  230. properties[kv.Key.Text()] = kv.Value.Text()
  231. }
  232. route.AtDoc.Properties = properties
  233. if astRoute.AtDoc.LineDoc != nil {
  234. route.AtDoc.Text = astRoute.AtDoc.LineDoc.Text()
  235. }
  236. }
  237. err = p.fillRouteType(&route)
  238. if err != nil {
  239. return err
  240. }
  241. group.Routes = append(group.Routes, route)
  242. name := item.ServiceApi.Name.Text()
  243. if len(p.spec.Service.Name) > 0 && p.spec.Service.Name != name {
  244. return fmt.Errorf("mulit service name defined %s and %s", name, p.spec.Service.Name)
  245. }
  246. p.spec.Service.Name = name
  247. }
  248. groups = append(groups, group)
  249. }
  250. p.spec.Service.Groups = groups
  251. return nil
  252. }
  253. func (p parser) fillRouteAtServer(astRoute *ast.ServiceRoute, route *spec.Route) error {
  254. if astRoute.AtServer != nil {
  255. properties := make(map[string]string, 0)
  256. for _, kv := range astRoute.AtServer.Kv {
  257. properties[kv.Key.Text()] = kv.Value.Text()
  258. }
  259. route.AtServerAnnotation.Properties = properties
  260. if len(route.Handler) == 0 {
  261. route.Handler = properties["handler"]
  262. }
  263. if len(route.Handler) == 0 {
  264. return fmt.Errorf("missing handler annotation for %q", route.Path)
  265. }
  266. for _, char := range route.Handler {
  267. if !unicode.IsDigit(char) && !unicode.IsLetter(char) {
  268. return fmt.Errorf("route [%s] handler [%s] invalid, handler name should only contains letter or digit",
  269. route.Path, route.Handler)
  270. }
  271. }
  272. }
  273. return nil
  274. }
  275. func (p parser) fillAtServer(item *ast.Service, group *spec.Group) {
  276. if item.AtServer != nil {
  277. properties := make(map[string]string, 0)
  278. for _, kv := range item.AtServer.Kv {
  279. properties[kv.Key.Text()] = kv.Value.Text()
  280. }
  281. group.Annotation.Properties = properties
  282. }
  283. }
  284. func (p parser) fillRouteType(route *spec.Route) error {
  285. if route.RequestType != nil {
  286. switch route.RequestType.(type) {
  287. case spec.DefineStruct:
  288. tp, err := p.findDefinedType(route.RequestType.Name())
  289. if err != nil {
  290. return err
  291. }
  292. route.RequestType = *tp
  293. }
  294. }
  295. if route.ResponseType != nil {
  296. switch route.ResponseType.(type) {
  297. case spec.DefineStruct:
  298. tp, err := p.findDefinedType(route.ResponseType.Name())
  299. if err != nil {
  300. return err
  301. }
  302. route.ResponseType = *tp
  303. }
  304. }
  305. return nil
  306. }