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.
This commit is contained in:
Marvin Zhang
2025-04-07 18:13:49 +08:00
parent ed29a76566
commit c4291e7a93
3 changed files with 46 additions and 15 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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
}