feature: refactor api parse to g4 (#365)
* feature: refactor api parse to g4 * new g4 parser * add CHANGE_LOG.MD * refactor * fix byte bug * refactor * optimized * optimized * revert * update readme.md * update readme.md * update readme.md * update readme.md * remove no need * fix java gen * add upgrade * resolve confilits Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
@@ -9,8 +9,9 @@ import (
|
||||
)
|
||||
|
||||
const bodyTagKey = "json"
|
||||
const formTagKey = "form"
|
||||
|
||||
var definedKeys = []string{"json", "form", "path"}
|
||||
var definedKeys = []string{bodyTagKey, formTagKey, "path"}
|
||||
|
||||
func (s Service) Routes() []Route {
|
||||
var result []Route
|
||||
@@ -45,6 +46,22 @@ func (m Member) IsOptional() bool {
|
||||
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
|
||||
}
|
||||
|
||||
func (m Member) IsOmitempty() bool {
|
||||
if !m.IsBodyMember() {
|
||||
return false
|
||||
@@ -76,7 +93,7 @@ func (m Member) GetPropertyName() (string, error) {
|
||||
}
|
||||
|
||||
func (m Member) GetComment() string {
|
||||
return strings.TrimSpace(strings.Join(m.Comments, "; "))
|
||||
return strings.TrimSpace(m.Comment)
|
||||
}
|
||||
|
||||
func (m Member) IsBodyMember() bool {
|
||||
@@ -93,7 +110,21 @@ func (m Member) IsBodyMember() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t Type) GetBodyMembers() []Member {
|
||||
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 {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
if member.IsBodyMember() {
|
||||
@@ -103,7 +134,17 @@ func (t Type) GetBodyMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
func (t Type) GetNonBodyMembers() []Member {
|
||||
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 {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
if !member.IsBodyMember() {
|
||||
@@ -112,3 +153,37 @@ func (t Type) GetNonBodyMembers() []Member {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (r Route) JoinedDoc() string {
|
||||
return strings.Join(r.Docs, " ")
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
25
tools/goctl/api/spec/name.go
Normal file
25
tools/goctl/api/spec/name.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package spec
|
||||
|
||||
func (t PrimitiveType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
func (t DefineStruct) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
func (t MapType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
func (t ArrayType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
func (t PointerType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
func (t InterfaceType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
@@ -1,59 +1,66 @@
|
||||
package spec
|
||||
|
||||
type (
|
||||
Doc []string
|
||||
|
||||
Annotation struct {
|
||||
Name string
|
||||
Properties map[string]string
|
||||
Value string
|
||||
}
|
||||
|
||||
ApiSyntax struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
ApiSpec struct {
|
||||
Info Info
|
||||
Syntax ApiSyntax
|
||||
Imports []Import
|
||||
Types []Type
|
||||
Service Service
|
||||
}
|
||||
|
||||
Import struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
Group struct {
|
||||
Annotations []Annotation
|
||||
Routes []Route
|
||||
Annotation Annotation
|
||||
Routes []Route
|
||||
}
|
||||
|
||||
Info struct {
|
||||
Title string
|
||||
Desc string
|
||||
// Deprecated: use Properties instead
|
||||
Title string
|
||||
// Deprecated: use Properties instead
|
||||
Desc string
|
||||
// Deprecated: use Properties instead
|
||||
Version string
|
||||
Author string
|
||||
Email string
|
||||
// Deprecated: use Properties instead
|
||||
Author string
|
||||
// Deprecated: use Properties instead
|
||||
Email string
|
||||
Properties map[string]string
|
||||
}
|
||||
|
||||
Member struct {
|
||||
Annotations []Annotation
|
||||
Name string
|
||||
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
|
||||
Type Type
|
||||
Tag string
|
||||
Comment string
|
||||
// 成员头顶注释说明
|
||||
Docs []string
|
||||
Docs Doc
|
||||
IsInline bool
|
||||
}
|
||||
|
||||
Route struct {
|
||||
Annotations []Annotation
|
||||
Annotation Annotation
|
||||
Method string
|
||||
Path string
|
||||
RequestType Type
|
||||
ResponseType Type
|
||||
Docs Doc
|
||||
Handler string
|
||||
}
|
||||
|
||||
Service struct {
|
||||
@@ -61,71 +68,45 @@ type (
|
||||
Groups []Group
|
||||
}
|
||||
|
||||
Type struct {
|
||||
Name string
|
||||
Annotations []Annotation
|
||||
Members []Member
|
||||
Type interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
// 系统预设基本数据类型
|
||||
BasicType struct {
|
||||
StringExpr string
|
||||
Name string
|
||||
DefineStruct struct {
|
||||
RawName string
|
||||
Members []Member
|
||||
Docs Doc
|
||||
}
|
||||
|
||||
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{}
|
||||
// 系统预设基本数据类型 bool int32 int64 float32
|
||||
PrimitiveType struct {
|
||||
RawName string
|
||||
}
|
||||
|
||||
MapType struct {
|
||||
StringExpr string
|
||||
// only support the BasicType
|
||||
RawName string
|
||||
// only support the PrimitiveType
|
||||
Key string
|
||||
// it can be asserted as BasicType: int、bool、
|
||||
// it can be asserted as PrimitiveType: int、bool、
|
||||
// PointerType: *string、*User、
|
||||
// MapType: map[${BasicType}]interface、
|
||||
// MapType: map[${PrimitiveType}]interface、
|
||||
// ArrayType:[]int、[]User、[]*User
|
||||
// InterfaceType: interface{}
|
||||
// Type
|
||||
Value interface{}
|
||||
Value Type
|
||||
}
|
||||
|
||||
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{}
|
||||
RawName string
|
||||
Value Type
|
||||
}
|
||||
|
||||
InterfaceType struct {
|
||||
StringExpr string
|
||||
// do nothing,just for assert
|
||||
RawName string
|
||||
}
|
||||
TimeType struct {
|
||||
StringExpr string
|
||||
}
|
||||
StructType struct {
|
||||
StringExpr string
|
||||
|
||||
PointerType struct {
|
||||
RawName string
|
||||
Type Type
|
||||
}
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user