feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
132
backend/application/base/appinfra/app_infra.go
Normal file
132
backend/application/base/appinfra/app_infra.go
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package appinfra
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/imagex"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/cache/redis"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/es"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/eventbus"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/idgen"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/imagex/veimagex"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/mysql"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/storage"
|
||||
"github.com/coze-dev/coze-studio/backend/types/consts"
|
||||
)
|
||||
|
||||
type AppDependencies struct {
|
||||
DB *gorm.DB
|
||||
CacheCli *redis.Client
|
||||
IDGenSVC idgen.IDGenerator
|
||||
ESClient es.Client
|
||||
ImageXClient imagex.ImageX
|
||||
TOSClient storage.Storage
|
||||
ResourceEventProducer eventbus.Producer
|
||||
AppEventProducer eventbus.Producer
|
||||
}
|
||||
|
||||
func Init(ctx context.Context) (*AppDependencies, error) {
|
||||
deps := &AppDependencies{}
|
||||
var err error
|
||||
|
||||
deps.DB, err = mysql.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deps.CacheCli = redis.New()
|
||||
|
||||
deps.IDGenSVC, err = idgen.New(deps.CacheCli)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deps.ESClient, err = es.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deps.ImageXClient, err = initImageX(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deps.TOSClient, err = initTOS(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deps.ResourceEventProducer, err = initResourceEventBusProducer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deps.AppEventProducer, err = initAppEventProducer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deps, nil
|
||||
}
|
||||
|
||||
func initImageX(ctx context.Context) (imagex.ImageX, error) {
|
||||
|
||||
uploadComponentType := os.Getenv(consts.FileUploadComponentType)
|
||||
|
||||
if uploadComponentType != consts.FileUploadComponentTypeImagex {
|
||||
return storage.NewImagex(ctx)
|
||||
}
|
||||
return veimagex.New(
|
||||
os.Getenv(consts.VeImageXAK),
|
||||
os.Getenv(consts.VeImageXSK),
|
||||
os.Getenv(consts.VeImageXDomain),
|
||||
os.Getenv(consts.VeImageXUploadHost),
|
||||
os.Getenv(consts.VeImageXTemplate),
|
||||
[]string{os.Getenv(consts.VeImageXServerID)},
|
||||
)
|
||||
}
|
||||
|
||||
func initTOS(ctx context.Context) (storage.Storage, error) {
|
||||
return storage.New(ctx)
|
||||
}
|
||||
|
||||
func initResourceEventBusProducer() (eventbus.Producer, error) {
|
||||
nameServer := os.Getenv(consts.MQServer)
|
||||
resourceEventBusProducer, err := eventbus.NewProducer(nameServer,
|
||||
consts.RMQTopicResource, consts.RMQConsumeGroupResource, 1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init resource producer failed, err=%w", err)
|
||||
}
|
||||
|
||||
return resourceEventBusProducer, nil
|
||||
}
|
||||
|
||||
func initAppEventProducer() (eventbus.Producer, error) {
|
||||
nameServer := os.Getenv(consts.MQServer)
|
||||
appEventProducer, err := eventbus.NewProducer(nameServer, consts.RMQTopicApp, consts.RMQConsumeGroupApp, 1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init app producer failed, err=%w", err)
|
||||
}
|
||||
|
||||
return appEventProducer, nil
|
||||
}
|
||||
42
backend/application/base/ctxutil/api_auth.go
Normal file
42
backend/application/base/ctxutil/api_auth.go
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ctxutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/openauth/openapiauth/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
|
||||
"github.com/coze-dev/coze-studio/backend/types/consts"
|
||||
)
|
||||
|
||||
func GetApiAuthFromCtx(ctx context.Context) *entity.ApiKey {
|
||||
data, ok := ctxcache.Get[*entity.ApiKey](ctx, consts.OpenapiAuthKeyInCtx)
|
||||
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func MustGetUIDFromApiAuthCtx(ctx context.Context) int64 {
|
||||
apiKeyInfo := GetApiAuthFromCtx(ctx)
|
||||
if apiKeyInfo == nil {
|
||||
panic("mustGetUIDFromApiAuthCtx: apiKeyInfo is nil")
|
||||
}
|
||||
return apiKeyInfo.UserID
|
||||
}
|
||||
35
backend/application/base/ctxutil/request.go
Normal file
35
backend/application/base/ctxutil/request.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ctxutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func GetRequestFullPathFromCtx(ctx context.Context) string {
|
||||
contextValue := ctx.Value("request.full_path")
|
||||
if contextValue == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
fullPath, ok := contextValue.(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return fullPath
|
||||
}
|
||||
52
backend/application/base/ctxutil/session.go
Normal file
52
backend/application/base/ctxutil/session.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ctxutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/user/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
|
||||
"github.com/coze-dev/coze-studio/backend/types/consts"
|
||||
)
|
||||
|
||||
func GetUserSessionFromCtx(ctx context.Context) *entity.Session {
|
||||
data, ok := ctxcache.Get[*entity.Session](ctx, consts.SessionDataKeyInCtx)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func MustGetUIDFromCtx(ctx context.Context) int64 {
|
||||
sessionData := GetUserSessionFromCtx(ctx)
|
||||
if sessionData == nil {
|
||||
panic("mustGetUIDFromCtx: sessionData is nil")
|
||||
}
|
||||
|
||||
return sessionData.UserID
|
||||
}
|
||||
|
||||
func GetUIDFromCtx(ctx context.Context) *int64 {
|
||||
sessionData := GetUserSessionFromCtx(ctx)
|
||||
if sessionData == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &sessionData.UserID
|
||||
}
|
||||
327
backend/application/base/pluginutil/api.go
Normal file
327
backend/application/base/pluginutil/api.go
Normal file
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package pluginutil
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
|
||||
common "github.com/coze-dev/coze-studio/backend/api/model/plugin_develop_common"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
func APIParamsToOpenapiOperation(reqParams, respParams []*common.APIParameter) (*openapi3.Operation, error) {
|
||||
op := &openapi3.Operation{}
|
||||
|
||||
hasSetReqBody := false
|
||||
hasSetParams := false
|
||||
|
||||
for _, apiParam := range reqParams {
|
||||
if apiParam.Location != common.ParameterLocation_Body {
|
||||
if !hasSetParams {
|
||||
hasSetParams = true
|
||||
op.Parameters = []*openapi3.ParameterRef{}
|
||||
}
|
||||
|
||||
_apiParam, err := toOpenapiParameter(apiParam)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
op.Parameters = append(op.Parameters, &openapi3.ParameterRef{
|
||||
Value: _apiParam,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
var mType *openapi3.MediaType
|
||||
if hasSetReqBody {
|
||||
mType = op.RequestBody.Value.Content[plugin.MediaTypeJson]
|
||||
} else {
|
||||
hasSetReqBody = true
|
||||
mType = &openapi3.MediaType{
|
||||
Schema: &openapi3.SchemaRef{
|
||||
Value: &openapi3.Schema{
|
||||
Type: openapi3.TypeObject,
|
||||
Properties: map[string]*openapi3.SchemaRef{},
|
||||
},
|
||||
},
|
||||
}
|
||||
op.RequestBody = &openapi3.RequestBodyRef{
|
||||
Value: &openapi3.RequestBody{
|
||||
Content: map[string]*openapi3.MediaType{
|
||||
plugin.MediaTypeJson: mType,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
_apiParam, err := toOpenapi3Schema(apiParam)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mType.Schema.Value.Properties[apiParam.Name] = &openapi3.SchemaRef{
|
||||
Value: _apiParam,
|
||||
}
|
||||
if apiParam.IsRequired {
|
||||
mType.Schema.Value.Required = append(mType.Schema.Value.Required, apiParam.Name)
|
||||
}
|
||||
}
|
||||
|
||||
hasSetRespBody := false
|
||||
|
||||
for _, apiParam := range respParams {
|
||||
if !hasSetRespBody {
|
||||
hasSetRespBody = true
|
||||
op.Responses = map[string]*openapi3.ResponseRef{
|
||||
strconv.Itoa(http.StatusOK): {
|
||||
Value: &openapi3.Response{
|
||||
Content: map[string]*openapi3.MediaType{
|
||||
plugin.MediaTypeJson: {
|
||||
Schema: &openapi3.SchemaRef{
|
||||
Value: &openapi3.Schema{
|
||||
Type: openapi3.TypeObject,
|
||||
Properties: map[string]*openapi3.SchemaRef{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
_apiParam, err := toOpenapi3Schema(apiParam)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, _ := op.Responses[strconv.Itoa(http.StatusOK)]
|
||||
mType, _ := resp.Value.Content[plugin.MediaTypeJson] // only support application/json
|
||||
mType.Schema.Value.Properties[apiParam.Name] = &openapi3.SchemaRef{
|
||||
Value: _apiParam,
|
||||
}
|
||||
|
||||
if apiParam.IsRequired {
|
||||
mType.Schema.Value.Required = append(mType.Schema.Value.Required, apiParam.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return op, nil
|
||||
}
|
||||
|
||||
func toOpenapiParameter(apiParam *common.APIParameter) (*openapi3.Parameter, error) {
|
||||
paramType, ok := plugin.ToOpenapiParamType(apiParam.Type)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the type '%s' of field '%s' is invalid", apiParam.Type, apiParam.Name))
|
||||
}
|
||||
|
||||
if paramType == openapi3.TypeObject {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the type of field '%s' cannot be 'object'", apiParam.Name))
|
||||
}
|
||||
|
||||
paramSchema := &openapi3.Schema{
|
||||
Type: paramType,
|
||||
Default: apiParam.GlobalDefault,
|
||||
Extensions: map[string]interface{}{
|
||||
plugin.APISchemaExtendGlobalDisable: apiParam.GlobalDisable,
|
||||
},
|
||||
}
|
||||
|
||||
if paramType == openapi3.TypeArray {
|
||||
if apiParam.Location == common.ParameterLocation_Path {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the type of field '%s' cannot be 'array'", apiParam.Name))
|
||||
}
|
||||
if len(apiParam.SubParameters) == 0 {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the sub parameters of field '%s' is required", apiParam.Name))
|
||||
}
|
||||
|
||||
arrayItem := apiParam.SubParameters[0]
|
||||
arrayItemType, ok := plugin.ToOpenapiParamType(arrayItem.Type)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the item type '%s' of field '%s' is invalid", arrayItemType, apiParam.Name))
|
||||
}
|
||||
|
||||
if arrayItemType == openapi3.TypeObject || arrayItemType == openapi3.TypeArray {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the item type of field '%s' cannot be 'array' or 'object'", apiParam.Name))
|
||||
}
|
||||
|
||||
itemSchema := &openapi3.Schema{
|
||||
Type: arrayItemType,
|
||||
Description: arrayItem.Desc,
|
||||
Extensions: map[string]any{},
|
||||
}
|
||||
|
||||
if arrayItem.GetAssistType() > 0 {
|
||||
aType, ok := plugin.ToAPIAssistType(arrayItem.GetAssistType())
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the assist type '%s' of field '%s' is invalid", arrayItem.GetAssistType(), apiParam.Name))
|
||||
}
|
||||
itemSchema.Extensions[plugin.APISchemaExtendAssistType] = aType
|
||||
format, ok := plugin.AssistTypeToFormat(aType)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the assist type '%s' of field '%s' is invalid", aType, apiParam.Name))
|
||||
}
|
||||
itemSchema.Format = format
|
||||
}
|
||||
|
||||
paramSchema.Items = &openapi3.SchemaRef{
|
||||
Value: itemSchema,
|
||||
}
|
||||
}
|
||||
|
||||
if apiParam.LocalDefault != nil && *apiParam.LocalDefault != "" {
|
||||
paramSchema.Default = apiParam.LocalDefault
|
||||
}
|
||||
if apiParam.LocalDisable {
|
||||
paramSchema.Extensions[plugin.APISchemaExtendLocalDisable] = true
|
||||
}
|
||||
if apiParam.VariableRef != nil && *apiParam.VariableRef != "" {
|
||||
paramSchema.Extensions[plugin.APISchemaExtendVariableRef] = apiParam.VariableRef
|
||||
}
|
||||
|
||||
if apiParam.GetAssistType() > 0 {
|
||||
aType, ok := plugin.ToAPIAssistType(apiParam.GetAssistType())
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the assist type '%s' of field '%s' is invalid", apiParam.GetAssistType(), apiParam.Name))
|
||||
}
|
||||
paramSchema.Extensions[plugin.APISchemaExtendAssistType] = aType
|
||||
format, ok := plugin.AssistTypeToFormat(aType)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the assist type '%s' of field '%s' is invalid", aType, apiParam.Name))
|
||||
}
|
||||
paramSchema.Format = format
|
||||
}
|
||||
|
||||
loc, ok := plugin.ToHTTPParamLocation(apiParam.Location)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the location '%s' of field '%s' is invalid ", apiParam.Location, apiParam.Name))
|
||||
}
|
||||
|
||||
param := &openapi3.Parameter{
|
||||
Description: apiParam.Desc,
|
||||
Name: apiParam.Name,
|
||||
In: string(loc),
|
||||
Required: apiParam.IsRequired,
|
||||
Schema: &openapi3.SchemaRef{
|
||||
Value: paramSchema,
|
||||
},
|
||||
}
|
||||
|
||||
return param, nil
|
||||
}
|
||||
|
||||
func toOpenapi3Schema(apiParam *common.APIParameter) (*openapi3.Schema, error) {
|
||||
paramType, ok := plugin.ToOpenapiParamType(apiParam.Type)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the type '%s' of field '%s' is invalid", apiParam.Type, apiParam.Name))
|
||||
}
|
||||
|
||||
sc := &openapi3.Schema{
|
||||
Description: apiParam.Desc,
|
||||
Type: paramType,
|
||||
Default: apiParam.GlobalDefault,
|
||||
Extensions: map[string]interface{}{
|
||||
plugin.APISchemaExtendGlobalDisable: apiParam.GlobalDisable,
|
||||
},
|
||||
}
|
||||
if apiParam.LocalDefault != nil && *apiParam.LocalDefault != "" {
|
||||
sc.Default = apiParam.LocalDefault
|
||||
}
|
||||
if apiParam.LocalDisable {
|
||||
sc.Extensions[plugin.APISchemaExtendLocalDisable] = true
|
||||
}
|
||||
if apiParam.VariableRef != nil && *apiParam.VariableRef != "" {
|
||||
sc.Extensions[plugin.APISchemaExtendVariableRef] = apiParam.VariableRef
|
||||
}
|
||||
|
||||
if apiParam.GetAssistType() > 0 {
|
||||
aType, ok := plugin.ToAPIAssistType(apiParam.GetAssistType())
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the assist type '%s' of field '%s' is invalid", apiParam.GetAssistType(), apiParam.Name))
|
||||
}
|
||||
sc.Extensions[plugin.APISchemaExtendAssistType] = aType
|
||||
format, ok := plugin.AssistTypeToFormat(aType)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the assist type '%s' of field '%s' is invalid", aType, apiParam.Name))
|
||||
}
|
||||
sc.Format = format
|
||||
}
|
||||
|
||||
switch paramType {
|
||||
case openapi3.TypeObject:
|
||||
sc.Properties = map[string]*openapi3.SchemaRef{}
|
||||
for _, subParam := range apiParam.SubParameters {
|
||||
_subParam, err := toOpenapi3Schema(subParam)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sc.Properties[subParam.Name] = &openapi3.SchemaRef{
|
||||
Value: _subParam,
|
||||
}
|
||||
if subParam.IsRequired {
|
||||
sc.Required = append(sc.Required, subParam.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
|
||||
case openapi3.TypeArray:
|
||||
if len(apiParam.SubParameters) == 0 {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the sub-parameters of field '%s' are required", apiParam.Name))
|
||||
}
|
||||
|
||||
arrayItem := apiParam.SubParameters[0]
|
||||
itemType, ok := plugin.ToOpenapiParamType(arrayItem.Type)
|
||||
if !ok {
|
||||
return nil, errorx.New(errno.ErrPluginInvalidParamCode,
|
||||
errorx.KVf(errno.PluginMsgKey, "the item type '%s' of field '%s' is invalid", itemType, apiParam.Name))
|
||||
}
|
||||
|
||||
subParam, err := toOpenapi3Schema(arrayItem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sc.Items = &openapi3.SchemaRef{
|
||||
Value: subParam,
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
Reference in New Issue
Block a user