From 623bdf6d56a1752f9f31e2feea3768d77ca1507e Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Mon, 21 Mar 2022 12:16:24 +0100 Subject: [PATCH] Replace magic numbers with constants - use strconv.Atoi and strconv.Itoa where appropriate - use constants for number base and size - use constant for reminder job interval --- boardvoting.go | 8 ++++---- forms.go | 11 +++++++---- jobs.go | 3 ++- models.go | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/boardvoting.go b/boardvoting.go index e69632f..fb86011 100644 --- a/boardvoting.go +++ b/boardvoting.go @@ -185,7 +185,7 @@ type motionParameters struct { } type motionListParameters struct { - Page int64 + Page int Flags struct { Confirmed, Withdraw, Unvoted bool } @@ -200,7 +200,7 @@ func parseMotionParameters(r *http.Request) motionParameters { func parseMotionListParameters(r *http.Request) motionListParameters { var m = motionListParameters{} - if page, err := strconv.ParseInt(r.URL.Query().Get("page"), 10, 0); err != nil { + if page, err := strconv.Atoi(r.URL.Query().Get("page")); err != nil { m.Page = 1 } else { m.Page = page @@ -230,7 +230,7 @@ func motionListHandler(w http.ResponseWriter, r *http.Request) { Decisions []*DecisionForDisplay Voter *Voter Params *motionListParameters - PrevPage, NextPage int64 + PrevPage, NextPage int PageTitle string Flashes interface{} } @@ -513,7 +513,7 @@ func (h *newMotionHandler) Handle(w http.ResponseWriter, r *http.Request) { default: templateContext.Voter = voter templateContext.Form = NewDecisionForm{ - VoteType: strconv.FormatInt(voteTypeMotion, 10), + VoteType: strconv.Itoa(voteTypeMotion), } renderTemplate(w, r, templates, templateContext) } diff --git a/forms.go b/forms.go index 8d573bc..c9c1f23 100644 --- a/forms.go +++ b/forms.go @@ -26,6 +26,9 @@ import ( const ( minimumContentLen = 3 minimumTitleLen = 3 + base10 = 10 + size8Bit = 8 + size64Bit = 64 ) const ( @@ -66,7 +69,7 @@ func (f *NewDecisionForm) Validate() (bool, *Decision) { f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen) } - if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) { + if voteType, err := strconv.ParseUint(f.VoteType, base10, size8Bit); err != nil || (voteType != 0 && voteType != 1) { f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err) } else { data.VoteType = VoteType(uint8(voteType)) @@ -106,7 +109,7 @@ func (f *EditDecisionForm) Validate() (bool, *Decision) { f.Errors["Content"] = fmt.Sprintf("Please enter at least %d words as Text.", minimumContentLen) } - if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) { + if voteType, err := strconv.ParseUint(f.VoteType, base10, size8Bit); err != nil || (voteType != 0 && voteType != 1) { f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type.", err) } else { data.VoteType = VoteType(uint8(voteType)) @@ -142,7 +145,7 @@ func (f *ProxyVoteForm) Validate() (bool, *Voter, *Vote, string) { data := &Vote{} - if voterID, err = strconv.ParseInt(f.Voter, 10, 64); err != nil { + if voterID, err = strconv.ParseInt(f.Voter, base10, size64Bit); err != nil { f.Errors["Voter"] = fmt.Sprintf("Please choose a valid voter: %v.", err) } else if voter, err = GetVoterByID(voterID); err != nil { f.Errors["Voter"] = fmt.Sprintf("Please choose a valid voter: %v.", err) @@ -150,7 +153,7 @@ func (f *ProxyVoteForm) Validate() (bool, *Voter, *Vote, string) { data.VoterID = voter.ID } - if vote, err = strconv.ParseInt(f.Vote, 10, 8); err != nil { + if vote, err = strconv.ParseInt(f.Vote, base10, size8Bit); err != nil { f.Errors["Vote"] = fmt.Sprintf("Please choose a valid vote: %v.", err) } else if voteChoice, ok := VoteChoices[vote]; !ok { f.Errors["Vote"] = "Please choose a valid vote." diff --git a/jobs.go b/jobs.go index 9f43902..08dbed0 100644 --- a/jobs.go +++ b/jobs.go @@ -33,6 +33,7 @@ type jobIdentifier int const ( JobIDCloseDecisions jobIdentifier = iota JobIDRemindVotersJob + reminderDays = 3 ) var rescheduleChannel = make(chan jobIdentifier, 1) @@ -143,7 +144,7 @@ func NewRemindVotersJob() *RemindVotersJob { func (j *RemindVotersJob) Schedule() { year, month, day := time.Now().UTC().Date() - nextExecution := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 3) + nextExecution := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, reminderDays) log.Infof("scheduling RemindVotersJob for %s", nextExecution) diff --git a/models.go b/models.go index 0a567d5..853947b 100644 --- a/models.go +++ b/models.go @@ -472,7 +472,7 @@ func FindDecisionForDisplayByTag(tag string) (*DecisionForDisplay, error) { // This function uses OFFSET for pagination which is not a good idea for larger data sets. // // TODO: migrate to timestamp base pagination -func FindDecisionsForDisplayOnPage(page int64, unVoted bool, voter *Voter) ([]*DecisionForDisplay, error) { +func FindDecisionsForDisplayOnPage(page int, unVoted bool, voter *Voter) ([]*DecisionForDisplay, error) { var decisionsStmt *sqlx.Stmt if unVoted && voter != nil {