photoprism/internal/form/report.go
Michael Mayer 0096243240 Metadata: Report supported Exiftool, XMP, and Dublin Core tags #2252
Replaces the --no-wrap flag with --md in all "photoprism show ..."
subcommands, as this is easier to understand. See also #2247.
Unused code was opportunistically removed along the way.
2022-04-14 10:49:56 +02:00

83 lines
2 KiB
Go

package form
import (
"fmt"
"reflect"
"time"
"github.com/photoprism/photoprism/pkg/sanitize"
)
// Report returns form fields as table rows for reports.
func Report(f interface{}) (rows [][]string, cols []string) {
cols = []string{"Filter", "Type", "Examples", "Notes"}
v := reflect.ValueOf(f)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return rows, cols
}
rows = make([][]string, 0, v.NumField())
// Iterate through all form fields.
for i := 0; i < v.NumField(); i++ {
if !v.Type().Field(i).IsExported() {
continue
}
fieldValue := v.Field(i)
fieldName := v.Type().Field(i).Tag.Get("form")
fieldInfo := v.Type().Field(i).Tag.Get("serialize")
notes := v.Type().Field(i).Tag.Get("notes")
// Serialize field values as string.
if fieldName != "" && fieldName != "q" && fieldInfo != "-" && notes != "-" {
example := v.Type().Field(i).Tag.Get("example")
typeName := "any"
switch t := fieldValue.Interface().(type) {
case time.Time:
typeName = "timestamp"
if example == "" {
example = fmt.Sprintf("%s:\"2022-01-30 15:23:42\"", fieldName)
}
case int, int8, int16, int32, int64:
typeName = "number"
if example == "" {
example = fmt.Sprintf("%s:0 %s:3", fieldName, fieldName)
}
case uint, uint8, uint16, uint32, uint64:
typeName = "number"
if example == "" {
example = fmt.Sprintf("%s:-1 %s:2", fieldName, fieldName)
}
case float32, float64:
typeName = "decimal"
if example == "" {
example = fmt.Sprintf("%s:1.245", fieldName)
}
case string:
typeName = "string"
if example == "" {
example = fmt.Sprintf("%s:\"name\"", fieldName)
}
case bool:
typeName = "switch"
if example == "" {
example = fmt.Sprintf("%s:yes %s:no", fieldName, fieldName)
}
default:
log.Warnf("failed reporting on %T %s", t, sanitize.Token(fieldName))
continue
}
rows = append(rows, []string{fieldName, typeName, example, notes})
}
}
return rows, cols
}