-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 𝐧𝐞𝐰 functions to get checksums of Technische / Steuerbare Ressour…
…ce; Change MaLo-ID Checksum func to not panic but return error (#324) * Add functions to get checksums of Technische and Steuerbare Ressource * less panic * tests are green * Update bo/netzlokation.go --------- Co-authored-by: Konstantin <konstantin.klein+github@hochfrequenz.de> Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>
- Loading branch information
1 parent
e91cfc1
commit bdd76c8
Showing
7 changed files
with
166 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"unicode" | ||
) | ||
|
||
// GetChecksum calculates the checksum of a (Netzlokation/Technische Ressource/Steuerbare Ressource) ID for a given 10-digit ID string (that does not contain a without checksum yet) | ||
func GetChecksum(idWithoutChecksum string) (string, error) { | ||
if len(idWithoutChecksum) != 10 { | ||
return "", fmt.Errorf("You must provide a string that is 10 characters long but '%s' is %d characters long", idWithoutChecksum, len(idWithoutChecksum)) | ||
} | ||
evenSum := 0 | ||
oddSum := 0 | ||
for index, digitRune := range idWithoutChecksum[0:10] { | ||
var digit int | ||
if !unicode.IsDigit(digitRune) { | ||
// if the digitRune is a letter, then we du the usual ASCII conversion | ||
digit = int(digitRune) // digit is 65 for digitRune='A' | ||
} else { | ||
//, but if it's a "digit" character, then we use the digits value. einmal mit profis arbeiten | ||
digit = int(digitRune - '0') // digit is 0 for digitRune='0' | ||
} | ||
if index%2 == 0 { | ||
// this is "odd", because BDEW starts counting at 1, so the first index is odd 🙄 einmal mit profis arbeiten | ||
oddSum = oddSum + digit | ||
} else { | ||
evenSum = evenSum + digit | ||
} | ||
} | ||
stepD := oddSum + (evenSum * 2) | ||
result := (((stepD/10)+1)*10 - stepD) % 10 | ||
return strconv.Itoa(result), nil | ||
} |