📝 README and inline docs

This commit is contained in:
Brian Wiborg 2024-05-14 10:26:48 +02:00
parent d533a879ef
commit c61300c6b8
No known key found for this signature in database
GPG key ID: BE53FA9286B719D6
14 changed files with 139 additions and 24 deletions

View file

@ -5,8 +5,10 @@ import (
"strings"
)
// Choice defines a custom int8 type for use as choice in a vote.
type Choice int8
// String implements fmt.Stringer
func (choice Choice) String() string {
switch choice {
case Yes:
@ -20,16 +22,20 @@ func (choice Choice) String() string {
}
}
// Available choices are:
const (
Abstain Choice = 0
Yes Choice = 1
No Choice = -1
)
// ValidChoices returns a []Choice with all available choices.
func ValidChoices() []Choice {
return []Choice{Yes, No, Abstain}
}
// ChoiceFromString takes a string and returns an initialized Choice or an
// error if the string couldn't be parsed.
func ChoiceFromString(s string) (Choice, error) {
for _, c := range ValidChoices() {
if strings.ToUpper(c.String()) == strings.ToUpper(s) {

View file

@ -5,13 +5,19 @@ import (
"time"
)
// Vote represents an individual vote in a voting.
type Vote struct {
Id string
Elector string
Choice Choice
// Id contains the unique ID of the vote.
Id string
// Elector contains the name of whoever placed the vote.
Elector string
// Choice contains the choice of the vote.
Choice Choice
// timestamp contains the UTC timestamp of when the vote was placed.
timestamp time.Time
}
// NewVote returns an initialized Vote.
func NewVote(id, elector string, choice Choice) Vote {
return Vote{
Id: id,
@ -21,6 +27,8 @@ func NewVote(id, elector string, choice Choice) Vote {
}
}
// NewVoteWithTimestamp returns an initialized Vote with a predefined
// timestamp. This can be useful when loading a vote from the store.
func NewVoteWithTimestamp(id, elector string, choice Choice, timestamp time.Time) Vote {
return Vote{
Id: id,
@ -30,6 +38,7 @@ func NewVoteWithTimestamp(id, elector string, choice Choice, timestamp time.Time
}
}
// String implements fmt.Stringer
func (vote Vote) String() string {
return fmt.Sprintf("%s %s %s %s", vote.Id, vote.timestamp.Format(time.DateTime), vote.Choice, vote.Elector)
}