fn.go 3.9 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. // Routes returns all routes in api service
  15. func (s Service) Routes() []Route {
  16. var result []Route
  17. for _, group := range s.Groups {
  18. result = append(result, group.Routes...)
  19. }
  20. return result
  21. }
  22. // Tags retuens all tags in Member
  23. func (m Member) Tags() []*Tag {
  24. tags, err := Parse(m.Tag)
  25. if err != nil {
  26. panic(m.Tag + ", " + err.Error())
  27. }
  28. return tags.Tags()
  29. }
  30. // IsOptional returns true if tag is optional
  31. func (m Member) IsOptional() bool {
  32. if !m.IsBodyMember() {
  33. return false
  34. }
  35. tag := m.Tags()
  36. for _, item := range tag {
  37. if item.Key == bodyTagKey {
  38. if stringx.Contains(item.Options, "optional") {
  39. return true
  40. }
  41. }
  42. }
  43. return false
  44. }
  45. // IsOmitEmpty returns true if tag contains omitempty
  46. func (m Member) IsOmitEmpty() bool {
  47. if !m.IsBodyMember() {
  48. return false
  49. }
  50. tag := m.Tags()
  51. for _, item := range tag {
  52. if item.Key == bodyTagKey {
  53. if stringx.Contains(item.Options, "omitempty") {
  54. return true
  55. }
  56. }
  57. }
  58. return false
  59. }
  60. // GetPropertyName returns json tag value
  61. func (m Member) GetPropertyName() (string, error) {
  62. tags := m.Tags()
  63. for _, tag := range tags {
  64. if stringx.Contains(definedKeys, tag.Key) {
  65. if tag.Name == "-" {
  66. return util.Untitle(m.Name), nil
  67. }
  68. return tag.Name, nil
  69. }
  70. }
  71. return "", errors.New("json property name not exist, member: " + m.Name)
  72. }
  73. // GetComment returns comment value of Member
  74. func (m Member) GetComment() string {
  75. return strings.TrimSpace(m.Comment)
  76. }
  77. // IsBodyMember returns true if contains json tag
  78. func (m Member) IsBodyMember() bool {
  79. if m.IsInline {
  80. return true
  81. }
  82. tags := m.Tags()
  83. for _, tag := range tags {
  84. if tag.Key == bodyTagKey {
  85. return true
  86. }
  87. }
  88. return false
  89. }
  90. // IsFormMember returns true if contains form tag
  91. func (m Member) IsFormMember() bool {
  92. if m.IsInline {
  93. return false
  94. }
  95. tags := m.Tags()
  96. for _, tag := range tags {
  97. if tag.Key == formTagKey {
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. // GetBodyMembers returns all json fields
  104. func (t DefineStruct) GetBodyMembers() []Member {
  105. var result []Member
  106. for _, member := range t.Members {
  107. if member.IsBodyMember() {
  108. result = append(result, member)
  109. }
  110. }
  111. return result
  112. }
  113. // GetFormMembers returns all form fields
  114. func (t DefineStruct) GetFormMembers() []Member {
  115. var result []Member
  116. for _, member := range t.Members {
  117. if member.IsFormMember() {
  118. result = append(result, member)
  119. }
  120. }
  121. return result
  122. }
  123. // GetNonBodyMembers retruns all have no tag fields
  124. func (t DefineStruct) GetNonBodyMembers() []Member {
  125. var result []Member
  126. for _, member := range t.Members {
  127. if !member.IsBodyMember() {
  128. result = append(result, member)
  129. }
  130. }
  131. return result
  132. }
  133. // JoinedDoc joins comments and summary value in AtDoc
  134. func (r Route) JoinedDoc() string {
  135. doc := r.AtDoc.Text
  136. if r.AtDoc.Properties != nil {
  137. doc += r.AtDoc.Properties[defaultSummaryKey]
  138. }
  139. doc += strings.Join(r.Docs, " ")
  140. return strings.TrimSpace(doc)
  141. }
  142. // GetAnnotation returns the value by specified key
  143. func (r Route) GetAnnotation(key string) string {
  144. if r.Annotation.Properties == nil {
  145. return ""
  146. }
  147. return r.Annotation.Properties[key]
  148. }
  149. // GetAnnotation returns the value by specified key
  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. // ResponseTypeName returns response type name of route
  157. func (r Route) ResponseTypeName() string {
  158. if r.ResponseType == nil {
  159. return ""
  160. }
  161. return r.ResponseType.Name()
  162. }
  163. // RequestTypeName returns request type name of route
  164. func (r Route) RequestTypeName() string {
  165. if r.RequestType == nil {
  166. return ""
  167. }
  168. return r.RequestType.Name()
  169. }