You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/spec/fn.go

198 lines
3.4 KiB
Go

4 years ago
package spec
import (
"errors"
"strings"
"github.com/tal-tech/go-zero/core/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util"
4 years ago
)
const (
bodyTagKey = "json"
formTagKey = "form"
defaultSummaryKey = "summary"
)
4 years ago
var definedKeys = []string{bodyTagKey, formTagKey, "path"}
4 years ago
func (s Service) Routes() []Route {
var result []Route
for _, group := range s.Groups {
result = append(result, group.Routes...)
}
return result
}
func (m Member) Tags() []*Tag {
tags, err := Parse(m.Tag)
if err != nil {
panic(m.Tag + ", " + err.Error())
4 years ago
}
return tags.Tags()
}
func (m Member) IsOptional() bool {
if !m.IsBodyMember() {
4 years ago
return false
}
tag := m.Tags()
for _, item := range tag {
if item.Key == bodyTagKey {
if stringx.Contains(item.Options, "optional") {
return true
}
4 years ago
}
}
return false
}
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
}
4 years ago
func (m Member) IsOmitempty() bool {
if !m.IsBodyMember() {
4 years ago
return false
}
tag := m.Tags()
for _, item := range tag {
if item.Key == bodyTagKey {
if stringx.Contains(item.Options, "omitempty") {
return true
}
4 years ago
}
}
return false
}
func (m Member) GetPropertyName() (string, error) {
tags := m.Tags()
for _, tag := range tags {
if stringx.Contains(definedKeys, tag.Key) {
if tag.Name == "-" {
4 years ago
return util.Untitle(m.Name), nil
}
return tag.Name, nil
4 years ago
}
}
4 years ago
return "", errors.New("json property name not exist, member: " + m.Name)
}
func (m Member) GetComment() string {
return strings.TrimSpace(m.Comment)
4 years ago
}
func (m Member) IsBodyMember() bool {
if m.IsInline {
return true
}
tags := m.Tags()
for _, tag := range tags {
if tag.Key == bodyTagKey {
4 years ago
return true
}
}
return false
}
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
}
func (t DefineStruct) GetBodyMembers() []Member {
4 years ago
var result []Member
for _, member := range t.Members {
if member.IsBodyMember() {
result = append(result, member)
}
}
return result
}
func (t DefineStruct) GetFormMembers() []Member {
var result []Member
for _, member := range t.Members {
if member.IsFormMember() {
result = append(result, member)
}
}
return result
}
func (t DefineStruct) GetNonBodyMembers() []Member {
4 years ago
var result []Member
for _, member := range t.Members {
if !member.IsBodyMember() {
result = append(result, member)
}
}
return result
}
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)
}
func (r Route) GetAnnotation(key string) string {
if r.Annotation.Properties == nil {
return ""
}
return r.Annotation.Properties[key]
}
func (g Group) GetAnnotation(key string) string {
if g.Annotation.Properties == nil {
return ""
}
return g.Annotation.Properties[key]
}
func (r Route) ResponseTypeName() string {
if r.ResponseType == nil {
return ""
}
return r.ResponseType.Name()
}
func (r Route) RequestTypeName() string {
if r.RequestType == nil {
return ""
}
return r.RequestType.Name()
}