fn.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package spec
  2. import (
  3. "errors"
  4. "regexp"
  5. "strings"
  6. "zero/core/stringx"
  7. "zero/tools/goctl/util"
  8. )
  9. const (
  10. TagKey = "tag"
  11. NameKey = "name"
  12. OptionKey = "option"
  13. BodyTag = "json"
  14. )
  15. var (
  16. TagRe = regexp.MustCompile(`(?P<tag>\w+):"(?P<name>[^,"]+)[,]?(?P<option>[^"]*)"`)
  17. TagSubNames = TagRe.SubexpNames()
  18. definedTags = []string{TagKey, NameKey, OptionKey}
  19. )
  20. type Attribute struct {
  21. Key string
  22. value string
  23. }
  24. func (m Member) IsOptional() bool {
  25. var option string
  26. matches := TagRe.FindStringSubmatch(m.Tag)
  27. for i := range matches {
  28. name := TagSubNames[i]
  29. if name == OptionKey {
  30. option = matches[i]
  31. }
  32. }
  33. if len(option) == 0 {
  34. return false
  35. }
  36. fields := strings.Split(option, ",")
  37. for _, field := range fields {
  38. if field == "optional" || strings.HasPrefix(field, "default=") {
  39. return true
  40. }
  41. }
  42. return false
  43. }
  44. func (m Member) IsOmitempty() bool {
  45. var option string
  46. matches := TagRe.FindStringSubmatch(m.Tag)
  47. for i := range matches {
  48. name := TagSubNames[i]
  49. if name == OptionKey {
  50. option = matches[i]
  51. }
  52. }
  53. if len(option) == 0 {
  54. return false
  55. }
  56. fields := strings.Split(option, ",")
  57. for _, field := range fields {
  58. if field == "omitempty" {
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. func (m Member) GetAttributes() []Attribute {
  65. matches := TagRe.FindStringSubmatch(m.Tag)
  66. var result []Attribute
  67. for i := range matches {
  68. name := TagSubNames[i]
  69. if stringx.Contains(definedTags, name) {
  70. result = append(result, Attribute{
  71. Key: name,
  72. value: matches[i],
  73. })
  74. }
  75. }
  76. return result
  77. }
  78. func (m Member) GetPropertyName() (string, error) {
  79. attrs := m.GetAttributes()
  80. for _, attr := range attrs {
  81. if attr.Key == NameKey && len(attr.value) > 0 {
  82. if attr.value == "-" {
  83. return util.Untitle(m.Name), nil
  84. }
  85. return attr.value, nil
  86. }
  87. }
  88. return "", errors.New("json property name not exist, member: " + m.Name)
  89. }
  90. func (m Member) GetComment() string {
  91. return strings.TrimSpace(strings.Join(m.Comments, "; "))
  92. }
  93. func (m Member) IsBodyMember() bool {
  94. if m.IsInline {
  95. return true
  96. }
  97. attrs := m.GetAttributes()
  98. for _, attr := range attrs {
  99. if attr.value == BodyTag {
  100. return true
  101. }
  102. }
  103. return false
  104. }
  105. func (t Type) GetBodyMembers() []Member {
  106. var result []Member
  107. for _, member := range t.Members {
  108. if member.IsBodyMember() {
  109. result = append(result, member)
  110. }
  111. }
  112. return result
  113. }
  114. func (t Type) GetNonBodyMembers() []Member {
  115. var result []Member
  116. for _, member := range t.Members {
  117. if !member.IsBodyMember() {
  118. result = append(result, member)
  119. }
  120. }
  121. return result
  122. }