Browse Source

修改bug并添加一些文档

yjp 1 year ago
parent
commit
354cb23dbc

+ 29 - 0
framework/core/api/api.go

@@ -103,6 +103,10 @@ func NewWithEngine(server *http.Server, engine *gin.Engine, opts ...Option) *Api
 	return api
 }
 
+// Start 运行Api
+// 参数: 无
+// 返回值:
+// - 错误
 func (api *Api) Start() error {
 	err := api.server.ListenAndServe()
 	if err != nil && !errors.Is(err, http.ErrServerClosed) {
@@ -112,6 +116,10 @@ func (api *Api) Start() error {
 	return nil
 }
 
+// Finish 终止Api
+// 参数: 无
+// 返回值:
+// - 错误
 func (api *Api) Finish() error {
 	err := api.server.Shutdown(context.Background())
 	if err != nil {
@@ -121,14 +129,26 @@ func (api *Api) Finish() error {
 	return nil
 }
 
+// Options 获取Api的选项
+// 参数: 无
+// 返回值:
+// - Api的选项
 func (api *Api) Options() Options {
 	return api.options
 }
 
+// RootRouter 获取Api的根路由
+// 参数: 无
+// 返回值:
+// - Api的根路由
 func (api *Api) RootRouter() Router {
 	return api.rootRouter
 }
 
+// PrefixRouter 获取Api的前缀路由
+// 参数: 无
+// 返回值:
+// - Api的前缀路由
 func (api *Api) PrefixRouter() Router {
 	if api.prefixRouter == nil {
 		return api.rootRouter
@@ -142,6 +162,15 @@ const (
 	RouterPrefix = "prefix"
 )
 
+// ChooseRouter 选择Router
+// 参数:
+// - routerType: 路由类型,有两种
+//   - api.RouterRoot: 根路由
+//   - api.RouterPrefix: 带url前缀的路由
+//
+// - version: 版本,用于获取基于上面两中路由够造的带有版本号的路由,传空字符串则选定上面两种路由本身
+// 返回值:
+// - 选择的Router
 func (api *Api) ChooseRouter(routerType string, version string) Router {
 	var router Router
 

+ 258 - 129
framework/core/api/context.go

@@ -22,7 +22,13 @@ type Context struct {
 	*gin.Context
 }
 
-// GetFileHeaderBytes 获取传递的文件名和文件内容
+// GetFileHeaderBytes 获取Multipart中传递的文件名和文件内容
+// 参数:
+// - fileHeader: Multipart的文件头
+// 返回值:
+// - 文件名
+// - 文件字节
+// - 错误
 func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
 	file, err := fileHeader.Open()
 	if err != nil {
@@ -45,24 +51,10 @@ func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string,
 	return fileHeader.Filename, contentBytes, nil
 }
 
-type Header struct {
-	c      *Context
-	header map[string]string
-}
-
-func (header *Header) Set(key string, value string) {
-	header.header[key] = value
-	header.c.Set(headerKey, header.header)
-}
-
-func (header *Header) Get(key string) string {
-	return header.header[key]
-}
-
-func (header *Header) Map() map[string]string {
-	return header.header
-}
-
+// GetHeader 获取上下文中的Header
+// 参数: 无
+// 返回值:
+// - 上下文中的Header
 func (c *Context) GetHeader() *Header {
 	savedHeader, exist := c.Get(headerKey)
 	if exist {
@@ -86,41 +78,12 @@ func (c *Context) GetHeader() *Header {
 	}
 }
 
-type CacheBody struct {
-	c         *Context
-	bytesBody []byte
-}
-
-func (cacheBody *CacheBody) Set(bytesBody []byte) {
-	cacheBody.bytesBody = bytesBody
-	cacheBody.c.Set(bodyKey, cacheBody.bytesBody)
-}
-
-func (cacheBody *CacheBody) Bytes() []byte {
-	return cacheBody.bytesBody
-}
-
-func (cacheBody *CacheBody) Marshal(output any) error {
-	bytesBody, err := json.Marshal(output)
-	if err != nil {
-		return errors.New(err.Error())
-	}
-
-	cacheBody.Set(bytesBody)
-
-	return nil
-}
-
-func (cacheBody *CacheBody) Unmarshal(output any) error {
-	err := json.Unmarshal(cacheBody.Bytes(), output)
-	if err != nil {
-		return errors.New(err.Error())
-	}
-
-	return nil
-}
-
-func (c *Context) GetBytesBody() (*CacheBody, error) {
+// GetBytesBody 获取上下文中的字节Body
+// 参数: 无
+// 返回值:
+// - 上下文中的字节Body
+// - 错误
+func (c *Context) GetBytesBody() (*BytesBody, error) {
 	body, exist := c.Get(bodyKey)
 	if !exist {
 		bytesBody, err := c.readOriginBody()
@@ -128,7 +91,9 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
 			return nil, err
 		}
 
-		return &CacheBody{
+		c.Set(bodyKey, bytesBody)
+
+		return &BytesBody{
 			c:         c,
 			bytesBody: bytesBody,
 		}, nil
@@ -136,7 +101,7 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
 
 	switch b := body.(type) {
 	case []byte:
-		return &CacheBody{
+		return &BytesBody{
 			c:         c,
 			bytesBody: b,
 		}, nil
@@ -146,7 +111,7 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
 			return nil, errors.New(err.Error())
 		}
 
-		return &CacheBody{
+		return &BytesBody{
 			c:         c,
 			bytesBody: bytesBody,
 		}, nil
@@ -155,29 +120,224 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
 	}
 }
 
+// GetJsonBody 获取上下文中的JsonBody
+// 参数: 无
+// 返回值:
+// - 上下文中的JsonBody
+// - 错误
+func (c *Context) GetJsonBody() (*JsonBody, error) {
+	body, exist := c.Get(bodyKey)
+	if !exist {
+		bytesBody, err := c.readOriginBody()
+		if err != nil {
+			return nil, err
+		}
+
+		jsonBodyMap := make(map[string]any)
+		err = json.Unmarshal(bytesBody, &jsonBodyMap)
+		if err != nil {
+			return nil, errors.New(err.Error())
+		}
+
+		c.Set(bodyKey, jsonBodyMap)
+
+		return &JsonBody{
+			c:           c,
+			jsonBodyMap: jsonBodyMap,
+		}, nil
+	}
+
+	switch b := body.(type) {
+	case []byte:
+		jsonBodyMap := make(map[string]any)
+		err := json.Unmarshal(b, &jsonBodyMap)
+		if err != nil {
+			return nil, errors.New(err.Error())
+		}
+
+		return &JsonBody{
+			c:           c,
+			jsonBodyMap: jsonBodyMap,
+		}, nil
+	case map[string]any:
+		return &JsonBody{
+			c:           c,
+			jsonBodyMap: body.(map[string]any),
+		}, nil
+	default:
+		return nil, errors.New("不支持的body类型")
+	}
+}
+
+// GetQueryParams 获取上下文中的查询参数
+// 参数: 无
+// 返回值:
+// - 上下文中的查询参数
+// - 错误
+func (c *Context) GetQueryParams() *QueryPrams {
+	queryParams, exist := c.Get(queryParamsKey)
+	if !exist {
+		return &QueryPrams{
+			c:           c,
+			queryParams: c.getAllQueryParams(),
+		}
+	}
+
+	return &QueryPrams{
+		c:           c,
+		queryParams: queryParams.(map[string]string),
+	}
+}
+
+// GetPathParams 获取上下文中的路径参数
+// 参数: 无
+// 返回值:
+// - 上下文中的路径参数
+// - 错误
+func (c *Context) GetPathParams() *PathPrams {
+	pathParams, exist := c.Get(pathParamsKey)
+	if !exist {
+		return &PathPrams{
+			c:          c,
+			pathParams: c.getAllPathParams(),
+		}
+	}
+
+	return &PathPrams{
+		c:          c,
+		pathParams: pathParams.(map[string]string),
+	}
+}
+
+type Header struct {
+	c      *Context
+	header map[string]string
+}
+
+// Set 设置Header
+// 参数:
+// - key: Header的键
+// - value: Header对应键的值
+// 返回值: 无
+func (header *Header) Set(key string, value string) {
+	header.header[key] = value
+	header.c.Set(headerKey, header.header)
+}
+
+// Get 获取Header对应键的值
+// 参数:
+// - key: Header的键
+// 返回值:
+// - Header对应键的值
+func (header *Header) Get(key string) string {
+	return header.header[key]
+}
+
+// Map 获取Header的map表示
+// 参数: 无
+// 返回值:
+// - Header的map表示
+func (header *Header) Map() map[string]string {
+	return header.header
+}
+
+type BytesBody struct {
+	c         *Context
+	bytesBody []byte
+}
+
+// Set 设置字节body
+// 参数:
+// - body: 字节body的内容
+// 返回值: 无
+func (bytesBody *BytesBody) Set(body []byte) {
+	bytesBody.bytesBody = body
+	bytesBody.c.Set(bodyKey, bytesBody.bytesBody)
+}
+
+// Bytes 获取字节body的内容
+// 参数: 无
+// 返回值:
+// - 字节body的内容
+func (bytesBody *BytesBody) Bytes() []byte {
+	return bytesBody.bytesBody
+}
+
+// Marshal 将input Marshal到字节body
+// 参数:
+// - input: 输入,一般为结构或map[string]any
+// 返回值:
+// - 错误
+func (bytesBody *BytesBody) Marshal(input any) error {
+	jsonBytesBody, err := json.Marshal(input)
+	if err != nil {
+		return errors.New(err.Error())
+	}
+
+	bytesBody.Set(jsonBytesBody)
+
+	return nil
+}
+
+// Unmarshal 将字节body的内容Unmarshal到output
+// 参数:
+// - output: 输出,一般为结构指针或map[string]any指针
+// 返回值:
+// - 错误
+func (bytesBody *BytesBody) Unmarshal(output any) error {
+	err := json.Unmarshal(bytesBody.Bytes(), output)
+	if err != nil {
+		return errors.New(err.Error())
+	}
+
+	return nil
+}
+
 type JsonBody struct {
 	c           *Context
 	jsonBodyMap map[string]any
 }
 
+// Set 设置JsonBody键对应的值
+// 参数:
+// - key: JsonBody的键
+// - value: JsonBody对应键的值
+// 返回值: 无
 func (jsonBody *JsonBody) Set(key string, value any) {
 	jsonBody.jsonBodyMap[key] = value
 	jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
 }
 
+// Delete 删除JsonBody键对应的值
+// 参数:
+// - key: JsonBody的键
+// 返回值: 无
 func (jsonBody *JsonBody) Delete(key string) {
 	delete(jsonBody.jsonBodyMap, key)
 	jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
 }
 
+// Get 获取JsonBody对应键的值
+// 参数:
+// - key: JsonBody的键
+// 返回值:
+// - JsonBody对应键的值
 func (jsonBody *JsonBody) Get(key string) any {
 	return jsonBody.jsonBodyMap[key]
 }
 
+// Map 获取JsonBody的map表示
+// 参数: 无
+// 返回值:
+// - JsonBody的map表示
 func (jsonBody *JsonBody) Map() map[string]any {
 	return jsonBody.jsonBodyMap
 }
 
+// Bytes 获取JsonBody的内容
+// 参数: 无
+// 返回值:
+// - JsonBody的内容
 func (jsonBody *JsonBody) Bytes() ([]byte, error) {
 	jsonBytes, err := json.Marshal(jsonBody.jsonBodyMap)
 	if err != nil {
@@ -187,6 +347,11 @@ func (jsonBody *JsonBody) Bytes() ([]byte, error) {
 	return jsonBytes, nil
 }
 
+// Unmarshal 将JsonBody的内容Unmarshal到output
+// 参数:
+// - output: 输出,一般为结构指针或map[string]any指针
+// 返回值:
+// - 错误
 func (jsonBody *JsonBody) Unmarshal(output any) error {
 	jsonBytes, err := jsonBody.Bytes()
 	if err != nil {
@@ -201,124 +366,88 @@ func (jsonBody *JsonBody) Unmarshal(output any) error {
 	return nil
 }
 
-func (c *Context) GetJsonBody() (*JsonBody, error) {
-	body, exist := c.Get(bodyKey)
-	if !exist {
-		bytesBody, err := c.readOriginBody()
-		if err != nil {
-			return nil, err
-		}
-
-		jsonBodyMap := make(map[string]any)
-		err = json.Unmarshal(bytesBody, &jsonBodyMap)
-		if err != nil {
-			return nil, errors.New(err.Error())
-		}
-
-		return &JsonBody{
-			c:           c,
-			jsonBodyMap: jsonBodyMap,
-		}, nil
-	}
-
-	switch b := body.(type) {
-	case []byte:
-		jsonBodyMap := make(map[string]any)
-		err := json.Unmarshal(b, &jsonBodyMap)
-		if err != nil {
-			return nil, errors.New(err.Error())
-		}
-
-		return &JsonBody{
-			c:           c,
-			jsonBodyMap: jsonBodyMap,
-		}, nil
-	case map[string]any:
-		return &JsonBody{
-			c:           c,
-			jsonBodyMap: body.(map[string]any),
-		}, nil
-	default:
-		return nil, errors.New("不支持的body类型")
-	}
-}
-
 type QueryPrams struct {
 	c           *Context
 	queryParams map[string]string
 }
 
+// Set 设置查询参数
+// 参数:
+// - key: 查询参数的键
+// - value: 查询参数对应键的值
+// 返回值: 无
 func (queryParams *QueryPrams) Set(key string, value string) {
 	queryParams.queryParams[key] = value
 	queryParams.c.Set(queryParamsKey, queryParams.queryParams)
 }
 
+// Delete 删除JsonBody键对应的值
+// 参数:
+// - key: JsonBody的键
+// 返回值: 无
 func (queryParams *QueryPrams) Delete(key string) {
 	delete(queryParams.queryParams, key)
 	queryParams.c.Set(queryParamsKey, queryParams.queryParams)
 }
 
+// Get 获取查询参数对应键的值
+// 参数:
+// - key: 查询参数的键
+// 返回值:
+// - 查询参数对应键的值
 func (queryParams *QueryPrams) Get(key string) string {
 	return queryParams.queryParams[key]
 }
 
+// Map 获取查询参数的map表示
+// 参数: 无
+// 返回值:
+// - 查询参数的map表示
 func (queryParams *QueryPrams) Map() map[string]string {
 	return queryParams.queryParams
 }
 
-func (c *Context) GetQueryParams() *QueryPrams {
-	queryParams, exist := c.Get(queryParamsKey)
-	if !exist {
-		return &QueryPrams{
-			c:           c,
-			queryParams: c.getAllQueryParams(),
-		}
-	}
-
-	return &QueryPrams{
-		c:           c,
-		queryParams: queryParams.(map[string]string),
-	}
-}
-
 type PathPrams struct {
 	c          *Context
 	pathParams map[string]string
 }
 
+// Set 设置路径参数
+// 参数:
+// - key: 路径参数的键
+// - value: 路径参数对应键的值
+// 返回值: 无
 func (pathParams *PathPrams) Set(key string, value string) {
 	pathParams.pathParams[key] = value
 	pathParams.c.Set(pathParamsKey, pathParams.pathParams)
 }
 
+// Delete 删除JsonBody键对应的值
+// 参数:
+// - key: JsonBody的键
+// 返回值: 无
 func (pathParams *PathPrams) Delete(key string) {
 	delete(pathParams.pathParams, key)
 	pathParams.c.Set(pathParamsKey, pathParams.pathParams)
 }
 
+// Get 获取路径参数对应键的值
+// 参数:
+// - key: 路径参数的键
+// 返回值:
+// - 路径参数对应键的值
 func (pathParams *PathPrams) Get(key string) string {
 	return pathParams.pathParams[key]
 }
 
+// Map 获取路径参数的map表示
+// 参数: 无
+// 返回值:
+// - 路径参数的map表示
 func (pathParams *PathPrams) Map() map[string]string {
 	return pathParams.pathParams
 }
 
-func (c *Context) GetPathParams() *PathPrams {
-	pathParams, exist := c.Get(pathParamsKey)
-	if !exist {
-		return &PathPrams{
-			c:          c,
-			pathParams: c.getAllPathParams(),
-		}
-	}
-
-	return &PathPrams{
-		c:          c,
-		pathParams: pathParams.(map[string]string),
-	}
-}
-
 func (c *Context) getAllQueryParams() map[string]string {
 	queryParams := make(map[string]string)
 

+ 21 - 1
framework/core/application/application.go

@@ -22,6 +22,9 @@ func New(api *api.Api, i *infrastructure.Infrastructure) *App {
 }
 
 // Start 运行应用
+// 参数: 无
+// 返回值:
+// - 错误
 func (app *App) Start() error {
 	err := app.apiInstance.Start()
 	if err != nil {
@@ -32,6 +35,9 @@ func (app *App) Start() error {
 }
 
 // Finish 终止应用
+// 参数: 无
+// 返回值:
+// - 错误
 func (app *App) Finish() error {
 	err := app.apiInstance.Finish()
 	if err != nil {
@@ -41,17 +47,31 @@ func (app *App) Finish() error {
 	return nil
 }
 
-// Api 获取api实例
+// Api 获取Api实例
+// 参数: 无
+// 返回值:
+// - Api实例
 func (app *App) Api() *api.Api {
 	return app.apiInstance
 }
 
 // Infrastructure 获取Infrastructure实例
+// 参数: 无
+// 返回值:
+// - Infrastructure实例
 func (app *App) Infrastructure() *infrastructure.Infrastructure {
 	return app.infrastructureInstance
 }
 
 // ChooseRouter 选择Router
+// 参数:
+// - routerType: 路由类型,有两种
+//   - api.RouterRoot: 根路由
+//   - api.RouterPrefix: 带url前缀的路由
+//
+// - version: 版本,用于获取基于上面两中路由够造的带有版本号的路由,传空字符串则选定上面两种路由本身
+// 返回值:
+// - 选择的Router
 func (app *App) ChooseRouter(routerType string, version string) api.Router {
 	return app.Api().ChooseRouter(routerType, version)
 }

+ 4 - 0
framework/core/application/info.go

@@ -1,18 +1,22 @@
 package application
 
+// InfoIDField Info的ID字段
 type InfoIDField struct {
 	ID string `json:"id" sqlresult:"column:id"`
 }
 
+// InfoTenantIDField Info的租户ID字段
 type InfoTenantIDField struct {
 	TenantID string `json:"tenantId" sqlresult:"column:tenant_id"`
 }
 
+// InfoUserIDFields Info的用户ID相关字段
 type InfoUserIDFields struct {
 	CreateUserID     string `json:"createUserId" sqlresult:"column:create_user_id;"`
 	LastUpdateUserID string `json:"lastUpdateUserId" sqlresult:"column:last_update_user_id;"`
 }
 
+// InfoTimeFields Info的时间相关字段
 type InfoTimeFields struct {
 	CreatedTime     string `json:"createdTime" sqlresult:"column:created_time;parseTime:'2006-01-02 15:04:05'"`
 	LastUpdatedTime string `json:"lastUpdatedTime" sqlresult:"column:last_updated_time;parseTime:'2006-01-02 15:04:05'"`

+ 2 - 2
framework/gateway/builder_request.go

@@ -199,8 +199,8 @@ func (req *GetRequest) Response() *http_client.Response {
 	return req.response
 }
 
-func (req *GetRequest) prepare(c *api.Context) (*DeleteRequest, error) {
-	preparedRequest := &DeleteRequest{
+func (req *GetRequest) prepare(c *api.Context) (*GetRequest, error) {
+	preparedRequest := &GetRequest{
 		Url:         req.Url,
 		QueryParams: req.QueryParams,
 		PathParams:  req.PathParams,