|
| 1 | +package countries |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + globalRand = rand.New(rand.NewSource(time.Now().UnixNano())) |
| 12 | +) |
| 13 | + |
| 14 | +// BrazilianVoteRegistration represents a Brazilian vote registration. |
| 15 | +type BrazilianVoteRegistration struct { |
| 16 | + Section string |
| 17 | + Zone string |
| 18 | + Number string |
| 19 | +} |
| 20 | + |
| 21 | +// GenerateBrazilianVoteRegistration generates a valid Brazilian vote registration number. |
| 22 | +// If formatted is true, the Brazilian vote registration number will be returned in the format XXX XXX XXX. |
| 23 | +// If formatted is false, the Brazilian vote registration number will be returned as a plain string. |
| 24 | +func GenerateBrazilianVoteRegistration(formatted bool) (*BrazilianVoteRegistration, error) { |
| 25 | + section := randomInt3Digits() |
| 26 | + zone := randomInt3Digits() |
| 27 | + |
| 28 | + // Generate an 8 digit sequence number |
| 29 | + sequenceNumber := randomInt(1, 99999999) |
| 30 | + sequenceNumberStr := fmt.Sprintf("%08d", sequenceNumber) |
| 31 | + |
| 32 | + // Generate a random state code 01 to 28 |
| 33 | + stateCode := randomInt(1, 28) |
| 34 | + stateCodeStr := fmt.Sprintf("%02d", stateCode) |
| 35 | + |
| 36 | + // Calculate the first check digit |
| 37 | + checkDigit1, err := calculateCheckDigit1(sequenceNumberStr) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + // Calculate the second check digit |
| 43 | + checkDigit2, err := calculateCheckDigit2(stateCodeStr, checkDigit1, stateCode) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + // Combine everything to form the complete number |
| 49 | + number := sequenceNumberStr + stateCodeStr + checkDigit1 + checkDigit2 |
| 50 | + if number == "" || len(number) != 12 { |
| 51 | + return nil, ErrInvalidVoteRegistration |
| 52 | + } |
| 53 | + |
| 54 | + if formatted { |
| 55 | + number = fmt.Sprintf("%s %s %s", number[:4], number[4:8], number[8:]) |
| 56 | + } |
| 57 | + |
| 58 | + createdBrazilianVoteRegistration := &BrazilianVoteRegistration{ |
| 59 | + Section: section, |
| 60 | + Zone: zone, |
| 61 | + Number: number, |
| 62 | + } |
| 63 | + |
| 64 | + return createdBrazilianVoteRegistration, nil |
| 65 | +} |
| 66 | + |
| 67 | +// randomInt generates a random integer between min and max |
| 68 | +func randomInt(min, max int) int { |
| 69 | + return globalRand.Intn(max-min+1) + min |
| 70 | +} |
| 71 | + |
| 72 | +// randomInt3Digits generates a random 3 digit number |
| 73 | +func randomInt3Digits() string { |
| 74 | + return fmt.Sprintf("%03d", globalRand.Intn(1000)) |
| 75 | +} |
| 76 | + |
| 77 | +// calculateCheckDigit1 calculates the first check digit. |
| 78 | +// It depends on the sequence number. |
| 79 | +func calculateCheckDigit1(sequenceNumber string) (string, error) { |
| 80 | + sum := 0 |
| 81 | + weights := []int{2, 3, 4, 5, 6, 7, 8, 9} |
| 82 | + for i := 0; i < len(sequenceNumber); i++ { |
| 83 | + digit, err := strconv.Atoi(string(sequenceNumber[i])) |
| 84 | + if err != nil { |
| 85 | + return "", ErrInvalidCheckDigit1 |
| 86 | + } |
| 87 | + sum += digit * weights[i] |
| 88 | + } |
| 89 | + |
| 90 | + checkDigit1 := sum % 11 |
| 91 | + if checkDigit1 == 10 { |
| 92 | + checkDigit1 = 0 |
| 93 | + } |
| 94 | + |
| 95 | + return strconv.Itoa(checkDigit1), nil |
| 96 | +} |
| 97 | + |
| 98 | +// calculateCheckDigit2 calculates the second check digit. |
| 99 | +// It depends on the state code and the first check digit. |
| 100 | +func calculateCheckDigit2(stateCode, checkDigit1 string, stateCodeInt int) (string, error) { |
| 101 | + sum := 0 |
| 102 | + weights := []int{7, 8} |
| 103 | + for i := 0; i < len(stateCode); i++ { |
| 104 | + digit, err := strconv.Atoi(string(stateCode[i])) |
| 105 | + if err != nil { |
| 106 | + return "", ErrInvalidCheckDigit2 |
| 107 | + } |
| 108 | + sum += digit * weights[i] |
| 109 | + } |
| 110 | + checkDigit1Int, err := strconv.Atoi(checkDigit1) |
| 111 | + if err != nil { |
| 112 | + return "", ErrInvalidCheckDigit2 |
| 113 | + } |
| 114 | + sum += checkDigit1Int * 9 |
| 115 | + |
| 116 | + checkDigit2 := sum % 11 |
| 117 | + if checkDigit2 == 10 { |
| 118 | + checkDigit2 = 0 |
| 119 | + } |
| 120 | + |
| 121 | + // Special case for states 01 SP and 02 MG |
| 122 | + if (stateCodeInt == 1 || stateCodeInt == 2) && checkDigit2 == 0 { |
| 123 | + checkDigit2 = 1 |
| 124 | + } |
| 125 | + |
| 126 | + return strconv.Itoa(checkDigit2), nil |
| 127 | +} |
0 commit comments