fix(go.sum): update ResolveSpec dependency to v1.0.87
CI / build-and-test (push) Failing after 1s
Release / release (push) Failing after 19m26s

This commit is contained in:
Hein
2026-06-23 13:17:16 +02:00
parent 0227912325
commit 1adf50e3db
2436 changed files with 1078758 additions and 114 deletions
+128
View File
@@ -0,0 +1,128 @@
package reflection
import (
"reflect"
"strings"
"github.com/bitechdev/ResolveSpec/pkg/logger"
)
type ModelFieldDetail struct {
Name string `json:"name"`
DataType string `json:"datatype"`
SQLName string `json:"sqlname"`
SQLDataType string `json:"sqldatatype"`
SQLKey string `json:"sqlkey"`
Nullable bool `json:"nullable"`
FieldValue reflect.Value `json:"-"`
}
// GetModelColumnDetail - Get a list of columns in the SQL declaration of the model
// This function recursively processes embedded structs to include their fields
func GetModelColumnDetail(record reflect.Value) []ModelFieldDetail {
defer func() {
if r := recover(); r != nil {
logger.Error("Panic in GetModelColumnDetail : %v", r)
}
}()
lst := make([]ModelFieldDetail, 0)
if !record.IsValid() {
return lst
}
if record.Kind() == reflect.Pointer || record.Kind() == reflect.Interface {
record = record.Elem()
}
if record.Kind() != reflect.Struct {
return lst
}
collectFieldDetails(record, &lst)
return lst
}
// collectFieldDetails recursively collects field details from a struct value and its embedded fields
func collectFieldDetails(record reflect.Value, lst *[]ModelFieldDetail) {
modeltype := record.Type()
for i := 0; i < modeltype.NumField(); i++ {
fieldtype := modeltype.Field(i)
fieldValue := record.Field(i)
// Check if this is an embedded struct
if fieldtype.Anonymous {
// Unwrap pointer type if necessary
embeddedValue := fieldValue
if fieldValue.Kind() == reflect.Pointer {
if fieldValue.IsNil() {
// Skip nil embedded pointers
continue
}
embeddedValue = fieldValue.Elem()
}
// Recursively process embedded struct
if embeddedValue.Kind() == reflect.Struct {
collectFieldDetails(embeddedValue, lst)
continue
}
}
gormdetail := fieldtype.Tag.Get("gorm")
gormdetail = strings.Trim(gormdetail, " ")
fielddetail := ModelFieldDetail{}
fielddetail.FieldValue = fieldValue
fielddetail.Name = fieldtype.Name
fielddetail.DataType = fieldtype.Type.Name()
fielddetail.SQLName = fnFindKeyVal(gormdetail, "column:")
fielddetail.SQLDataType = fnFindKeyVal(gormdetail, "type:")
gormdetailLower := strings.ToLower(gormdetail)
switch {
case strings.Index(gormdetailLower, "identity") > 0 || strings.Index(gormdetailLower, "primary_key") > 0:
fielddetail.SQLKey = "primary_key"
case strings.Contains(gormdetailLower, "unique"):
fielddetail.SQLKey = "unique"
case strings.Contains(gormdetailLower, "uniqueindex"):
fielddetail.SQLKey = "uniqueindex"
}
if strings.Contains(strings.ToLower(gormdetail), "nullable") {
fielddetail.Nullable = true
} else if strings.Contains(strings.ToLower(gormdetail), "null") {
fielddetail.Nullable = true
}
if strings.Contains(strings.ToLower(gormdetail), "not null") {
fielddetail.Nullable = false
}
if strings.Contains(strings.ToLower(gormdetail), "foreignkey:") {
fielddetail.SQLKey = "foreign_key"
ik := strings.Index(strings.ToLower(gormdetail), "foreignkey:")
ie := strings.Index(gormdetail[ik:], ";")
if ie > ik && ik > 0 {
fielddetail.SQLName = strings.ToLower(gormdetail)[ik+11 : ik+ie]
// fmt.Printf("\r\nforeignkey: %v", fielddetail)
}
}
// ";foreignkey:rid_parent;association_foreignkey:id_atevent;save_associations:false;association_autocreate:false;"
*lst = append(*lst, fielddetail)
}
}
func fnFindKeyVal(src, key string) string {
icolStart := strings.Index(strings.ToLower(src), strings.ToLower(key))
val := ""
if icolStart >= 0 {
val = src[icolStart+len(key):]
icolend := strings.Index(val, ";")
if icolend > 0 {
val = val[:icolend]
}
return val
}
return ""
}
+137
View File
@@ -0,0 +1,137 @@
package reflection
import (
"reflect"
"strings"
)
func Len(v any) int {
val := reflect.ValueOf(v)
valKind := val.Kind()
if valKind == reflect.Pointer {
val = val.Elem()
}
switch val.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.String, reflect.Chan:
return val.Len()
default:
return 0
}
}
// ExtractTableNameOnly extracts the table name from a fully qualified table reference.
// It removes any schema prefix (e.g., "schema.table" -> "table") and truncates at
// the first delimiter (comma, space, tab, or newline). If the input contains multiple
// dots, it returns everything after the last dot up to the first delimiter.
func ExtractTableNameOnly(fullName string) string {
// First, split by dot to remove schema prefix if present
lastDotIndex := -1
for i, char := range fullName {
if char == '.' {
lastDotIndex = i
}
}
// Start from after the last dot (or from beginning if no dot)
startIndex := 0
if lastDotIndex != -1 {
startIndex = lastDotIndex + 1
}
// Now find the end (first delimiter after the table name)
for i := startIndex; i < len(fullName); i++ {
char := rune(fullName[i])
if char == ',' || char == ' ' || char == '\t' || char == '\n' {
return fullName[startIndex:i]
}
}
return fullName[startIndex:]
}
// IsEmptyValue reports whether v is nil, an empty string, or a zero number.
func IsEmptyValue(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Pointer {
if rv.IsNil() {
return true
}
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.String:
return rv.String() == ""
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rv.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return rv.Uint() == 0
case reflect.Float32, reflect.Float64:
return rv.Float() == 0
}
return false
}
// GetPointerElement returns the element type if the provided reflect.Type is a pointer.
// If the type is a slice of pointers, it returns the element type of the pointer within the slice.
// If neither condition is met, it returns the original type.
func GetPointerElement(v reflect.Type) reflect.Type {
if v.Kind() == reflect.Pointer {
return v.Elem()
}
if v.Kind() == reflect.Slice && v.Elem().Kind() == reflect.Pointer {
subElem := v.Elem()
if subElem.Elem().Kind() == reflect.Pointer {
return subElem.Elem().Elem()
}
return v.Elem()
}
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 ""
}
// Unwrap pointer and slice indirections to reach the struct type
for {
switch modelType.Kind() {
case reflect.Pointer, reflect.Slice:
modelType = modelType.Elem()
continue
}
break
}
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 ""
}
File diff suppressed because it is too large Load Diff