fn.go 3.9 KB

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