From 89cf772bd305c13b95ad8b81d18294ab79de0c9f Mon Sep 17 00:00:00 2001 From: xufeixiang Date: Mon, 9 Sep 2024 14:53:31 +0800 Subject: [PATCH] feat: suitable to support python-sdk. --- assets/front | 2 +- pkg/common/consts/config_path.go | 2 +- pkg/common/models/sdk.go | 23 ++++++++++++----------- pkg/common/models/storage_profile_hook.go | 2 +- pkg/engine/sdk/hook_reflect.go | 10 +++++----- pkg/engine/sdk/schema.go | 3 ++- pkg/engine/sdk/template.go | 2 ++ pkg/engine/sdk/template_helpers.go | 10 ++++++++++ 8 files changed, 34 insertions(+), 20 deletions(-) diff --git a/assets/front b/assets/front index 5ca81e3..cc4d7fa 160000 --- a/assets/front +++ b/assets/front @@ -1 +1 @@ -Subproject commit 5ca81e3ad7c2fd7362f6a411d3cb1856b2d41f85 +Subproject commit cc4d7fa8b930db955f6ee690f3de787326b5fc30 diff --git a/pkg/common/consts/config_path.go b/pkg/common/consts/config_path.go index 6dd7d08..0cc23e0 100644 --- a/pkg/common/consts/config_path.go +++ b/pkg/common/consts/config_path.go @@ -51,7 +51,7 @@ const ( HookGlobalParent = "global" HookAuthenticationParent = "authentication" HookOperationParent = "operation" - HookStorageParent = "storage" + HookStorageProfileParent = "upload" HookCustomizeParent = "customize" HookProxyParent = "proxy" HookFunctionParent = "function" diff --git a/pkg/common/models/sdk.go b/pkg/common/models/sdk.go index c89be16..fed77cc 100644 --- a/pkg/common/models/sdk.go +++ b/pkg/common/models/sdk.go @@ -28,17 +28,18 @@ const ( ) type Sdk struct { - Name string `json:"name"` - Enabled bool `json:"enabled"` - Type sdkType `json:"type"` - Language string `json:"language"` - Extension string `json:"extension"` - GitUrl string `json:"gitUrl"` - GitBranch string `json:"gitBranch"` - GitCommitHash string `json:"gitCommitHash"` - OutputPath string `json:"outputPath"` - CodePackage string `json:"codePackage"` - UpperFirstBasename bool `json:"upperFirstBasename"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + Type sdkType `json:"type"` + Language string `json:"language"` + Extension string `json:"extension"` + GitUrl string `json:"gitUrl"` + GitBranch string `json:"gitBranch"` + GitCommitHash string `json:"gitCommitHash"` + OutputPath string `json:"outputPath"` + CodePackage string `json:"codePackage"` + UpperFirstBasename bool `json:"upperFirstBasename"` + Keywords []string `json:"keywords"` CreateTime string `json:"createTime"` UpdateTime string `json:"updateTime"` diff --git a/pkg/common/models/storage_profile_hook.go b/pkg/common/models/storage_profile_hook.go index 0260c30..e4567b6 100644 --- a/pkg/common/models/storage_profile_hook.go +++ b/pkg/common/models/storage_profile_hook.go @@ -60,7 +60,7 @@ func buildS3UploadProfileHook(hook consts.UploadHook, enabledFunc func(item *wgp if codePackage != "" { outputPath = utils.NormalizePath(outputPath, codePackage) } - outputPath = utils.NormalizePath(outputPath, consts.HookStorageParent) + outputPath = utils.NormalizePath(outputPath, consts.HookStorageProfileParent) } item.Root = outputPath item.UpperFirstBasename = upperFirstBasename diff --git a/pkg/engine/sdk/hook_reflect.go b/pkg/engine/sdk/hook_reflect.go index 44881a3..7572cb0 100644 --- a/pkg/engine/sdk/hook_reflect.go +++ b/pkg/engine/sdk/hook_reflect.go @@ -41,7 +41,7 @@ type ( endpoints map[string]*endpointMetadata } baseRequestBody struct { - Wg baseRequestBodyWg `json:"__wg"` + Wg baseRequestBodyWg `json:"__wg,omitempty"` } baseRequestBodyWg struct { ClientRequest hooks.WunderGraphRequest `json:"clientRequest"` @@ -288,7 +288,7 @@ func (o *reflectObjectFactory) reflectBaseHook() { hookParentSchema.Title = "HookParent" hookParentSchema.Enum = append(hookParentSchema.Enum, consts.HookGeneratedParent, consts.HookGlobalParent, consts.HookAuthenticationParent, - consts.HookOperationParent, consts.HookStorageParent, consts.HookCustomizeParent, + consts.HookOperationParent, consts.HookStorageProfileParent, consts.HookCustomizeParent, consts.HookProxyParent, consts.HookFunctionParent, consts.HookFragmentsParent) o.buildObjectFromDataSchema(&openapi3.SchemaRef{Value: hookParentSchema}, &objectField{}) @@ -378,9 +378,9 @@ func (o *reflectObjectFactory) reflectOperationHook() { // 生成上传钩子相关的jsonschema func (o *reflectObjectFactory) reflectUploadHook() { - o.reflectStructSchema(uploadHookPayload{}, consts.HookStorageParent) - o.reflectStructSchema(hooks.UploadHookResponse{}, consts.HookStorageParent) - o.reflectStructSchema(s3uploadclient.UploadedFiles{}, consts.HookStorageParent) + o.reflectStructSchema(uploadHookPayload{}, consts.HookStorageProfileParent) + o.reflectStructSchema(hooks.UploadHookResponse{}, consts.HookStorageProfileParent) + o.reflectStructSchema(s3uploadclient.UploadedFiles{}, consts.HookStorageProfileParent) uploadHookSchema := openapi3.NewStringSchema() uploadHookSchema.Title = utils.GetTypeName(consts.PreUpload) diff --git a/pkg/engine/sdk/schema.go b/pkg/engine/sdk/schema.go index b27d94c..431611d 100644 --- a/pkg/engine/sdk/schema.go +++ b/pkg/engine/sdk/schema.go @@ -331,7 +331,8 @@ func (o *objectInfoFactory) optimizeFieldInfo() { for _, field := range o.objectFieldMap { o.buildObjectFieldItemRef(field) slices.SortFunc(field.Fields, func(a *objectField, b *objectField) bool { - return a.Name < b.Name + aa, bb := cast.ToInt(a.Required), cast.ToInt(b.Required) + return aa > bb || aa == bb && a.Name < b.Name }) } diff --git a/pkg/engine/sdk/template.go b/pkg/engine/sdk/template.go index 3b81d4d..ee3d03b 100644 --- a/pkg/engine/sdk/template.go +++ b/pkg/engine/sdk/template.go @@ -71,6 +71,7 @@ type ( OnceMap map[string]any LengthMap map[string]any + Sdk *models.Sdk } ) @@ -168,6 +169,7 @@ func (t *templateContext) generateTemplate(sdk *models.Sdk) { } t.OnceMap = make(map[string]any) + t.Sdk = sdk ignoredFiles := getIgnoredFiles(utils.NormalizePath(outputPath, ignoreFile)) existIgnoredFiles := len(ignoredFiles) > 0 if existIgnoredFiles { diff --git a/pkg/engine/sdk/template_helpers.go b/pkg/engine/sdk/template_helpers.go index 4e88024..35ab9c8 100644 --- a/pkg/engine/sdk/template_helpers.go +++ b/pkg/engine/sdk/template_helpers.go @@ -163,4 +163,14 @@ func init() { return defaultValue }) + // 获取值或默认值 + handlebars.RegisterHelper("filterFieldsByKeywords", func(fields []*objectField, keywords []string) []*objectField { + var result []*objectField + for _, item := range fields { + if slices.Contains(keywords, item.Name) { + result = append(result, item) + } + } + return result + }) }