Skip to content

Commit

Permalink
Merge pull request #19 from sshaplygin/add-snils
Browse files Browse the repository at this point in the history
add: snils
  • Loading branch information
sshaplygin authored Jan 7, 2024
2 parents ee133dc + 7c53bf5 commit 78e7c36
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 39 deletions.
20 changes: 20 additions & 0 deletions BENCHMARKS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Benchmarks

## Notice

Command to update result:

```bash
go test -benchmem -run=^$ -bench . github.com/sshaplygin/docs-code/snils >> BENCHMARKS.md
```

## BIK

```
Expand Down Expand Up @@ -62,3 +70,15 @@ BenchmarkGenerate-10 403179 3100 ns/op 1010 B/op 46
PASS
ok github.com/sshaplygin/docs-code/ogrnip 3.411s
```

## SNILS

```
goos: darwin
goarch: arm64
pkg: github.com/sshaplygin/docs-code/snils
BenchmarkValidateCorrect-10 4451258 263.2 ns/op 336 B/op 5 allocs/op
BenchmarkGenerate-10 1302042 895.3 ns/op 568 B/op 25 allocs/op
PASS
ok github.com/sshaplygin/docs-code/snils 3.768s
```
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ Status of implementation by code package:
- [ ] OKATO
- [ ] Generate method
- [ ] Validate method
- [ ] SNILS
- [ ] Generate method
- [x] Validate method

Full supported codes: BIK, INN, KPP, OGRN, OGRNIP
Full supported codes: BIK, INN, KPP, OGRN, OGRNIP, SNILS

## Usage

Expand Down
3 changes: 3 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/sshaplygin/docs-code/kpp"
"github.com/sshaplygin/docs-code/ogrn"
"github.com/sshaplygin/docs-code/ogrnip"
"github.com/sshaplygin/docs-code/snils"
)

type GenerateFunc func() string
Expand All @@ -23,6 +24,8 @@ func Generate(docType DocType) string {
callFunc = ogrn.Generate
case OGRNIP:
callFunc = ogrnip.Generate
case SNILS:
callFunc = snils.Generate
}

if callFunc == nil {
Expand Down
91 changes: 91 additions & 0 deletions snils/models.go
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)
}
35 changes: 5 additions & 30 deletions snils/snils.go
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()
}
21 changes: 17 additions & 4 deletions snils/snils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func TestValidate(t *testing.T) {
},
{
Code: "112-233-445 98",
Error: nil,
IsValid: false,
},
{
Expand All @@ -85,7 +84,6 @@ func TestValidate(t *testing.T) {
},
{
Code: "112-233-445 95",
Error: nil,
IsValid: true,
},
{
Expand All @@ -109,7 +107,22 @@ func TestValidate(t *testing.T) {
}

func Test_Generate(t *testing.T) {
require.Panics(t, func() {
for i := 0; i < 10; i++ {
snils := Generate()
isValid, err := Validate(snils)
require.NoError(t, err, fmt.Sprintf("invalid ogrnip value: %s", snils))

assert.True(t, isValid)
}
}

func BenchmarkValidateCorrect(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Validate("112-233-445 95")
}
}
func BenchmarkGenerate(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate()
})
}
}
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ const (
KPP
OGRN
OGRNIP
SNILS
)
2 changes: 1 addition & 1 deletion utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func StrCode(val, length int) string {

n := length
if len(code) > length {
panic(fmt.Sprintf("invalid int code length: %d, %d", len(code), length))
panic(fmt.Sprintf("invalid int code '%s' length: %d, %d", code, len(code), length))
}

str.Grow(n)
Expand Down
3 changes: 3 additions & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/sshaplygin/docs-code/kpp"
"github.com/sshaplygin/docs-code/ogrn"
"github.com/sshaplygin/docs-code/ogrnip"
"github.com/sshaplygin/docs-code/snils"
)

type ValidateFunc func(code string) (bool, error)
Expand All @@ -23,6 +24,8 @@ func Validate(docType DocType, code string) (bool, error) {
callFunc = ogrn.Validate
case OGRNIP:
callFunc = ogrnip.Validate
case SNILS:
callFunc = snils.Validate
}

if callFunc == nil {
Expand Down

0 comments on commit 78e7c36

Please sign in to comment.