Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-panicking MaLo ID checksum function #279

Merged
merged 3 commits into from
Jan 28, 2024
Merged

Conversation

GodsBoss
Copy link
Collaborator

This adds a new function, CalculateMaLoIdCheckSum, that calculates the checksum for a given MaLoID or the first 10 digits of a MaLoID. The existing function, GetMaLoIdCheckSum, is marked as deprecated, but kept for backwards-compatibility.

@GodsBoss GodsBoss requested a review from a team as a code owner January 28, 2024 15:45
Copy link

@hf-krechan hf-krechan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm :)

Copy link
Contributor

@hf-kklein hf-kklein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but I personally would check against a regex.

Comment on lines +129 to +151
errs := make([]error, 0)
if first := maloID[0] - '0'; first < 1 || first > 9 {
errs = append(errs, fmt.Errorf("first char must be a digit from 1-9, is '%c'", maloID[0]))
}

evenSum, oddSum := 0, 0

for index, digitRune := range maloID[:10] {
digit := int(digitRune - '0')
if digit < 0 || digit > 9 {
errs = append(errs, fmt.Errorf("char at index %d must be a digit from 0-9, is '%c'", index, digitRune))
}

// Note: The specification for the MaLo defines the position such that the first digit has the position 1,
// unlike indexing in Go, which starts at 0. To avoid confusion, we explicitly define the position here.
position := index + 1

if position%2 == 0 {
evenSum += digit
} else {
oddSum += digit
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the old approach a bit easier to read. instead for checking each and every character on their own, whether it's a digit, a simple match check with [1-9]\d{9,10} seems more straigh forward to me. Doesn't one fail early in go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a trade-off. A regex check would have been much shorter on the implementation side, but it makes for less readable error messages on the client side. Although in this case the regex is pretty simple and also it would be possible to translate to some error message like "need a string that starts with a non-zero digit, followed by 9 or 10 more digits, got ".

I wouldn't see this as a very important issue as the exact validation is an implementation detail and could be changed later.

@@ -112,6 +116,48 @@ func GetMaLoIdCheckSum(maloIdWithoutCheckSum string) int {
return (10 - (stepC % 10)) % 10
}

// CalculateMaLoIdCheckSum calculates the checksum (last digit) for the given malo ID. Takes both the 10-digit part of a MaLo or the whole MaLo (11 digits).
// Returns an error if maloID's length is neither 10 nor 11, or if the first ten characters do not constitute a maloID.
func CalculateMaLoIdCheckSum(maloID string) (int, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if I'd prefer to return a rune or an integer.

@hf-kklein hf-kklein merged commit 672c295 into main Jan 28, 2024
8 checks passed
@hf-kklein hf-kklein deleted the calculate-malo-id branch January 28, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants