Skip to content

Commit

Permalink
updated luhn check
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 14, 2023
1 parent 7365ba3 commit 4c579ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions worker/internal/benthos/transformers/card_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GenerateCardNumber(luhn bool) (int64, error) {

if luhn {

val, err := GenerateValidVLuhnCheckCardNumber()
val, err := GenerateValidLuhnCheckCardNumber()

if err != nil {
return 0, fmt.Errorf("unable to generate a luhn valid card number")

Check warning on line 49 in worker/internal/benthos/transformers/card_number.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/card_number.go#L49

Added line #L49 was not covered by tests
Expand All @@ -67,7 +67,7 @@ func GenerateCardNumber(luhn bool) (int64, error) {
}

// generates a card number that passes luhn validation
func GenerateValidVLuhnCheckCardNumber() (int64, error) {
func GenerateValidLuhnCheckCardNumber() (int64, error) {

// To find the checksum digit on
cardNo := make([]int, 0)
Expand All @@ -82,15 +82,19 @@ func GenerateValidVLuhnCheckCardNumber() (int64, error) {
}

// Acc no (9 digits)
nineDigits, _ := transformer_utils.GenerateRandomInt(int64(9))
nineDigits, err := transformer_utils.GenerateRandomInt(int64(9))
if err != nil {
return 0, err
}

Check warning on line 88 in worker/internal/benthos/transformers/card_number.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/card_number.go#L87-L88

Added lines #L87 - L88 were not covered by tests
strNine := strconv.FormatInt(nineDigits, 10)
var nineInts []int
for _, k := range strNine {
nineInts = append(nineInts, int(k))
}
for _, i := range nineInts {
cardNo = append(cardNo, i)
cardNum = append(cardNum, i)
digit, err := strconv.Atoi(string(k))
if err != nil {
fmt.Println("Error converting to int:", err)
continue

Check warning on line 94 in worker/internal/benthos/transformers/card_number.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/card_number.go#L93-L94

Added lines #L93 - L94 were not covered by tests
}
cardNo = append(cardNo, digit)
cardNum = append(cardNum, digit)
}

// odd position digits
Expand Down
2 changes: 1 addition & 1 deletion worker/internal/benthos/transformers/card_number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestGenerateValidLuhnCardNumber(t *testing.T) {

val, err := GenerateValidVLuhnCheckCardNumber()
val, err := GenerateValidLuhnCheckCardNumber()

assert.NoError(t, err)
assert.Len(t, strconv.FormatInt(val, 10), 16, "The output card should be 16 characters long")
Expand Down

0 comments on commit 4c579ec

Please sign in to comment.