parser.go 9.0 KB

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