|
@@ -1,6 +1,7 @@
|
|
|
package rest
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"reflect"
|
|
|
"strings"
|
|
@@ -241,6 +242,25 @@ func Fields(obj any, files RequestFiles, names ...any) (json.Map, IErrorArgs) {
|
|
|
return res, nil
|
|
|
}
|
|
|
|
|
|
+// GetStructFields возвращает список полей структуры
|
|
|
+func FieldNames(s any) ([]string, error) {
|
|
|
+ // Получаем тип и проверяем, что это структура
|
|
|
+ t := reflect.TypeOf(s)
|
|
|
+ if t.Kind() == reflect.Ptr || t.Kind() == reflect.Interface {
|
|
|
+ return FieldNames(reflect.ValueOf(s).Elem().Interface())
|
|
|
+ } else if t.Kind() != reflect.Struct {
|
|
|
+ return nil, errors.New("expected struct last type")
|
|
|
+ }
|
|
|
+ // Создаем срез для хранения имен полей
|
|
|
+ var fields []string
|
|
|
+ // Перебираем поля структуры
|
|
|
+ for i := 0; i < t.NumField(); i++ {
|
|
|
+ field := t.Field(i)
|
|
|
+ fields = append(fields, camelToSnake(field.Name))
|
|
|
+ }
|
|
|
+ return fields, nil
|
|
|
+}
|
|
|
+
|
|
|
/////////////////////////////////////////////
|
|
|
|
|
|
func OutFileds(req IRequestIn, obj any, files RequestFiles, names ...any) IRequestOut {
|