genenums_parser.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package genenums
  7. import (
  8. "go/constant"
  9. "go/types"
  10. "golang.org/x/tools/go/packages"
  11. "github.com/gogf/gf/v2/encoding/gjson"
  12. "github.com/gogf/gf/v2/text/gstr"
  13. "github.com/gogf/gf/v2/util/gconv"
  14. )
  15. const pkgLoadMode = 0xffffff
  16. type EnumsParser struct {
  17. enums []EnumItem
  18. parsedPkg map[string]struct{}
  19. prefixes []string
  20. }
  21. type EnumItem struct {
  22. Name string
  23. Value string
  24. Kind constant.Kind // String/Int/Bool/Float/Complex/Unknown
  25. Type string // Pkg.ID + TypeName
  26. }
  27. var standardPackages = make(map[string]struct{})
  28. func init() {
  29. stdPackages, err := packages.Load(nil, "std")
  30. if err != nil {
  31. panic(err)
  32. }
  33. for _, p := range stdPackages {
  34. standardPackages[p.ID] = struct{}{}
  35. }
  36. }
  37. func NewEnumsParser(prefixes []string) *EnumsParser {
  38. return &EnumsParser{
  39. enums: make([]EnumItem, 0),
  40. parsedPkg: make(map[string]struct{}),
  41. prefixes: prefixes,
  42. }
  43. }
  44. func (p *EnumsParser) ParsePackages(pkgs []*packages.Package) {
  45. for _, pkg := range pkgs {
  46. p.ParsePackage(pkg)
  47. }
  48. }
  49. func (p *EnumsParser) ParsePackage(pkg *packages.Package) {
  50. // Ignore std packages.
  51. if _, ok := standardPackages[pkg.ID]; ok {
  52. return
  53. }
  54. // Ignore pared packages.
  55. if _, ok := p.parsedPkg[pkg.ID]; ok {
  56. return
  57. }
  58. p.parsedPkg[pkg.ID] = struct{}{}
  59. // Only parse specified prefixes.
  60. if len(p.prefixes) > 0 {
  61. var hasPrefix bool
  62. for _, prefix := range p.prefixes {
  63. if hasPrefix = gstr.HasPrefix(pkg.ID, prefix); hasPrefix {
  64. break
  65. }
  66. }
  67. if !hasPrefix {
  68. return
  69. }
  70. }
  71. var (
  72. scope = pkg.Types.Scope()
  73. names = scope.Names()
  74. )
  75. for _, name := range names {
  76. con, ok := scope.Lookup(name).(*types.Const)
  77. if !ok {
  78. // Only constants can be enums.
  79. continue
  80. }
  81. if !con.Exported() {
  82. // Ignore unexported values.
  83. continue
  84. }
  85. var enumType = con.Type().String()
  86. if !gstr.Contains(enumType, "/") {
  87. // Ignore std types.
  88. continue
  89. }
  90. var (
  91. enumName = con.Name()
  92. enumValue = con.Val().ExactString()
  93. enumKind = con.Val().Kind()
  94. )
  95. if con.Val().Kind() == constant.String {
  96. enumValue = constant.StringVal(con.Val())
  97. }
  98. p.enums = append(p.enums, EnumItem{
  99. Name: enumName,
  100. Value: enumValue,
  101. Type: enumType,
  102. Kind: enumKind,
  103. })
  104. }
  105. for _, im := range pkg.Imports {
  106. p.ParsePackage(im)
  107. }
  108. }
  109. func (p *EnumsParser) Export() string {
  110. var typeEnumMap = make(map[string][]interface{})
  111. for _, enum := range p.enums {
  112. if typeEnumMap[enum.Type] == nil {
  113. typeEnumMap[enum.Type] = make([]interface{}, 0)
  114. }
  115. var value interface{}
  116. switch enum.Kind {
  117. case constant.Int:
  118. value = gconv.Int64(enum.Value)
  119. case constant.String:
  120. value = enum.Value
  121. case constant.Float:
  122. value = gconv.Float64(enum.Value)
  123. case constant.Bool:
  124. value = gconv.Bool(enum.Value)
  125. default:
  126. value = enum.Value
  127. }
  128. typeEnumMap[enum.Type] = append(typeEnumMap[enum.Type], value)
  129. }
  130. return gjson.MustEncodeString(typeEnumMap)
  131. }