goctl added

This commit is contained in:
kim
2020-07-29 17:11:41 +08:00
parent b1975d29a7
commit 121323b8c3
142 changed files with 10690 additions and 0 deletions

143
tools/goctl/api/spec/fn.go Normal file
View File

@@ -0,0 +1,143 @@
package spec
import (
"errors"
"regexp"
"strings"
"zero/core/stringx"
"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 (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
}

View File

@@ -0,0 +1,131 @@
package spec
type (
Annotation struct {
Name string
Properties map[string]string
}
ApiSpec struct {
Info Info
Types []Type
Service Service
}
Group struct {
Annotations []Annotation
Routes []Route
}
Info struct {
Title string
Desc string
Version string
Author string
Email string
}
Member struct {
Annotations []Annotation
Name string
// 数据类型字面值string、map[int]string、[]int64、[]*User
Type string
// it can be asserted as BasicType: int、bool、
// PointerType: *string、*User、
// MapType: map[${BasicType}]interface、
// ArrayType:[]int、[]User、[]*User
// InterfaceType: interface{}
// Type
Expr interface{}
Tag string
// Deprecated
Comment string // 换成标准struct中将废弃
// 成员尾部注释说明
Comments []string
// 成员头顶注释说明
Docs []string
IsInline bool
}
Route struct {
Annotations []Annotation
Method string
Path string
RequestType Type
ResponseType Type
}
Service struct {
Name string
Annotations []Annotation
Routes []Route
Groups []Group
}
Type struct {
Name string
Annotations []Annotation
Members []Member
}
// 系统预设基本数据类型
BasicType struct {
StringExpr string
Name string
}
PointerType struct {
StringExpr string
// it can be asserted as BasicType: int、bool、
// PointerType: *string、*User、
// MapType: map[${BasicType}]interface、
// ArrayType:[]int、[]User、[]*User
// InterfaceType: interface{}
// Type
Star interface{}
}
MapType struct {
StringExpr string
// only support the BasicType
Key string
// it can be asserted as BasicType: int、bool、
// PointerType: *string、*User、
// MapType: map[${BasicType}]interface、
// ArrayType:[]int、[]User、[]*User
// InterfaceType: interface{}
// Type
Value interface{}
}
ArrayType struct {
StringExpr string
// it can be asserted as BasicType: int、bool、
// PointerType: *string、*User、
// MapType: map[${BasicType}]interface、
// ArrayType:[]int、[]User、[]*User
// InterfaceType: interface{}
// Type
ArrayType interface{}
}
InterfaceType struct {
StringExpr string
// do nothing,just for assert
}
TimeType struct {
StringExpr string
}
StructType struct {
StringExpr string
}
)
func (spec *ApiSpec) ContainsTime() bool {
for _, item := range spec.Types {
members := item.Members
for _, member := range members {
if _, ok := member.Expr.(*TimeType); ok {
return true
}
}
}
return false
}