123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package spec
- import (
- "errors"
- "regexp"
- "strings"
- "github.com/tal-tech/go-zero/core/stringx"
- "github.com/tal-tech/go-zero/tools/goctl/util"
- )
- const (
- TagKey = "tag"
- NameKey = "name"
- OptionKey = "option"
- BodyTag = "json"
- )
- var (
- TagRe = regexp.MustCompile(`(?P<tag>\w+):"(?P<name>[^,"]+)[,]?(?P<option>[^"]*)"`)
- TagSubNames = TagRe.SubexpNames()
- definedTags = []string{TagKey, NameKey, OptionKey}
- )
- type Attribute struct {
- Key string
- value string
- }
- func (s Service) Routes() []Route {
- var result []Route
- for _, group := range s.Groups {
- result = append(result, group.Routes...)
- }
- return result
- }
- func (m Member) IsOptional() bool {
- var option string
- matches := TagRe.FindStringSubmatch(m.Tag)
- for i := range matches {
- name := TagSubNames[i]
- if name == OptionKey {
- option = matches[i]
- }
- }
- if len(option) == 0 {
- return false
- }
- fields := strings.Split(option, ",")
- for _, field := range fields {
- if field == "optional" || strings.HasPrefix(field, "default=") {
- return true
- }
- }
- return false
- }
- func (m Member) IsOmitempty() bool {
- var option string
- matches := TagRe.FindStringSubmatch(m.Tag)
- for i := range matches {
- name := TagSubNames[i]
- if name == OptionKey {
- option = matches[i]
- }
- }
- if len(option) == 0 {
- return false
- }
- fields := strings.Split(option, ",")
- for _, field := range fields {
- if field == "omitempty" {
- return true
- }
- }
- return false
- }
- func (m Member) GetAttributes() []Attribute {
- matches := TagRe.FindStringSubmatch(m.Tag)
- var result []Attribute
- for i := range matches {
- name := TagSubNames[i]
- if stringx.Contains(definedTags, name) {
- result = append(result, Attribute{
- Key: name,
- value: matches[i],
- })
- }
- }
- return result
- }
- func (m Member) GetPropertyName() (string, error) {
- attrs := m.GetAttributes()
- for _, attr := range attrs {
- if attr.Key == NameKey && len(attr.value) > 0 {
- if attr.value == "-" {
- return util.Untitle(m.Name), nil
- }
- return attr.value, nil
- }
- }
- return "", errors.New("json property name not exist, member: " + m.Name)
- }
- func (m Member) GetComment() string {
- return strings.TrimSpace(strings.Join(m.Comments, "; "))
- }
- func (m Member) IsBodyMember() bool {
- if m.IsInline {
- return true
- }
- attrs := m.GetAttributes()
- for _, attr := range attrs {
- if attr.value == BodyTag {
- return true
- }
- }
- return false
- }
- func (t Type) GetBodyMembers() []Member {
- var result []Member
- for _, member := range t.Members {
- if member.IsBodyMember() {
- result = append(result, member)
- }
- }
- return result
- }
- func (t Type) GetNonBodyMembers() []Member {
- var result []Member
- for _, member := range t.Members {
- if !member.IsBodyMember() {
- result = append(result, member)
- }
- }
- return result
- }
|