From c4291e7a9374037aa1d5d704e95daf5ea70c520e Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Mon, 7 Apr 2025 18:13:49 +0800 Subject: [PATCH] refactor: update response handling and utility functions - Changed the summary conversion method in getIDForAction from PascalCase to SnakeCase for consistency with naming conventions. - Introduced BaseResponse interface and implemented GetData and GetDataString methods for Response, ListResponse, and VoidResponse types, enhancing response handling. - Simplified the Contains function to specifically handle string slices, improving clarity and maintainability of utility functions. - Ensured alignment with existing coding standards and added necessary comments for better understanding. --- core/controllers/router.go | 2 +- core/controllers/utils.go | 40 ++++++++++++++++++++++++++++++++++++++ core/utils/helpers.go | 19 +++++------------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/core/controllers/router.go b/core/controllers/router.go index 5989c3c5..dcfa8dad 100644 --- a/core/controllers/router.go +++ b/core/controllers/router.go @@ -114,7 +114,7 @@ func registerBuiltinHandler(group *fizz.RouterGroup, wrapper *openapi.FizzWrappe // Helper functions to generate OpenAPI documentation func getIDForAction(method, path, summary string) string { if summary != "" { - return utils.ToPascalCase(summary) + return utils.ToSnakeCase(summary) } // Remove leading slash and convert remaining slashes to underscores diff --git a/core/controllers/utils.go b/core/controllers/utils.go index c5aa77e7..287ff876 100644 --- a/core/controllers/utils.go +++ b/core/controllers/utils.go @@ -280,6 +280,11 @@ func SortsToOption(sorts []entity.Sort) (sort bson.D, err error) { return sort, nil } +type BaseResponse interface { + GetData() interface{} + GetDataString() string +} + type Response[T any] struct { Status string `json:"status"` Message string `json:"message"` @@ -287,6 +292,18 @@ type Response[T any] struct { Error string `json:"error"` } +func (r Response[T]) GetData() any { + return r.Data +} + +func (r Response[T]) GetDataString() string { + data, err := json.Marshal(r.Data) + if err != nil { + return "" + } + return string(data) +} + type ListResponse[T any] struct { Status string `json:"status"` Message string `json:"message"` @@ -295,12 +312,35 @@ type ListResponse[T any] struct { Error string `json:"error"` } +func (r ListResponse[T]) GetData() any { + return r.Data +} + +func (r ListResponse[T]) GetDataString() string { + if r.Data == nil { + return "" + } + data, err := json.Marshal(r.Data) + if err != nil { + return "" + } + return string(data) +} + type VoidResponse struct { Status string `json:"status"` Message string `json:"message"` Error string `json:"error"` } +func (r VoidResponse) GetData() any { + return nil +} + +func (r VoidResponse) GetDataString() string { + return "" +} + func GetDataResponse[T any](model T) (res *Response[T], err error) { return &Response[T]{ Status: constants.HttpResponseStatusOk, diff --git a/core/utils/helpers.go b/core/utils/helpers.go index f3fcd56e..79fd2046 100644 --- a/core/utils/helpers.go +++ b/core/utils/helpers.go @@ -3,7 +3,6 @@ package utils import ( "github.com/crawlab-team/crawlab/trace" "io" - "reflect" ) func Close(c io.Closer) { @@ -13,19 +12,11 @@ func Close(c io.Closer) { } } -func Contains(array interface{}, val interface{}) (fla bool) { - fla = false - switch reflect.TypeOf(array).Kind() { - case reflect.Slice: - { - s := reflect.ValueOf(array) - for i := 0; i < s.Len(); i++ { - if reflect.DeepEqual(val, s.Index(i).Interface()) { - fla = true - return - } - } +func ContainsString(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true } } - return + return false }