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: ogrn and ogrnip #16

Merged
merged 4 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions BENCHMARKS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Benchmarks

## BIK

goos: darwin
goarch: arm64
pkg: github.com/sshaplygin/docs-code/bik
BenchmarkValidateCorrect-10 8064954 140.7 ns/op 256 B/op 3 allocs/op
BenchmarkGenerate-10 615589 1972 ns/op 240 B/op 18 allocs/op
PASS
ok github.com/sshaplygin/docs-code/bik 2.635s

## INN

goos: darwin
goarch: arm64
pkg: github.com/sshaplygin/docs-code/inn
BenchmarkValidateCorrectLegal-10 2719714 443.8 ns/op 616 B/op 23 allocs/op
BenchmarkValidateCorrectPhysical-10 2076355 576.6 ns/op 936 B/op 30 allocs/op
BenchmarkGenerate-10 394204 3133 ns/op 875 B/op 42 allocs/op
BenchmarkGenerateLegal-10 354616 3213 ns/op 801 B/op 41 allocs/op
BenchmarkGeneratePhysical-10 492985 2419 ns/op 974 B/op 41 allocs/op
PASS
ok github.com/sshaplygin/docs-code/inn 7.215s

## KPP

goos: darwin
goarch: arm64
pkg: github.com/sshaplygin/docs-code/kpp
BenchmarkValidateCorrect-10 5280958 218.9 ns/op 216 B/op 8 allocs/op
BenchmarkGenerate-10 484114 2434 ns/op 385 B/op 22 allocs/op
PASS
ok github.com/sshaplygin/docs-code/kpp 2.810s

## OGRN

goos: darwin
goarch: arm64
pkg: github.com/sshaplygin/docs-code/ogrn
BenchmarkValidateCorrect-10 2583738 457.3 ns/op 728 B/op 18 allocs/op
BenchmarkGenerate-10 294908 3938 ns/op 841 B/op 45 allocs/op
PASS
ok github.com/sshaplygin/docs-code/ogrn 3.074s

## OGRNIP

goos: darwin
goarch: arm64
pkg: github.com/sshaplygin/docs-code/ogrnip
BenchmarkValidateCorrect-10 1991065 580.4 ns/op 1008 B/op 24 allocs/op
BenchmarkGenerate-10 403179 3100 ns/op 1010 B/op 46 allocs/op
PASS
ok github.com/sshaplygin/docs-code/ogrnip 3.411s
10 changes: 6 additions & 4 deletions bik/bik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ func TestValidate(t *testing.T) {
}

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

assert.True(t, isValid)
assert.True(t, isValid)
}
}

func Test_Exists(t *testing.T) {
Expand Down
21 changes: 8 additions & 13 deletions fts/fts.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,8 @@ func (trc *TaxRegionCode) Ints() []int {

res := make([]int, subjectCodeLength+regionTaxServiceNumberLength)

nums := utils.CodeToInts(int(trc.subjectCode))
idx := subjectCodeLength - 1
for i := len(nums) - 1; i >= 0; i-- {
res[idx] = nums[i]
idx--
}

nums = utils.CodeToInts(int(trc.serviceNumber))
idx = len(res) - 1
for i := len(nums) - 1; i >= 0; i-- {
res[idx] = nums[i]
idx--
}
utils.FillSlice(utils.CodeToInts(int(trc.subjectCode)), res, subjectCodeLength-1)
utils.FillSlice(utils.CodeToInts(int(trc.serviceNumber)), res, len(res)-1)

return res
}
Expand Down Expand Up @@ -154,6 +143,12 @@ func (csc ConstitutionRegionCode) GetName() string {
return codeName
}

func (csc ConstitutionRegionCode) Ints() []int {
res := make([]int, subjectCodeLength)
utils.FillSlice(utils.CodeToInts(int(csc)), res, len(res)-1)
return res
}

func GenerateConstitutionSubjectCode() ConstitutionRegionCode {
return regionsCodes[utils.Random(0, len(regionsCodes)-1)]
}
Expand Down
6 changes: 6 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/sshaplygin/docs-code/bik"
"github.com/sshaplygin/docs-code/inn"
"github.com/sshaplygin/docs-code/kpp"
"github.com/sshaplygin/docs-code/ogrn"
"github.com/sshaplygin/docs-code/ogrnip"
)

type GenerateFunc func() string
Expand All @@ -17,6 +19,10 @@ func Generate(docType DocType) string {
callFunc = inn.Generate
case KPP:
callFunc = kpp.Generate
case OGRN:
callFunc = ogrn.Generate
case OGRNIP:
callFunc = ogrnip.Generate
}

if callFunc == nil {
Expand Down
33 changes: 13 additions & 20 deletions inn/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,36 @@ const (
)

type SerialNumber struct {
val int
size int
val int
len int
}

func (sn SerialNumber) String() string {
return utils.StrCode(sn.val, sn.size)
return utils.StrCode(sn.val, sn.len)
}

func (sn *SerialNumber) Ints() []int {
if sn == nil {
return nil
}

res := make([]int, sn.size)
nums := utils.CodeToInts(sn.val)

idx := len(res) - 1
for i := len(nums) - 1; i >= 0; i-- {
res[idx] = nums[i]
idx--
}
res := make([]int, sn.len)
utils.FillSlice(utils.CodeToInts(sn.val), res, len(res)-1)

return res
}

func GenerateSerailNumber(innType INNType) SerialNumber {
if innType == Physical {
return SerialNumber{
val: int(utils.RandomDigits(physicalSerialNumberLength)),
size: physicalSerialNumberLength,
val: int(utils.RandomDigits(physicalSerialNumberLength)),
len: physicalSerialNumberLength,
}
}

return SerialNumber{
val: int(utils.RandomDigits(legalSerialNumberLength)),
size: legalSerialNumberLength,
val: int(utils.RandomDigits(legalSerialNumberLength)),
len: legalSerialNumberLength,
}
}

Expand Down Expand Up @@ -111,7 +105,6 @@ func GenerateCheckSums(innType INNType, nums []int) CheckSums {
nums = append(nums, f(nums))
}

fmt.Println(nums[len(nums)+shiftIdx:])
return nums[len(nums)+shiftIdx:]
}

Expand Down Expand Up @@ -148,10 +141,10 @@ func ParseINN(inn string) (*INNStruct, error) {
}

t := Physical
snSize := physicalSerialNumberLength
snlen := physicalSerialNumberLength
parseIdx := len(inn) - 2
if len(inn) == legalLength {
snSize = legalSerialNumberLength
snlen = legalSerialNumberLength
t = Legal
parseIdx = len(inn) - 1
const foreignLegalStartWith = "9909"
Expand All @@ -173,8 +166,8 @@ func ParseINN(inn string) (*INNStruct, error) {
return &INNStruct{
taxRegionCode: taxRegionCode,
serialNumber: SerialNumber{
val: utils.SliceToInt(serialNumberArr),
size: snSize,
val: utils.SliceToInt(serialNumberArr),
len: snlen,
},
checkSums: checkSums,
t: t,
Expand Down
4 changes: 2 additions & 2 deletions kpp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ var (
// ErrInvalidReasonCode invalid reason code
ErrInvalidReasonCode = errors.New("invalid reason code")

// ErrInvalidSerialNumber invalid serial number
ErrInvalidSerialNumber = errors.New("invalid serial number")
// ErrInvalidSerialNumbers invalid serial number
ErrInvalidSerialNumbers = errors.New("invalid serial number")
)
10 changes: 6 additions & 4 deletions kpp/kpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ func TestValidate(t *testing.T) {
}

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

assert.True(t, isValid)
assert.True(t, isValid)
}
}

func BenchmarkValidateCorrect(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion kpp/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (kpp *KPPStruct) IsValid() (bool, error) {
}

if !kpp.serialNumber.IsValid() {
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidSerialNumber, kpp.serialNumber)
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidSerialNumbers, kpp.serialNumber)
}

return true, nil
Expand Down
20 changes: 20 additions & 0 deletions ogrn/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ogrn

import "errors"

var (
// ErrNilOGRN try call methods for nil ogrn struct
ErrNilOGRN = errors.New("nil ogrn struct")

// ErrInvalidCodeType invalid code type
ErrInvalidCodeType = errors.New("invalid code type")

// ErrInvalidYearsNumbers invalid years number code
ErrInvalidYearsNumbers = errors.New("invalid years number code")

// ErrInvalidRegion invalid region code
ErrInvalidRegion = errors.New("invalid region code")

// ErrInvalidSerialNumbers invalid serial numbers
ErrInvalidSerialNumbers = errors.New("invalid serial numbers")
)
Loading