🎉🚀 May the concensus be with us!

This commit is contained in:
Brian Wiborg 2024-05-13 00:47:24 +02:00
commit 48328e7db2
No known key found for this signature in database
GPG key ID: BE53FA9286B719D6
33 changed files with 4051 additions and 0 deletions

40
voting/vote/choice.go Normal file
View file

@ -0,0 +1,40 @@
package vote
import (
"fmt"
"strings"
)
type Choice int8
func (choice Choice) String() string {
switch choice {
case Yes:
return "YIP"
case No:
return "NOPE"
case Abstain:
return "DUNNO"
default:
panic("this code should never be reached")
}
}
const (
Abstain Choice = 0
Yes Choice = 1
No Choice = -1
)
func ValidChoices() []Choice {
return []Choice{Yes, No, Abstain}
}
func ChoiceFromString(s string) (Choice, error) {
for _, c := range ValidChoices() {
if strings.ToUpper(c.String()) == strings.ToUpper(s) {
return c, nil
}
}
return Abstain, fmt.Errorf("invalid choice: %s", s)
}

32
voting/vote/vote.go Normal file
View file

@ -0,0 +1,32 @@
package vote
import (
"fmt"
"time"
)
type Vote struct {
Elector string
Choice Choice
timestamp time.Time
}
func NewVote(elector string, choice Choice) Vote {
return Vote{
Elector: elector,
Choice: choice,
timestamp: time.Now().UTC(),
}
}
func NewVoteWithTimestamp(elector string, choice Choice, timestamp time.Time) Vote {
return Vote{
Elector: elector,
Choice: choice,
timestamp: timestamp,
}
}
func (vote Vote) String() string {
return fmt.Sprintf("%s %s %s", vote.timestamp.Format(time.DateTime), vote.Choice, vote.Elector)
}