-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from sshaplygin/add-snils
add: snils
- Loading branch information
Showing
9 changed files
with
142 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,94 @@ | ||
package snils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/sshaplygin/docs-code/models" | ||
"github.com/sshaplygin/docs-code/utils" | ||
) | ||
|
||
const packageName = "snils" | ||
|
||
type Numbers []int | ||
|
||
const checkSumLength = 2 | ||
|
||
type checkSum int | ||
|
||
type SNILSStruct struct { | ||
numbers Numbers | ||
checkSum checkSum | ||
} | ||
|
||
func (ss *SNILSStruct) String() string { | ||
var res strings.Builder | ||
res.Grow(snilsFullLength) | ||
|
||
for i := 0; i < len(ss.numbers); i++ { | ||
if i%3 == 0 && i != 0 { | ||
res.WriteString("-") | ||
} | ||
res.WriteString(strconv.Itoa(ss.numbers[i])) | ||
} | ||
|
||
res.WriteString(" ") | ||
res.WriteString(utils.StrCode(int(ss.checkSum), checkSumLength)) | ||
|
||
return res.String() | ||
} | ||
|
||
func (ss *SNILSStruct) IsValid() (bool, error) { | ||
return ss.calculateCheckSum() == ss.checkSum, nil | ||
} | ||
|
||
const ( | ||
snilsFullLength = 14 | ||
snilsShrinkLength = 11 | ||
) | ||
|
||
func ParseSNILS(snils string) (*SNILSStruct, error) { | ||
if len(snils) != snilsFullLength { | ||
return nil, &models.CommonError{ | ||
Method: packageName, | ||
Err: models.ErrInvalidLength, | ||
} | ||
} | ||
|
||
fSnils := strings.ReplaceAll(snils, "-", "") | ||
fSnils = strings.ReplaceAll(fSnils, " ", "") | ||
|
||
if len(fSnils) != snilsShrinkLength { | ||
return nil, ErrInvalidFormattedLength | ||
} | ||
|
||
snilsArr, err := utils.StrToArr(fSnils) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse raw %s: %w", packageName, err) | ||
} | ||
|
||
return &SNILSStruct{ | ||
numbers: Numbers(snilsArr[:len(snilsArr)-2]), | ||
checkSum: checkSum(utils.SliceToInt(snilsArr[len(snilsArr)-2:])), | ||
}, nil | ||
} | ||
|
||
func NewSNILS() *SNILSStruct { | ||
data := &SNILSStruct{ | ||
numbers: Numbers(utils.CodeToInts(int(utils.RandomDigits(9)))), | ||
} | ||
|
||
data.checkSum = data.calculateCheckSum() | ||
|
||
return data | ||
} | ||
|
||
func (ss *SNILSStruct) calculateCheckSum() checkSum { | ||
var hashSum int | ||
for i, v := range ss.numbers { | ||
hashSum += v * (len(ss.numbers) - i) | ||
} | ||
|
||
return checkSum(hashSum % 101 % 100) | ||
} |
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 |
---|---|---|
@@ -1,46 +1,21 @@ | ||
package snils | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/sshaplygin/docs-code/models" | ||
"github.com/sshaplygin/docs-code/utils" | ||
"fmt" | ||
) | ||
|
||
// Validate check to valid SNILS format | ||
// example: input format is 112-233-445 95 | ||
func Validate(snils string) (bool, error) { | ||
if len(snils) != 14 { | ||
return false, &models.CommonError{ | ||
Method: packageName, | ||
Err: models.ErrInvalidLength, | ||
} | ||
} | ||
|
||
fSnils := strings.ReplaceAll(snils, "-", "") | ||
fSnils = strings.ReplaceAll(fSnils, " ", "") | ||
|
||
if len(fSnils) != 11 { | ||
return false, ErrInvalidFormattedLength | ||
} | ||
|
||
snilsArr, err := utils.StrToArr(fSnils) | ||
snilsData, err := ParseSNILS(snils) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
hashSum := 0 | ||
hashLen := len(fSnils) - 2 | ||
code, _ := strconv.Atoi(fSnils[hashLen:]) | ||
for i, v := range snilsArr[:hashLen] { | ||
hashSum += v * (hashLen - i) | ||
return false, fmt.Errorf("parse %s model: %w", packageName, err) | ||
} | ||
|
||
return hashSum%101 == code, nil | ||
return snilsData.IsValid() | ||
} | ||
|
||
// Generate generate random | ||
func Generate() string { | ||
panic("not implemented!") | ||
return NewSNILS().String() | ||
} |
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ const ( | |
KPP | ||
OGRN | ||
OGRNIP | ||
SNILS | ||
) |
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