parcoursmob/renderer/func-maps.go

78 lines
1.2 KiB
Go
Raw Normal View History

package renderer
import (
"fmt"
"time"
)
func TimeFrom(d any) *time.Time {
if date, ok := d.(time.Time); ok {
return &date
} else if date, ok := d.(string); ok {
datetime, err := time.Parse("2006-01-02T15:04:05Z", date)
if err != nil {
panic(err)
}
return &datetime
}
return nil
}
func GenderISO5218(d any) string {
if date, ok := d.(string); ok {
switch date {
case "0":
return "Inconnu"
case "1":
return "Masculin"
case "2":
return "Féminin"
case "9":
return "Sans objet"
}
}
if date, ok := d.(int64); ok {
switch date {
case 0:
return "Inconnu"
case 1:
return "Masculin"
case 2:
return "Féminin"
case 9:
return "Sans objet"
}
}
return ""
}
func Dict(v ...interface{}) map[string]interface{} {
dict := map[string]interface{}{}
lenv := len(v)
for i := 0; i < lenv; i += 2 {
key := strval(v[i])
if i+1 >= lenv {
dict[key] = ""
continue
}
dict[key] = v[i+1]
}
return dict
}
func strval(v interface{}) string {
switch v := v.(type) {
case string:
return v
case []byte:
return string(v)
case error:
return v.Error()
case fmt.Stringer:
return v.String()
default:
return fmt.Sprintf("%v", v)
}
}