Skip to content

Commit

Permalink
Merge pull request #7 from sshaplygin/ref-struct
Browse files Browse the repository at this point in the history
ref project struct
  • Loading branch information
sshaplygin authored Dec 23, 2023
2 parents 83dd64e + 4a494b7 commit 43fa2b6
Show file tree
Hide file tree
Showing 27 changed files with 279 additions and 190 deletions.
21 changes: 11 additions & 10 deletions bik/bik.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ package bik
import (
"strconv"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
"github.com/sshaplygin/ru-doc-code/utils"
)

// Validate check to valid BIK format
// example valid format is 044525225
func Validate(bik string) (bool, error) {
if len(bik) != 9 {
name, err := ru_doc_code.GetModuleName()
if err != nil {
return false, err
}
return false, &ru_doc_code.CommonError{
Method: name,
Err: ru_doc_code.ErrInvalidLength,
return false, &models.CommonError{
Method: packageName,
Err: models.ErrInvalidLength,
}
}

bikArr, err := ru_doc_code.StrToArr(bik)
bikArr, err := utils.StrToArr(bik)
if err != nil {
return false, err
}

if bikArr[0] != 0 || bikArr[1] != 4 {
return false, ru_doc_code.ErrInvalidBIKCountryCode
return false, ErrInvalidCountryCode
}

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

return code >= 50 && code < 1000, nil
}

func Generate() string {
panic("not implemented!")
}
31 changes: 22 additions & 9 deletions bik/bik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import (

"github.com/stretchr/testify/assert"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
)

func TestValidate(t *testing.T) {
t.Parallel()

type testCase struct {
Code string
IsValid bool
Error error
}

t.Run("invalid bik length", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
testCases := []testCase{
{
Code: "1234567888776",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Code: "044525",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Expand All @@ -35,6 +41,7 @@ func TestValidate(t *testing.T) {
IsValid: true,
},
}

for i, tc := range testCases {
tc := tc

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

t.Run("invalid bik value", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
type testCase struct {
Code string
IsValid bool
Error error
}

testCases := []testCase{
{
Code: "0445?5226",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Code: "054525225",
Error: ru_doc_code.ErrInvalidBIKCountryCode,
Error: ErrInvalidCountryCode,
IsValid: false,
},
{
Code: "104525225",
Error: ru_doc_code.ErrInvalidBIKCountryCode,
Error: ErrInvalidCountryCode,
IsValid: false,
},
{
Code: "044#55#25",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Expand Down
8 changes: 8 additions & 0 deletions bik/errros.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bik

import "errors"

var (
// ErrInvalidCountryCode invalid bik code country
ErrInvalidCountryCode = errors.New("invalid bik country code")
)
3 changes: 3 additions & 0 deletions bik/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bik

const packageName = "bik"
57 changes: 0 additions & 57 deletions errors.go

This file was deleted.

25 changes: 11 additions & 14 deletions inn/inn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"strconv"
"strings"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
"github.com/sshaplygin/ru-doc-code/utils"
)

const (
Expand All @@ -13,8 +14,8 @@ const (
)

type INN struct {
Code ru_doc_code.TaxRegionCode
SerialNumber ru_doc_code.SerialNumber
Code models.TaxRegionCode
SerialNumber models.SerialNumber
Hash10 uint
Hash11 uint
Hash12 uint
Expand All @@ -24,13 +25,9 @@ type INN struct {
// example: input format is 7707083893
func Validate(inn string) (bool, error) {
if len(inn) != lengthLegal && len(inn) != lengthPhysical {
name, err := ru_doc_code.GetModuleName()
if err != nil {
return false, err
}
return false, &ru_doc_code.CommonError{
Method: name,
Err: ru_doc_code.ErrInvalidLength,
return false, &models.CommonError{
Method: packageName,
Err: models.ErrInvalidLength,
}
}

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

// GenerateLegal generate legal type inn string value.
func GenerateLegal() string {
inn := strconv.FormatInt(ru_doc_code.RandomDigits(9), 10)
inn := strconv.FormatInt(utils.RandomDigits(9), 10)
innArr, _ := transformInn(inn)

return inn + strconv.Itoa(hash10(innArr))
}

// GeneratePhysical generate physical type inn string value.
func GeneratePhysical() string {
inn := strconv.FormatInt(ru_doc_code.RandomDigits(10), 10)
inn := strconv.FormatInt(utils.RandomDigits(10), 10)
innArr, _ := transformInn(inn)

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

// Generate generate random type inn string value.
func Generate() string {
if ru_doc_code.RandomDigits(1)%2 == 1 {
if utils.RandomDigits(1)%2 == 1 {
return GeneratePhysical()
}

Expand Down
31 changes: 22 additions & 9 deletions inn/inn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
"github.com/sshaplygin/ru-doc-code/utils"
)

func TestValidate(t *testing.T) {
t.Parallel()

t.Run("invalid inn length", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
type testCase struct {
Code string
IsValid bool
Error error
}

testCases := []testCase{
{
Code: "12345678",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Code: "9876543211123",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Expand Down Expand Up @@ -50,20 +57,26 @@ func TestValidate(t *testing.T) {
})

t.Run("invalid inn value", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
type testCase struct {
Code string
IsValid bool
Error error
}

testCases := []testCase{
{
Code: "77$7083893",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Code: "98754321N123",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Code: "9854132d1123",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Expand Down Expand Up @@ -162,7 +175,7 @@ func TestGenerate(t *testing.T) {
for _, tc := range tests {
tc := tc

digits = ru_doc_code.RandomDigits(tc.len)
digits = utils.RandomDigits(tc.len)
assert.True(t, digits >= tc.min && digits <= tc.max)
}
})
Expand Down
3 changes: 3 additions & 0 deletions inn/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package inn

const packageName = "packageName"
8 changes: 8 additions & 0 deletions kpp/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kpp

import "errors"

var (
// ErrRegistrationReasonCode invalid registration reason code
ErrRegistrationReasonCode = errors.New("invalid registration reason code")
)
Loading

0 comments on commit 43fa2b6

Please sign in to comment.