123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package spec
- import (
- "errors"
- "strings"
- "git.i2edu.net/i2/go-zero/core/stringx"
- "git.i2edu.net/i2/go-zero/tools/goctl/util"
- )
- const (
- bodyTagKey = "json"
- formTagKey = "form"
- pathTagKey = "path"
- defaultSummaryKey = "summary"
- )
- var definedKeys = []string{bodyTagKey, formTagKey, pathTagKey}
- // Routes returns all routes in api service
- func (s Service) Routes() []Route {
- var result []Route
- for _, group := range s.Groups {
- result = append(result, group.Routes...)
- }
- return result
- }
- // Tags returns all tags in Member
- func (m Member) Tags() []*Tag {
- tags, err := Parse(m.Tag)
- if err != nil {
- panic(m.Tag + ", " + err.Error())
- }
- return tags.Tags()
- }
- // IsOptional returns true if tag is optional
- func (m Member) IsOptional() bool {
- if !m.IsBodyMember() {
- return false
- }
- tag := m.Tags()
- for _, item := range tag {
- if item.Key == bodyTagKey {
- if stringx.Contains(item.Options, "optional") {
- return true
- }
- }
- }
- return false
- }
- // IsOmitEmpty returns true if tag contains omitempty
- func (m Member) IsOmitEmpty() bool {
- if !m.IsBodyMember() {
- return false
- }
- tag := m.Tags()
- for _, item := range tag {
- if item.Key == bodyTagKey {
- if stringx.Contains(item.Options, "omitempty") {
- return true
- }
- }
- }
- return false
- }
- // GetPropertyName returns json tag value
- func (m Member) GetPropertyName() (string, error) {
- tags := m.Tags()
- for _, tag := range tags {
- if stringx.Contains(definedKeys, tag.Key) {
- if tag.Name == "-" {
- return util.Untitle(m.Name), nil
- }
- return tag.Name, nil
- }
- }
- return "", errors.New("json property name not exist, member: " + m.Name)
- }
- // GetComment returns comment value of Member
- func (m Member) GetComment() string {
- return strings.TrimSpace(m.Comment)
- }
- // IsBodyMember returns true if contains json tag
- func (m Member) IsBodyMember() bool {
- if m.IsInline {
- return true
- }
- tags := m.Tags()
- for _, tag := range tags {
- if tag.Key == bodyTagKey {
- return true
- }
- }
- return false
- }
- // IsFormMember returns true if contains form tag
- func (m Member) IsFormMember() bool {
- if m.IsInline {
- return false
- }
- tags := m.Tags()
- for _, tag := range tags {
- if tag.Key == formTagKey {
- return true
- }
- }
- return false
- }
- // GetBodyMembers returns all json fields
- func (t DefineStruct) GetBodyMembers() []Member {
- var result []Member
- for _, member := range t.Members {
- if member.IsBodyMember() {
- result = append(result, member)
- }
- }
- return result
- }
- // GetFormMembers returns all form fields
- func (t DefineStruct) GetFormMembers() []Member {
- var result []Member
- for _, member := range t.Members {
- if member.IsFormMember() {
- result = append(result, member)
- }
- }
- return result
- }
- // GetNonBodyMembers returns all have no tag fields
- func (t DefineStruct) GetNonBodyMembers() []Member {
- var result []Member
- for _, member := range t.Members {
- if !member.IsBodyMember() {
- result = append(result, member)
- }
- }
- return result
- }
- // JoinedDoc joins comments and summary value in AtDoc
- func (r Route) JoinedDoc() string {
- doc := r.AtDoc.Text
- if r.AtDoc.Properties != nil {
- doc += r.AtDoc.Properties[defaultSummaryKey]
- }
- doc += strings.Join(r.Docs, " ")
- return strings.TrimSpace(doc)
- }
- // GetAnnotation returns the value by specified key from @server
- func (r Route) GetAnnotation(key string) string {
- if r.AtServerAnnotation.Properties == nil {
- return ""
- }
- return r.AtServerAnnotation.Properties[key]
- }
- // GetAnnotation returns the value by specified key from @server
- func (g Group) GetAnnotation(key string) string {
- if g.Annotation.Properties == nil {
- return ""
- }
- return g.Annotation.Properties[key]
- }
- // ResponseTypeName returns response type name of route
- func (r Route) ResponseTypeName() string {
- if r.ResponseType == nil {
- return ""
- }
- return r.ResponseType.Name()
- }
- // RequestTypeName returns request type name of route
- func (r Route) RequestTypeName() string {
- if r.RequestType == nil {
- return ""
- }
- return r.RequestType.Name()
- }
|