mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-15 07:24:25 +00:00
fix: 🎨 fix recursive crud bugs
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package reflection
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Len(v any) int {
|
||||
val := reflect.ValueOf(v)
|
||||
@@ -64,3 +67,41 @@ func GetPointerElement(v reflect.Type) reflect.Type {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// GetJSONNameForField gets the JSON tag name for a struct field.
|
||||
// Returns the JSON field name from the json struct tag, or an empty string if not found.
|
||||
// Handles the "json" tag format: "name", "name,omitempty", etc.
|
||||
func GetJSONNameForField(modelType reflect.Type, fieldName string) string {
|
||||
if modelType == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Handle pointer types
|
||||
if modelType.Kind() == reflect.Ptr {
|
||||
modelType = modelType.Elem()
|
||||
}
|
||||
|
||||
if modelType.Kind() != reflect.Struct {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Find the field
|
||||
field, found := modelType.FieldByName(fieldName)
|
||||
if !found {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Get the JSON tag
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Parse the tag (format: "name,omitempty" or just "name")
|
||||
parts := strings.Split(jsonTag, ",")
|
||||
if len(parts) > 0 && parts[0] != "" && parts[0] != "-" {
|
||||
return parts[0]
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user