Code optimized (#523)
* optimized markdown generator * optimized markdown generator * optimized markdown generator * add more comment * add comment * add comment * add comments for rpc tool * add comments for model tool * add comments for model tool * add comments for model tool * add comments for config tool * add comments for config tool * add comments * add comments * add comments * add comments * add comment * remove rpc main head info * add comment * optimized Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
@@ -16,6 +16,7 @@ const (
|
||||
|
||||
var definedKeys = []string{bodyTagKey, formTagKey, "path"}
|
||||
|
||||
// Routes returns all routes in api service
|
||||
func (s Service) Routes() []Route {
|
||||
var result []Route
|
||||
for _, group := range s.Groups {
|
||||
@@ -24,6 +25,7 @@ func (s Service) Routes() []Route {
|
||||
return result
|
||||
}
|
||||
|
||||
// Tags retuens all tags in Member
|
||||
func (m Member) Tags() []*Tag {
|
||||
tags, err := Parse(m.Tag)
|
||||
if err != nil {
|
||||
@@ -33,6 +35,7 @@ func (m Member) Tags() []*Tag {
|
||||
return tags.Tags()
|
||||
}
|
||||
|
||||
// IsOptional returns true if tag is optional
|
||||
func (m Member) IsOptional() bool {
|
||||
if !m.IsBodyMember() {
|
||||
return false
|
||||
@@ -49,6 +52,7 @@ func (m Member) IsOptional() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsOmitEmpty returns true if tag contains omitempty
|
||||
func (m Member) IsOmitEmpty() bool {
|
||||
if !m.IsBodyMember() {
|
||||
return false
|
||||
@@ -65,22 +69,7 @@ func (m Member) IsOmitEmpty() 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
|
||||
}
|
||||
|
||||
// GetPropertyName returns json tag value
|
||||
func (m Member) GetPropertyName() (string, error) {
|
||||
tags := m.Tags()
|
||||
for _, tag := range tags {
|
||||
@@ -95,10 +84,12 @@ func (m Member) GetPropertyName() (string, error) {
|
||||
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
|
||||
@@ -113,6 +104,7 @@ func (m Member) IsBodyMember() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsFormMember returns true if contains form tag
|
||||
func (m Member) IsFormMember() bool {
|
||||
if m.IsInline {
|
||||
return false
|
||||
@@ -127,6 +119,7 @@ func (m Member) IsFormMember() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetBodyMembers returns all json fields
|
||||
func (t DefineStruct) GetBodyMembers() []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
@@ -137,6 +130,7 @@ func (t DefineStruct) GetBodyMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetFormMembers returns all form fields
|
||||
func (t DefineStruct) GetFormMembers() []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
@@ -147,6 +141,7 @@ func (t DefineStruct) GetFormMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetNonBodyMembers retruns all have no tag fields
|
||||
func (t DefineStruct) GetNonBodyMembers() []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
@@ -157,6 +152,7 @@ func (t DefineStruct) GetNonBodyMembers() []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 {
|
||||
@@ -166,6 +162,7 @@ func (r Route) JoinedDoc() string {
|
||||
return strings.TrimSpace(doc)
|
||||
}
|
||||
|
||||
// GetAnnotation returns the value by specified key
|
||||
func (r Route) GetAnnotation(key string) string {
|
||||
if r.Annotation.Properties == nil {
|
||||
return ""
|
||||
@@ -174,6 +171,7 @@ func (r Route) GetAnnotation(key string) string {
|
||||
return r.Annotation.Properties[key]
|
||||
}
|
||||
|
||||
// GetAnnotation returns the value by specified key
|
||||
func (g Group) GetAnnotation(key string) string {
|
||||
if g.Annotation.Properties == nil {
|
||||
return ""
|
||||
@@ -182,6 +180,7 @@ func (g Group) GetAnnotation(key string) string {
|
||||
return g.Annotation.Properties[key]
|
||||
}
|
||||
|
||||
// ResponseTypeName returns response type name of route
|
||||
func (r Route) ResponseTypeName() string {
|
||||
if r.ResponseType == nil {
|
||||
return ""
|
||||
@@ -190,6 +189,7 @@ func (r Route) ResponseTypeName() string {
|
||||
return r.ResponseType.Name()
|
||||
}
|
||||
|
||||
// RequestTypeName returns request type name of route
|
||||
func (r Route) RequestTypeName() string {
|
||||
if r.RequestType == nil {
|
||||
return ""
|
||||
|
||||
@@ -1,25 +1,31 @@
|
||||
package spec
|
||||
|
||||
// Name returns a basic string, such as int32,int64
|
||||
func (t PrimitiveType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a structure string, such as User
|
||||
func (t DefineStruct) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a map string, such as map[string]int
|
||||
func (t MapType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a slice string, such as []int
|
||||
func (t ArrayType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a pointer string, such as *User
|
||||
func (t PointerType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a interface string, Its fixed value is interface{}
|
||||
func (t InterfaceType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package spec
|
||||
|
||||
type (
|
||||
// Doc describes document
|
||||
Doc []string
|
||||
|
||||
// Annotation defines key-value
|
||||
Annotation struct {
|
||||
Properties map[string]string
|
||||
}
|
||||
|
||||
// ApiSyntax describes the syntax grammar
|
||||
ApiSyntax struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
// ApiSpec describes a api file
|
||||
ApiSpec struct {
|
||||
Info Info
|
||||
Syntax ApiSyntax
|
||||
@@ -19,15 +23,18 @@ type (
|
||||
Service Service
|
||||
}
|
||||
|
||||
// Import describes api import
|
||||
Import struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
// Group defines a set of routing information
|
||||
Group struct {
|
||||
Annotation Annotation
|
||||
Routes []Route
|
||||
}
|
||||
|
||||
// Info describes info grammar block
|
||||
Info struct {
|
||||
// Deprecated: use Properties instead
|
||||
Title string
|
||||
@@ -42,6 +49,7 @@ type (
|
||||
Properties map[string]string
|
||||
}
|
||||
|
||||
// Member describes the field of a structure
|
||||
Member struct {
|
||||
Name string
|
||||
// 数据类型字面值,如:string、map[int]string、[]int64、[]*User
|
||||
@@ -53,6 +61,7 @@ type (
|
||||
IsInline bool
|
||||
}
|
||||
|
||||
// Route describes api route
|
||||
Route struct {
|
||||
Annotation Annotation
|
||||
Method string
|
||||
@@ -64,26 +73,30 @@ type (
|
||||
AtDoc AtDoc
|
||||
}
|
||||
|
||||
// Service describes api service
|
||||
Service struct {
|
||||
Name string
|
||||
Groups []Group
|
||||
}
|
||||
|
||||
// Type defines api type
|
||||
Type interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
// DefineStruct describes api structure
|
||||
DefineStruct struct {
|
||||
RawName string
|
||||
Members []Member
|
||||
Docs Doc
|
||||
}
|
||||
|
||||
// 系统预设基本数据类型 bool int32 int64 float32
|
||||
// PrimitiveType describes the basic golang type, such as bool,int32,int64, ...
|
||||
PrimitiveType struct {
|
||||
RawName string
|
||||
}
|
||||
|
||||
// MapType describes a map for api
|
||||
MapType struct {
|
||||
RawName string
|
||||
// only support the PrimitiveType
|
||||
@@ -97,20 +110,24 @@ type (
|
||||
Value Type
|
||||
}
|
||||
|
||||
// ArrayType describes a slice for api
|
||||
ArrayType struct {
|
||||
RawName string
|
||||
Value Type
|
||||
}
|
||||
|
||||
// InterfaceType describes a interface for api
|
||||
InterfaceType struct {
|
||||
RawName string
|
||||
}
|
||||
|
||||
// PointerType describes a pointer for api
|
||||
PointerType struct {
|
||||
RawName string
|
||||
Type Type
|
||||
}
|
||||
|
||||
// AtDoc describes a metadata for api grammar: @doc(...)
|
||||
AtDoc struct {
|
||||
Properties map[string]string
|
||||
Text string
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
var errTagNotExist = errors.New("tag does not exist")
|
||||
|
||||
type (
|
||||
// Tag defines a tag for structure filed
|
||||
Tag struct {
|
||||
// Key is the tag key, such as json, xml, etc..
|
||||
// i.e: `json:"foo,omitempty". Here key is: "json"
|
||||
@@ -24,11 +25,13 @@ type (
|
||||
Options []string
|
||||
}
|
||||
|
||||
// Tags defines a slice for Tag
|
||||
Tags struct {
|
||||
tags []*Tag
|
||||
}
|
||||
)
|
||||
|
||||
// Parse converts tag string into Tag
|
||||
func Parse(tag string) (*Tags, error) {
|
||||
tag = strings.TrimPrefix(tag, "`")
|
||||
tag = strings.TrimSuffix(tag, "`")
|
||||
@@ -44,6 +47,7 @@ func Parse(tag string) (*Tags, error) {
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// Get gets tag value by specified key
|
||||
func (t *Tags) Get(key string) (*Tag, error) {
|
||||
for _, tag := range t.tags {
|
||||
if tag.Key == key {
|
||||
@@ -54,6 +58,7 @@ func (t *Tags) Get(key string) (*Tag, error) {
|
||||
return nil, errTagNotExist
|
||||
}
|
||||
|
||||
// Keys returns all keys in Tags
|
||||
func (t *Tags) Keys() []string {
|
||||
var keys []string
|
||||
for _, tag := range t.tags {
|
||||
@@ -62,6 +67,7 @@ func (t *Tags) Keys() []string {
|
||||
return keys
|
||||
}
|
||||
|
||||
// Tags returns all tags in Tags
|
||||
func (t *Tags) Tags() []*Tag {
|
||||
return t.tags
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user