2023-10-18 13:40:21 +02:00
|
|
|
package models
|
|
|
|
|
2023-11-18 11:12:07 +01:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2023-11-20 14:14:09 +01:00
|
|
|
"encoding/json"
|
2023-11-18 11:12:07 +01:00
|
|
|
"fmt"
|
2023-11-21 15:12:13 +01:00
|
|
|
"sort"
|
2023-11-18 11:12:07 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-21 15:12:13 +01:00
|
|
|
type AttributeList map[string]string
|
|
|
|
|
2023-10-18 13:40:21 +02:00
|
|
|
type Participant struct {
|
2023-12-05 22:11:08 +01:00
|
|
|
// ID string `csv:"id" gorm:"primaryKey"`
|
|
|
|
Meta
|
2023-10-18 13:40:21 +02:00
|
|
|
|
2023-11-18 11:12:07 +01:00
|
|
|
Firstname string `csv:"firstname"`
|
|
|
|
Lastname string `csv:"lastname"`
|
2023-11-05 14:36:33 +01:00
|
|
|
|
2023-12-12 09:21:55 +01:00
|
|
|
Token string `csv:"token"`
|
2023-11-05 14:36:33 +01:00
|
|
|
|
2023-11-21 15:12:13 +01:00
|
|
|
Attributes AttributeList `csv:"attributes"`
|
2023-10-18 13:40:21 +02:00
|
|
|
}
|
2023-11-18 11:12:07 +01:00
|
|
|
|
|
|
|
func (p *Participant) String() string {
|
|
|
|
return fmt.Sprintf("%s %s", p.Lastname, p.Firstname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Participant) GetHash() string {
|
|
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(append([]string{p.Lastname, p.Firstname}, p.AttributesToSlice()...), ""))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Participant) AttributesToSlice() []string {
|
|
|
|
result := make([]string, len(p.Attributes)*2)
|
|
|
|
|
|
|
|
for k, v := range p.Attributes {
|
|
|
|
result = append(result, k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2023-11-20 14:14:09 +01:00
|
|
|
|
|
|
|
func (p *Participant) Marshal() ([]byte, error) {
|
|
|
|
return json.Marshal(p)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Participant) Unmarshal(data []byte) error {
|
|
|
|
return json.Unmarshal(data, p)
|
|
|
|
}
|
2023-11-21 15:12:13 +01:00
|
|
|
|
|
|
|
func (al AttributeList) MarshalCSV() (string, error) {
|
|
|
|
result := convertMapToKeyValueOrderedString(al)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 18:24:10 +01:00
|
|
|
func (al AttributeList) UnmarshalCSV(csv string) error {
|
|
|
|
al = make(map[string]string)
|
|
|
|
al["foo"] = "bar"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:12:13 +01:00
|
|
|
func convertMapToKeyValueOrderedString(m map[string]string) string {
|
|
|
|
keys := make([]string, 0, len(m))
|
|
|
|
for key := range m {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
var result strings.Builder
|
|
|
|
for _, key := range keys {
|
|
|
|
result.WriteString(key)
|
|
|
|
result.WriteString(":")
|
|
|
|
result.WriteString(m[key])
|
|
|
|
result.WriteString(",")
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSuffix(result.String(), ",")
|
|
|
|
}
|