fisch/misc.go
2025-04-11 17:49:54 +02:00

88 lines
1.7 KiB
Go
Executable file

package main
import (
"fmt"
"unicode"
)
func ToTable[T DatabaseType](rows []T, columns TableColumns) Table {
tableRows := make([]TableRow, len(rows))
for i, dT := range rows {
tableRows[i] = dT.ToTableRow()
}
return Table{
columns,
tableRows,
}
}
func IsAlphaNumeric(s string) bool {
for _, r := range s {
if !(unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsSpace(r)) {
return false
}
}
return true
}
func IsEmpty(s string) bool {
if len(s) == 0 {
return true
}
for _, r := range s {
if !unicode.IsSpace(r) {
return false
}
}
return true
}
func LocationsToOption(locations []*Location, selected *Location) ([]*Option, error) {
options, err := NewList[Option](uint(len(locations)))
if err != nil {
return nil, err
}
for _, l := range locations {
fullPath, err := l.FullPath()
if err != nil {
fmt.Println(err)
continue
}
if l.Id.Valid && l.Id.Int64 != selected.Id.Int64 {
option := Option{
Caption: fullPath,
Value: fmt.Sprintf("%v", int(l.Id.Int64)),
Selected: selected.Parent.Valid && int(l.Id.Int64) == int(selected.Parent.Int64),
}
options.Add(&option)
}
}
return options.ToArray(), nil
}
func ContainersToOption(containers []*Container, selected *Container) ([]*Option, error) {
options, err := NewList[Option](uint(len(containers)))
if err != nil {
return nil, err
}
for _, c := range containers {
if c.Id.Valid {
option := Option{
Caption: "",
Value: fmt.Sprintf("%v", int(c.Id.Int64)),
Selected: selected.Id.Valid && int(c.Id.Int64) == int(selected.Id.Int64),
}
if c.Name.Valid {
option.Caption = c.Name.String
}
options.Add(&option)
}
}
return options.ToArray(), nil
}