Skip to content

Commit 43fa2b6

Browse files
authored
Merge pull request #7 from sshaplygin/ref-struct
ref project struct
2 parents 83dd64e + 4a494b7 commit 43fa2b6

27 files changed

+279
-190
lines changed

bik/bik.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,27 @@ package bik
33
import (
44
"strconv"
55

6-
ru_doc_code "github.com/sshaplygin/ru-doc-code"
6+
"github.com/sshaplygin/ru-doc-code/models"
7+
"github.com/sshaplygin/ru-doc-code/utils"
78
)
89

910
// Validate check to valid BIK format
1011
// example valid format is 044525225
1112
func Validate(bik string) (bool, error) {
1213
if len(bik) != 9 {
13-
name, err := ru_doc_code.GetModuleName()
14-
if err != nil {
15-
return false, err
16-
}
17-
return false, &ru_doc_code.CommonError{
18-
Method: name,
19-
Err: ru_doc_code.ErrInvalidLength,
14+
return false, &models.CommonError{
15+
Method: packageName,
16+
Err: models.ErrInvalidLength,
2017
}
2118
}
2219

23-
bikArr, err := ru_doc_code.StrToArr(bik)
20+
bikArr, err := utils.StrToArr(bik)
2421
if err != nil {
2522
return false, err
2623
}
2724

2825
if bikArr[0] != 0 || bikArr[1] != 4 {
29-
return false, ru_doc_code.ErrInvalidBIKCountryCode
26+
return false, ErrInvalidCountryCode
3027
}
3128

3229
// special code
@@ -39,3 +36,7 @@ func Validate(bik string) (bool, error) {
3936

4037
return code >= 50 && code < 1000, nil
4138
}
39+
40+
func Generate() string {
41+
panic("not implemented!")
42+
}

bik/bik_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,28 @@ import (
66

77
"github.com/stretchr/testify/assert"
88

9-
ru_doc_code "github.com/sshaplygin/ru-doc-code"
9+
"github.com/sshaplygin/ru-doc-code/models"
1010
)
1111

1212
func TestValidate(t *testing.T) {
1313
t.Parallel()
1414

15+
type testCase struct {
16+
Code string
17+
IsValid bool
18+
Error error
19+
}
20+
1521
t.Run("invalid bik length", func(t *testing.T) {
16-
testCases := []ru_doc_code.TestCodeCase{
22+
testCases := []testCase{
1723
{
1824
Code: "1234567888776",
19-
Error: ru_doc_code.ErrInvalidLength,
25+
Error: models.ErrInvalidLength,
2026
IsValid: false,
2127
},
2228
{
2329
Code: "044525",
24-
Error: ru_doc_code.ErrInvalidLength,
30+
Error: models.ErrInvalidLength,
2531
IsValid: false,
2632
},
2733
{
@@ -35,6 +41,7 @@ func TestValidate(t *testing.T) {
3541
IsValid: true,
3642
},
3743
}
44+
3845
for i, tc := range testCases {
3946
tc := tc
4047

@@ -49,25 +56,31 @@ func TestValidate(t *testing.T) {
4956
})
5057

5158
t.Run("invalid bik value", func(t *testing.T) {
52-
testCases := []ru_doc_code.TestCodeCase{
59+
type testCase struct {
60+
Code string
61+
IsValid bool
62+
Error error
63+
}
64+
65+
testCases := []testCase{
5366
{
5467
Code: "0445?5226",
55-
Error: ru_doc_code.ErrInvalidValue,
68+
Error: models.ErrInvalidValue,
5669
IsValid: false,
5770
},
5871
{
5972
Code: "054525225",
60-
Error: ru_doc_code.ErrInvalidBIKCountryCode,
73+
Error: ErrInvalidCountryCode,
6174
IsValid: false,
6275
},
6376
{
6477
Code: "104525225",
65-
Error: ru_doc_code.ErrInvalidBIKCountryCode,
78+
Error: ErrInvalidCountryCode,
6679
IsValid: false,
6780
},
6881
{
6982
Code: "044#55#25",
70-
Error: ru_doc_code.ErrInvalidValue,
83+
Error: models.ErrInvalidValue,
7184
IsValid: false,
7285
},
7386
{

bik/errros.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package bik
2+
3+
import "errors"
4+
5+
var (
6+
// ErrInvalidCountryCode invalid bik code country
7+
ErrInvalidCountryCode = errors.New("invalid bik country code")
8+
)

bik/models.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package bik
2+
3+
const packageName = "bik"

errors.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

inn/inn.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"strconv"
55
"strings"
66

7-
ru_doc_code "github.com/sshaplygin/ru-doc-code"
7+
"github.com/sshaplygin/ru-doc-code/models"
8+
"github.com/sshaplygin/ru-doc-code/utils"
89
)
910

1011
const (
@@ -13,8 +14,8 @@ const (
1314
)
1415

1516
type INN struct {
16-
Code ru_doc_code.TaxRegionCode
17-
SerialNumber ru_doc_code.SerialNumber
17+
Code models.TaxRegionCode
18+
SerialNumber models.SerialNumber
1819
Hash10 uint
1920
Hash11 uint
2021
Hash12 uint
@@ -24,13 +25,9 @@ type INN struct {
2425
// example: input format is 7707083893
2526
func Validate(inn string) (bool, error) {
2627
if len(inn) != lengthLegal && len(inn) != lengthPhysical {
27-
name, err := ru_doc_code.GetModuleName()
28-
if err != nil {
29-
return false, err
30-
}
31-
return false, &ru_doc_code.CommonError{
32-
Method: name,
33-
Err: ru_doc_code.ErrInvalidLength,
28+
return false, &models.CommonError{
29+
Method: packageName,
30+
Err: models.ErrInvalidLength,
3431
}
3532
}
3633

@@ -47,15 +44,15 @@ func Validate(inn string) (bool, error) {
4744

4845
// GenerateLegal generate legal type inn string value.
4946
func GenerateLegal() string {
50-
inn := strconv.FormatInt(ru_doc_code.RandomDigits(9), 10)
47+
inn := strconv.FormatInt(utils.RandomDigits(9), 10)
5148
innArr, _ := transformInn(inn)
5249

5350
return inn + strconv.Itoa(hash10(innArr))
5451
}
5552

5653
// GeneratePhysical generate physical type inn string value.
5754
func GeneratePhysical() string {
58-
inn := strconv.FormatInt(ru_doc_code.RandomDigits(10), 10)
55+
inn := strconv.FormatInt(utils.RandomDigits(10), 10)
5956
innArr, _ := transformInn(inn)
6057

6158
hash1Num := hash11(innArr)
@@ -75,7 +72,7 @@ func transformInn(inn string) ([]int, error) {
7572
for _, str := range innNumbers {
7673
number, err := strconv.Atoi(str)
7774
if err != nil {
78-
return nil, ru_doc_code.ErrInvalidValue
75+
return nil, models.ErrInvalidValue
7976
}
8077
innArr = append(innArr, number)
8178
}
@@ -85,7 +82,7 @@ func transformInn(inn string) ([]int, error) {
8582

8683
// Generate generate random type inn string value.
8784
func Generate() string {
88-
if ru_doc_code.RandomDigits(1)%2 == 1 {
85+
if utils.RandomDigits(1)%2 == 1 {
8986
return GeneratePhysical()
9087
}
9188

inn/inn_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,29 @@ import (
77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
99

10-
ru_doc_code "github.com/sshaplygin/ru-doc-code"
10+
"github.com/sshaplygin/ru-doc-code/models"
11+
"github.com/sshaplygin/ru-doc-code/utils"
1112
)
1213

1314
func TestValidate(t *testing.T) {
1415
t.Parallel()
1516

1617
t.Run("invalid inn length", func(t *testing.T) {
17-
testCases := []ru_doc_code.TestCodeCase{
18+
type testCase struct {
19+
Code string
20+
IsValid bool
21+
Error error
22+
}
23+
24+
testCases := []testCase{
1825
{
1926
Code: "12345678",
20-
Error: ru_doc_code.ErrInvalidLength,
27+
Error: models.ErrInvalidLength,
2128
IsValid: false,
2229
},
2330
{
2431
Code: "9876543211123",
25-
Error: ru_doc_code.ErrInvalidLength,
32+
Error: models.ErrInvalidLength,
2633
IsValid: false,
2734
},
2835
{
@@ -50,20 +57,26 @@ func TestValidate(t *testing.T) {
5057
})
5158

5259
t.Run("invalid inn value", func(t *testing.T) {
53-
testCases := []ru_doc_code.TestCodeCase{
60+
type testCase struct {
61+
Code string
62+
IsValid bool
63+
Error error
64+
}
65+
66+
testCases := []testCase{
5467
{
5568
Code: "77$7083893",
56-
Error: ru_doc_code.ErrInvalidValue,
69+
Error: models.ErrInvalidValue,
5770
IsValid: false,
5871
},
5972
{
6073
Code: "98754321N123",
61-
Error: ru_doc_code.ErrInvalidValue,
74+
Error: models.ErrInvalidValue,
6275
IsValid: false,
6376
},
6477
{
6578
Code: "9854132d1123",
66-
Error: ru_doc_code.ErrInvalidValue,
79+
Error: models.ErrInvalidValue,
6780
IsValid: false,
6881
},
6982
{
@@ -162,7 +175,7 @@ func TestGenerate(t *testing.T) {
162175
for _, tc := range tests {
163176
tc := tc
164177

165-
digits = ru_doc_code.RandomDigits(tc.len)
178+
digits = utils.RandomDigits(tc.len)
166179
assert.True(t, digits >= tc.min && digits <= tc.max)
167180
}
168181
})

inn/models.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package inn
2+
3+
const packageName = "packageName"

kpp/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package kpp
2+
3+
import "errors"
4+
5+
var (
6+
// ErrRegistrationReasonCode invalid registration reason code
7+
ErrRegistrationReasonCode = errors.New("invalid registration reason code")
8+
)

0 commit comments

Comments
 (0)