package_module_enter.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package ast
  2. import (
  3. "go/ast"
  4. "go/token"
  5. "io"
  6. )
  7. // PackageModuleEnter 模块化入口
  8. // ModuleName := PackageName.AppName.GroupName.ServiceName
  9. type PackageModuleEnter struct {
  10. Base
  11. Type Type // 类型
  12. Path string // 文件路径
  13. ImportPath string // 导包路径
  14. RelativePath string // 相对路径
  15. StructName string // 结构体名称
  16. AppName string // 应用名称
  17. GroupName string // 分组名称
  18. ModuleName string // 模块名称
  19. PackageName string // 包名
  20. ServiceName string // 服务名称
  21. }
  22. func (a *PackageModuleEnter) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
  23. if filename == "" {
  24. if a.RelativePath == "" {
  25. filename = a.Path
  26. a.RelativePath = a.Base.RelativePath(a.Path)
  27. return a.Base.Parse(filename, writer)
  28. }
  29. a.Path = a.Base.AbsolutePath(a.RelativePath)
  30. filename = a.Path
  31. }
  32. return a.Base.Parse(filename, writer)
  33. }
  34. func (a *PackageModuleEnter) Rollback(file *ast.File) error {
  35. for i := 0; i < len(file.Decls); i++ {
  36. v1, o1 := file.Decls[i].(*ast.GenDecl)
  37. if o1 {
  38. for j := 0; j < len(v1.Specs); j++ {
  39. v2, o2 := v1.Specs[j].(*ast.TypeSpec)
  40. if o2 {
  41. if v2.Name.Name != a.Type.Group() {
  42. continue
  43. }
  44. v3, o3 := v2.Type.(*ast.StructType)
  45. if o3 {
  46. for k := 0; k < len(v3.Fields.List); k++ {
  47. v4, o4 := v3.Fields.List[k].Type.(*ast.Ident)
  48. if o4 && v4.Name == a.StructName {
  49. v3.Fields.List = append(v3.Fields.List[:k], v3.Fields.List[k+1:]...)
  50. }
  51. }
  52. }
  53. continue
  54. }
  55. if a.Type == TypePackageServiceModuleEnter {
  56. continue
  57. }
  58. v3, o3 := v1.Specs[j].(*ast.ValueSpec)
  59. if o3 {
  60. if len(v3.Names) == 1 && v3.Names[0].Name == a.ModuleName {
  61. v1.Specs = append(v1.Specs[:j], v1.Specs[j+1:]...)
  62. }
  63. }
  64. if v1.Tok == token.VAR && len(v1.Specs) == 0 {
  65. _ = NewImport(a.ImportPath).Rollback(file)
  66. if i == len(file.Decls) {
  67. file.Decls = append(file.Decls[:i-1])
  68. break
  69. } // 空的var(), 如果不删除则会影响的注入变量, 因为识别不到*ast.ValueSpec
  70. file.Decls = append(file.Decls[:i], file.Decls[i+1:]...)
  71. }
  72. }
  73. }
  74. }
  75. return nil
  76. }
  77. func (a *PackageModuleEnter) Injection(file *ast.File) error {
  78. _ = NewImport(a.ImportPath).Injection(file)
  79. var hasValue bool
  80. var hasVariables bool
  81. for i := 0; i < len(file.Decls); i++ {
  82. v1, o1 := file.Decls[i].(*ast.GenDecl)
  83. if o1 {
  84. if v1.Tok == token.VAR {
  85. hasVariables = true
  86. }
  87. for j := 0; j < len(v1.Specs); j++ {
  88. if a.Type == TypePackageServiceModuleEnter {
  89. hasValue = true
  90. }
  91. v2, o2 := v1.Specs[j].(*ast.TypeSpec)
  92. if o2 {
  93. if v2.Name.Name != a.Type.Group() {
  94. continue
  95. }
  96. v3, o3 := v2.Type.(*ast.StructType)
  97. if o3 {
  98. var hasStruct bool
  99. for k := 0; k < len(v3.Fields.List); k++ {
  100. v4, o4 := v3.Fields.List[k].Type.(*ast.Ident)
  101. if o4 && v4.Name == a.StructName {
  102. hasStruct = true
  103. }
  104. }
  105. if !hasStruct {
  106. field := &ast.Field{Type: &ast.Ident{Name: a.StructName}}
  107. v3.Fields.List = append(v3.Fields.List, field)
  108. }
  109. }
  110. continue
  111. }
  112. v3, o3 := v1.Specs[j].(*ast.ValueSpec)
  113. if o3 {
  114. hasVariables = true
  115. if len(v3.Names) == 1 && v3.Names[0].Name == a.ModuleName {
  116. hasValue = true
  117. }
  118. }
  119. if v1.Tok == token.VAR && len(v1.Specs) == 0 {
  120. hasVariables = false
  121. } // 说明是空var()
  122. if hasVariables && !hasValue {
  123. spec := &ast.ValueSpec{
  124. Names: []*ast.Ident{{Name: a.ModuleName}},
  125. Values: []ast.Expr{
  126. &ast.SelectorExpr{
  127. X: &ast.SelectorExpr{
  128. X: &ast.SelectorExpr{
  129. X: &ast.Ident{Name: a.PackageName},
  130. Sel: &ast.Ident{Name: a.AppName},
  131. },
  132. Sel: &ast.Ident{Name: a.GroupName},
  133. },
  134. Sel: &ast.Ident{Name: a.ServiceName},
  135. },
  136. },
  137. }
  138. v1.Specs = append(v1.Specs, spec)
  139. hasValue = true
  140. }
  141. }
  142. }
  143. }
  144. if !hasValue && !hasVariables {
  145. decl := &ast.GenDecl{
  146. Tok: token.VAR,
  147. Specs: []ast.Spec{
  148. &ast.ValueSpec{
  149. Names: []*ast.Ident{{Name: a.ModuleName}},
  150. Values: []ast.Expr{
  151. &ast.SelectorExpr{
  152. X: &ast.SelectorExpr{
  153. X: &ast.SelectorExpr{
  154. X: &ast.Ident{Name: a.PackageName},
  155. Sel: &ast.Ident{Name: a.AppName},
  156. },
  157. Sel: &ast.Ident{Name: a.GroupName},
  158. },
  159. Sel: &ast.Ident{Name: a.ServiceName},
  160. },
  161. },
  162. },
  163. },
  164. }
  165. file.Decls = append(file.Decls, decl)
  166. }
  167. return nil
  168. }
  169. func (a *PackageModuleEnter) Format(filename string, writer io.Writer, file *ast.File) error {
  170. if filename == "" {
  171. filename = a.Path
  172. }
  173. return a.Base.Format(filename, writer, file)
  174. }