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
This commit is contained in:
Jan Dittberner 2022-03-21 12:16:24 +01:00
parent 63c748bb1d
commit 623bdf6d56
4 changed files with 14 additions and 10 deletions

View file

@ -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)
}

View file

@ -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."

View file

@ -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)

View file

@ -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 {