Skip to content

Commit 0fc06df

Browse files
committed
refactor: removed error sentinels for vote registration
1 parent 7280780 commit 0fc06df

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

pkg/mocai/entities/voteregistration/countries/brazilian_vote_registration.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func calculateCheckDigit1(sequenceNumber string) (string, error) {
2727
for i := 0; i < len(sequenceNumber); i++ {
2828
digit, err := strconv.Atoi(string(sequenceNumber[i]))
2929
if err != nil {
30-
return "", ErrInvalidCheckDigit1
30+
return "", fmt.Errorf("%s", translations.Get("en_us", "invalid_check_digit_1"))
3131
}
3232
sum += digit * weights[i]
3333
}
@@ -48,13 +48,13 @@ func calculateCheckDigit2(stateCode, checkDigit1 string, stateCodeInt int) (stri
4848
for i := 0; i < len(stateCode); i++ {
4949
digit, err := strconv.Atoi(string(stateCode[i]))
5050
if err != nil {
51-
return "", ErrInvalidCheckDigit2
51+
return "", fmt.Errorf("%s", translations.Get("en_us", "invalid_check_digit_2"))
5252
}
5353
sum += digit * weights[i]
5454
}
5555
checkDigit1Int, err := strconv.Atoi(checkDigit1)
5656
if err != nil {
57-
return "", ErrInvalidCheckDigit2
57+
return "", fmt.Errorf("%s", translations.Get("en_us", "invalid_check_digit_2"))
5858
}
5959
sum += checkDigit1Int * 9
6060

@@ -63,7 +63,7 @@ func calculateCheckDigit2(stateCode, checkDigit1 string, stateCodeInt int) (stri
6363
checkDigit2 = 0
6464
}
6565

66-
// Special case for states 01 SP and 02 MG
66+
// special case for states 01 SP and 02 MG
6767
if (stateCodeInt == 1 || stateCodeInt == 2) && checkDigit2 == 0 {
6868
checkDigit2 = 1
6969
}
@@ -72,35 +72,47 @@ func calculateCheckDigit2(stateCode, checkDigit1 string, stateCodeInt int) (stri
7272
}
7373

7474
func NewBrazilianVoteRegistrationCustom(lang string, isFormatted bool, rnd translations.RandSource) (*BrazilianVoteRegistration, error) {
75+
if lang == "" {
76+
lang = "en_us"
77+
}
78+
79+
supported := false
80+
if translations.Get(lang, "invalid_vote_registration") != "invalid_vote_registration" {
81+
supported = true
82+
}
83+
if !supported {
84+
lang = "en_us"
85+
}
86+
7587
if rnd == nil {
7688
rnd = translations.DefaultRandSource()
7789
}
7890

79-
// Generate a random 3-digit section and zone
91+
// generate a random 3-digit section and zone
8092
section := fmt.Sprintf("%03d", rnd.Intn(1000))
8193
zone := fmt.Sprintf("%03d", rnd.Intn(1000))
8294

83-
// Generate an 8-digit sequence number
95+
// generate an 8-digit sequence number
8496
sequenceNumber := rnd.Intn(99999999) + 1
8597
sequenceNumberStr := fmt.Sprintf("%08d", sequenceNumber)
8698

87-
// Generate a random state code 01 to 28
99+
// generate a random state code 01 to 28
88100
stateCode := rnd.Intn(28) + 1
89101
stateCodeStr := fmt.Sprintf("%02d", stateCode)
90102

91-
// Calculate the first check digit
103+
// calculate the first check digit
92104
checkDigit1, err := calculateCheckDigit1(sequenceNumberStr)
93105
if err != nil {
94106
return nil, fmt.Errorf("%s", translations.Get(lang, "invalid_check_digit_1"))
95107
}
96108

97-
// Calculate the second check digit
109+
// calculate the second check digit
98110
checkDigit2, err := calculateCheckDigit2(stateCodeStr, checkDigit1, stateCode)
99111
if err != nil {
100112
return nil, fmt.Errorf("%s", translations.Get(lang, "invalid_check_digit_2"))
101113
}
102114

103-
// Combine everything to form the complete number
115+
// combine everything to form the complete number
104116
number := sequenceNumberStr + stateCodeStr + checkDigit1 + checkDigit2
105117
if number == "" || len(number) != 12 {
106118
return nil, fmt.Errorf("%s", translations.Get(lang, "invalid_vote_registration"))

0 commit comments

Comments
 (0)