📝 README and inline docs
This commit is contained in:
parent
d533a879ef
commit
c61300c6b8
14 changed files with 139 additions and 24 deletions
|
|
@ -5,8 +5,10 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// Threshold defines a custom uint8 type for representing a threshold.
|
||||
type Threshold uint8
|
||||
|
||||
// Supported thresholds are:
|
||||
const (
|
||||
Simple Threshold = iota
|
||||
OneFifth
|
||||
|
|
@ -21,16 +23,19 @@ const (
|
|||
Unanimous
|
||||
)
|
||||
|
||||
// ValidThresholds returns a []Threshold of all supported thresholds.
|
||||
func ValidThresholds() []Threshold {
|
||||
return []Threshold{
|
||||
Simple, Unanimous,
|
||||
OneFifth, OneQuarter, OneThird, OneHalf,
|
||||
TwoFifths, TwoThirds,
|
||||
ThreeFifths, ThreeQuarters, ThreeFifths,
|
||||
ThreeQuarters, ThreeFifths,
|
||||
FourFifths,
|
||||
}
|
||||
}
|
||||
|
||||
// FromString takes a string and returns an initialized Threshold or an error
|
||||
// if the string could not be parsed.
|
||||
func FromString(s string) (Threshold, error) {
|
||||
for _, t := range ValidThresholds() {
|
||||
if strings.ToUpper(t.String()) == strings.ToUpper(s) {
|
||||
|
|
@ -40,6 +45,7 @@ func FromString(s string) (Threshold, error) {
|
|||
return Simple, fmt.Errorf("invalid threshold: %s", s)
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer
|
||||
func (t Threshold) String() string {
|
||||
switch t {
|
||||
case Simple:
|
||||
|
|
@ -69,6 +75,8 @@ func (t Threshold) String() string {
|
|||
}
|
||||
}
|
||||
|
||||
// IsSatisfied takes the number of all votes, yes votes and no votes and
|
||||
// returns a bool indicating if the threshold is satisfied.
|
||||
func (t Threshold) IsSatisfied(totalVotes, yesVotes, noVotes int) bool {
|
||||
if totalVotes == 0 {
|
||||
return false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue