/
githubmirror
/
photoprism
Обзор
Документация
Войти
/
githubmirror
/
photoprism
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
internal/entity/entity_values.go
89 строк
2 KB
Michael Mayer
Go: Apply go fix modernizations across backend packages
20 фев 2026, 05:54
20 фев 2026, 05:54
a2b7615
Код
Авторство
О чём код?
package entity import ( "fmt" "reflect" "slices" ) // Values is a shorthand alias for map[string]interface{}. type Values = map[string]any // Map is retained for backward compatibility. // TODO: Remove when no longer needed. type Map = Values // ModelValues extracts exported struct fields into a Values map, optionally omitting selected names. func ModelValues(m any, omit ...string) (result Values, omitted []any, err error) { mustOmit := func(name string) bool { return slices.Contains(omit, name) } r := reflect.ValueOf(m) if r.Kind() != reflect.Pointer { return result, omitted, fmt.Errorf("model interface expected") } values := r.Elem() if kind := values.Kind(); kind != reflect.Struct { return result, omitted, fmt.Errorf("model expected") } t := values.Type() num := t.NumField() omitted = make([]any, 0, len(omit)) result = make(Values, num) // Add exported fields to result. for i := range num { field := t.Field(i) // Skip non-exported fields. if !field.IsExported() { continue } fieldName := field.Name // Skip timestamps. if fieldName == "" || fieldName == "UpdatedAt" || fieldName == "CreatedAt" { continue } v := values.Field(i) switch v.Kind() { case reflect.Slice, reflect.Chan, reflect.Func, reflect.Map, reflect.UnsafePointer: continue case reflect.Struct: if v.IsZero() { continue } } // Skip read-only fields. if !v.CanSet() { continue } // Skip omitted. if mustOmit(fieldName) { if !v.IsZero() { omitted = append(omitted, v.Interface()) } continue } // Add value to result. result[fieldName] = v.Interface() } if len(result) == 0 { return result, omitted, fmt.Errorf("no values") } return result, omitted, nil }