fn.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package spec
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/tal-tech/go-zero/core/stringx"
  6. "github.com/tal-tech/go-zero/tools/goctl/util"
  7. )
  8. const (
  9. bodyTagKey = "json"
  10. formTagKey = "form"
  11. defaultSummaryKey = "summary"
  12. )
  13. var definedKeys = []string{bodyTagKey, formTagKey, "path"}
  14. func (s Service) Routes() []Route {
  15. var result []Route
  16. for _, group := range s.Groups {
  17. result = append(result, group.Routes...)
  18. }
  19. return result
  20. }
  21. func (m Member) Tags() []*Tag {
  22. tags, err := Parse(m.Tag)
  23. if err != nil {
  24. panic(m.Tag + ", " + err.Error())
  25. }
  26. return tags.Tags()
  27. }
  28. func (m Member) IsOptional() bool {
  29. if !m.IsBodyMember() {
  30. return false
  31. }
  32. tag := m.Tags()
  33. for _, item := range tag {
  34. if item.Key == bodyTagKey {
  35. if stringx.Contains(item.Options, "optional") {
  36. return true
  37. }
  38. }
  39. }
  40. return false
  41. }
  42. func (m Member) IsOmitEmpty() bool {
  43. if !m.IsBodyMember() {
  44. return false
  45. }
  46. tag := m.Tags()
  47. for _, item := range tag {
  48. if item.Key == bodyTagKey {
  49. if stringx.Contains(item.Options, "omitempty") {
  50. return true
  51. }
  52. }
  53. }
  54. return false
  55. }
  56. func (m Member) IsOmitempty() bool {
  57. if !m.IsBodyMember() {
  58. return false
  59. }
  60. tag := m.Tags()
  61. for _, item := range tag {
  62. if item.Key == bodyTagKey {
  63. if stringx.Contains(item.Options, "omitempty") {
  64. return true
  65. }
  66. }
  67. }
  68. return false
  69. }
  70. func (m Member) GetPropertyName() (string, error) {
  71. tags := m.Tags()
  72. for _, tag := range tags {
  73. if stringx.Contains(definedKeys, tag.Key) {
  74. if tag.Name == "-" {
  75. return util.Untitle(m.Name), nil
  76. }
  77. return tag.Name, nil
  78. }
  79. }
  80. return "", errors.New("json property name not exist, member: " + m.Name)
  81. }
  82. func (m Member) GetComment() string {
  83. return strings.TrimSpace(m.Comment)
  84. }
  85. func (m Member) IsBodyMember() bool {
  86. if m.IsInline {
  87. return true
  88. }
  89. tags := m.Tags()
  90. for _, tag := range tags {
  91. if tag.Key == bodyTagKey {
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. func (m Member) IsFormMember() bool {
  98. if m.IsInline {
  99. return false
  100. }
  101. tags := m.Tags()
  102. for _, tag := range tags {
  103. if tag.Key == formTagKey {
  104. return true
  105. }
  106. }
  107. return false
  108. }
  109. func (t DefineStruct) GetBodyMembers() []Member {
  110. var result []Member
  111. for _, member := range t.Members {
  112. if member.IsBodyMember() {
  113. result = append(result, member)
  114. }
  115. }
  116. return result
  117. }
  118. func (t DefineStruct) GetFormMembers() []Member {
  119. var result []Member
  120. for _, member := range t.Members {
  121. if member.IsFormMember() {
  122. result = append(result, member)
  123. }
  124. }
  125. return result
  126. }
  127. func (t DefineStruct) GetNonBodyMembers() []Member {
  128. var result []Member
  129. for _, member := range t.Members {
  130. if !member.IsBodyMember() {
  131. result = append(result, member)
  132. }
  133. }
  134. return result
  135. }
  136. func (r Route) JoinedDoc() string {
  137. doc := r.AtDoc.Text
  138. if r.AtDoc.Properties != nil {
  139. doc += r.AtDoc.Properties[defaultSummaryKey]
  140. }
  141. doc += strings.Join(r.Docs, " ")
  142. return strings.TrimSpace(doc)
  143. }
  144. func (r Route) GetAnnotation(key string) string {
  145. if r.Annotation.Properties == nil {
  146. return ""
  147. }
  148. return r.Annotation.Properties[key]
  149. }
  150. func (g Group) GetAnnotation(key string) string {
  151. if g.Annotation.Properties == nil {
  152. return ""
  153. }
  154. return g.Annotation.Properties[key]
  155. }
  156. func (r Route) ResponseTypeName() string {
  157. if r.ResponseType == nil {
  158. return ""
  159. }
  160. return r.ResponseType.Name()
  161. }
  162. func (r Route) RequestTypeName() string {
  163. if r.RequestType == nil {
  164. return ""
  165. }
  166. return r.RequestType.Name()
  167. }