Skip to content

Commit

Permalink
move common used tag from packages to package gtag for maintainability (
Browse files Browse the repository at this point in the history
#2256)

* move common used tag from packages to package gtag for maintainability

* move common used tag from packages to package gtag for maintainability

Co-authored-by: houseme <housemecn@gmail.com>
  • Loading branch information
gqcn and houseme authored Nov 7, 2022
1 parent 033ba58 commit ee58255
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 176 deletions.
3 changes: 2 additions & 1 deletion contrib/drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gtag"
"github.com/gogf/gf/v2/util/gutil"
"github.com/google/uuid"
"github.com/shopspring/decimal"
Expand All @@ -49,7 +50,7 @@ const (
filterTypePattern = `(?i)^UPDATE|DELETE`
replaceSchemaPattern = `@(.+?)/([\w\.\-]+)+`
needParsedSqlInCtx gctx.StrKey = "NeedParsedSql"
OrmTagForStruct = "orm"
OrmTagForStruct = gtag.ORM
driverName = "clickhouse"
)

Expand Down
10 changes: 5 additions & 5 deletions net/gclient/gclient_request_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/net/goai"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gmeta"
"github.com/gogf/gf/v2/util/gtag"
"github.com/gogf/gf/v2/util/gutil"
)

Expand All @@ -41,21 +41,21 @@ import (
// DoRequestObj(ctx, req, &res)
func (c *Client) DoRequestObj(ctx context.Context, req, res interface{}) error {
var (
method = gmeta.Get(req, goai.TagNameMethod).String()
path = gmeta.Get(req, goai.TagNamePath).String()
method = gmeta.Get(req, gtag.Method).String()
path = gmeta.Get(req, gtag.Path).String()
)
if method == "" {
return gerror.NewCodef(
gcode.CodeInvalidParameter,
`no "%s" tag found in request object: %s`,
goai.TagNameMethod, reflect.TypeOf(req).String(),
gtag.Method, reflect.TypeOf(req).String(),
)
}
if path == "" {
return gerror.NewCodef(
gcode.CodeInvalidParameter,
`no "%s" tag found in request object: %s`,
goai.TagNamePath, reflect.TypeOf(req).String(),
gtag.Path, reflect.TypeOf(req).String(),
)
}
path = c.handlePathForObjRequest(path, req)
Expand Down
3 changes: 2 additions & 1 deletion net/ghttp/ghttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"reflect"
"time"

"github.com/gogf/gf/v2/util/gtag"
"github.com/gorilla/websocket"

"github.com/gogf/gf/v2/container/gmap"
Expand Down Expand Up @@ -180,7 +181,7 @@ var (
gracefulEnabled = false

// defaultValueTags are the struct tag names for default value storing.
defaultValueTags = []string{"d", "default"}
defaultValueTags = []string{gtag.DefaultShort, gtag.Default}
)

var (
Expand Down
8 changes: 4 additions & 4 deletions net/ghttp/ghttp_server_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/net/goai"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gmeta"
"github.com/gogf/gf/v2/util/gtag"
)

var (
Expand Down Expand Up @@ -96,13 +96,13 @@ func (s *Server) setHandler(ctx context.Context, in setHandlerInput) {
// Change the registered route according to meta info from its request structure.
if handler.Info.Type != nil && handler.Info.Type.NumIn() == 2 {
var objectReq = reflect.New(handler.Info.Type.In(1))
if v := gmeta.Get(objectReq, goai.TagNamePath); !v.IsEmpty() {
if v := gmeta.Get(objectReq, gtag.Path); !v.IsEmpty() {
uri = v.String()
}
if v := gmeta.Get(objectReq, goai.TagNameMethod); !v.IsEmpty() {
if v := gmeta.Get(objectReq, gtag.Method); !v.IsEmpty() {
method = v.String()
}
if v := gmeta.Get(objectReq, goai.TagNameDomain); !v.IsEmpty() {
if v := gmeta.Get(objectReq, gtag.Domain); !v.IsEmpty() {
domain = v.String()
}
}
Expand Down
40 changes: 10 additions & 30 deletions net/goai/goai.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gtag"
)

// OpenApiV3 is the structure defined from:
Expand All @@ -36,18 +37,6 @@ type OpenApiV3 struct {
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
}

const (
HttpMethodGet = `GET`
HttpMethodPut = `PUT`
HttpMethodPost = `POST`
HttpMethodDelete = `DELETE`
HttpMethodConnect = `CONNECT`
HttpMethodHead = `HEAD`
HttpMethodOptions = `OPTIONS`
HttpMethodPatch = `PATCH`
HttpMethodTrace = `TRACE`
)

const (
TypeInteger = `integer`
TypeNumber = `number`
Expand All @@ -72,15 +61,6 @@ const (
ParameterInCookie = `cookie`
)

const (
TagNamePath = `path`
TagNameMethod = `method`
TagNameMime = `mime`
TagNameConsumes = `consumes`
TagNameType = `type`
TagNameDomain = `domain`
)

const (
validationRuleKeyForRequired = `required`
validationRuleKeyForIn = `in:`
Expand All @@ -90,14 +70,14 @@ var (
defaultReadContentTypes = []string{`application/json`}
defaultWriteContentTypes = []string{`application/json`}
shortTypeMapForTag = map[string]string{
"d": "Default",
"sum": "Summary",
"sm": "Summary",
"des": "Description",
"dc": "Description",
"eg": "Example",
"egs": "Examples",
"ed": "ExternalDocs",
gtag.DefaultShort: gtag.Default,
gtag.SummaryShort: gtag.Summary,
gtag.SummaryShort2: gtag.Summary,
gtag.DescriptionShort: gtag.Description,
gtag.DescriptionShort2: gtag.Description,
gtag.ExampleShort: gtag.Example,
gtag.ExamplesShort: gtag.Examples,
gtag.ExternalDocsShort: gtag.ExternalDocs,
}
)

Expand Down Expand Up @@ -231,7 +211,7 @@ func (oai *OpenApiV3) golangTypeToSchemaName(t reflect.Type) string {
return schemaName
}

func (oai *OpenApiV3) fileMapWithShortTags(m map[string]string) map[string]string {
func (oai *OpenApiV3) fillMapWithShortTags(m map[string]string) map[string]string {
for k, v := range shortTypeMapForTag {
if m[v] == "" && m[k] != "" {
m[v] = m[k]
Expand Down
2 changes: 1 addition & 1 deletion net/goai/goai_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Operation struct {
}

func (oai *OpenApiV3) tagMapToOperation(tagMap map[string]string, operation *Operation) error {
var mergedTagMap = oai.fileMapWithShortTags(tagMap)
var mergedTagMap = oai.fillMapWithShortTags(tagMap)
if err := gconv.Struct(mergedTagMap, operation); err != nil {
return gerror.Wrap(err, `mapping struct tags to Operation failed`)
}
Expand Down
2 changes: 1 addition & 1 deletion net/goai/goai_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Parameter struct {
}

func (oai *OpenApiV3) tagMapToParameter(tagMap map[string]string, parameter *Parameter) error {
var mergedTagMap = oai.fileMapWithShortTags(tagMap)
var mergedTagMap = oai.fillMapWithShortTags(tagMap)
if err := gconv.Struct(mergedTagMap, parameter); err != nil {
return gerror.Wrap(err, `mapping struct tags to Parameter failed`)
}
Expand Down
3 changes: 2 additions & 1 deletion net/goai/goai_parameter_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package goai

import (
"fmt"
"net/http"

"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/errors/gcode"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (oai *OpenApiV3) newParameterRefWithStructMethod(field gstructs.Field, path
} else {
// Default the parameter input to "query" if method is "GET/DELETE".
switch gstr.ToUpper(method) {
case HttpMethodGet, HttpMethodDelete:
case http.MethodGet, http.MethodDelete:
parameter.In = ParameterInQuery

default:
Expand Down
38 changes: 20 additions & 18 deletions net/goai/goai_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package goai

import (
"net/http"
"reflect"

"github.com/gogf/gf/v2/container/garray"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gmeta"
"github.com/gogf/gf/v2/util/gtag"
)

type Path struct {
Expand Down Expand Up @@ -97,7 +99,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
)
// Path check.
if in.Path == "" {
in.Path = gmeta.Get(inputObject.Interface(), TagNamePath).String()
in.Path = gmeta.Get(inputObject.Interface(), gtag.Path).String()
if in.Prefix != "" {
in.Path = gstr.TrimRight(in.Prefix, "/") + "/" + gstr.TrimLeft(in.Path, "/")
}
Expand All @@ -106,7 +108,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
return gerror.NewCodef(
gcode.CodeMissingParameter,
`missing necessary path parameter "%s" for input struct "%s", missing tag in attribute Meta?`,
TagNamePath, inputStructTypeName,
gtag.Path, inputStructTypeName,
)
}

Expand All @@ -116,13 +118,13 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {

// Method check.
if in.Method == "" {
in.Method = gmeta.Get(inputObject.Interface(), TagNameMethod).String()
in.Method = gmeta.Get(inputObject.Interface(), gtag.Method).String()
}
if in.Method == "" {
return gerror.NewCodef(
gcode.CodeMissingParameter,
`missing necessary method parameter "%s" for input struct "%s", missing tag in attribute Meta?`,
TagNameMethod, inputStructTypeName,
gtag.Method, inputStructTypeName,
)
}

Expand All @@ -138,8 +140,8 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
return err
}
// Allowed request mime.
if mime = inputMetaMap[TagNameMime]; mime == "" {
mime = inputMetaMap[TagNameConsumes]
if mime = inputMetaMap[gtag.Mime]; mime == "" {
mime = inputMetaMap[gtag.Consumes]
}
}

Expand Down Expand Up @@ -179,7 +181,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
// Supported mime types of request.
var (
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(inputObject.Interface(), TagNameMime).String()
tagMimeValue = gmeta.Get(inputObject.Interface(), gtag.Mime).String()
)
if tagMimeValue != "" {
contentTypes = gstr.SplitAndTrim(tagMimeValue, ",")
Expand Down Expand Up @@ -224,7 +226,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
// Supported mime types of response.
var (
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(outputObject.Interface(), TagNameMime).String()
tagMimeValue = gmeta.Get(outputObject.Interface(), gtag.Mime).String()
refInput = getResponseSchemaRefInput{
BusinessStructName: outputStructTypeName,
CommonResponseObject: oai.Config.CommonResponse,
Expand Down Expand Up @@ -256,35 +258,35 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {

// Assign to certain operation attribute.
switch gstr.ToUpper(in.Method) {
case HttpMethodGet:
case http.MethodGet:
// GET operations cannot have a requestBody.
operation.RequestBody = nil
path.Get = &operation

case HttpMethodPut:
case http.MethodPut:
path.Put = &operation

case HttpMethodPost:
case http.MethodPost:
path.Post = &operation

case HttpMethodDelete:
case http.MethodDelete:
// DELETE operations cannot have a requestBody.
operation.RequestBody = nil
path.Delete = &operation

case HttpMethodConnect:
case http.MethodConnect:
// Nothing to do for Connect.

case HttpMethodHead:
case http.MethodHead:
path.Head = &operation

case HttpMethodOptions:
case http.MethodOptions:
path.Options = &operation

case HttpMethodPatch:
case http.MethodPatch:
path.Patch = &operation

case HttpMethodTrace:
case http.MethodTrace:
path.Trace = &operation

default:
Expand Down Expand Up @@ -354,7 +356,7 @@ func (oai *OpenApiV3) doesStructHasNoFields(s interface{}) bool {
}

func (oai *OpenApiV3) tagMapToPath(tagMap map[string]string, path *Path) error {
var mergedTagMap = oai.fileMapWithShortTags(tagMap)
var mergedTagMap = oai.fillMapWithShortTags(tagMap)
if err := gconv.Struct(mergedTagMap, path); err != nil {
return gerror.Wrap(err, `mapping struct tags to Path failed`)
}
Expand Down
2 changes: 1 addition & 1 deletion net/goai/goai_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Response struct {
}

func (oai *OpenApiV3) tagMapToResponse(tagMap map[string]string, response *Response) error {
var mergedTagMap = oai.fileMapWithShortTags(tagMap)
var mergedTagMap = oai.fillMapWithShortTags(tagMap)
if err := gconv.Struct(mergedTagMap, response); err != nil {
return gerror.Wrap(err, `mapping struct tags to Response failed`)
}
Expand Down
2 changes: 1 addition & 1 deletion net/goai/goai_shema.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
}

func (oai *OpenApiV3) tagMapToSchema(tagMap map[string]string, schema *Schema) error {
var mergedTagMap = oai.fileMapWithShortTags(tagMap)
var mergedTagMap = oai.fillMapWithShortTags(tagMap)
if err := gconv.Struct(mergedTagMap, schema); err != nil {
return gerror.Wrap(err, `mapping struct tags to Schema failed`)
}
Expand Down
Loading

0 comments on commit ee58255

Please sign in to comment.