Skip to content

Commit

Permalink
Merge pull request #12 from sshaplygin/kpp-code
Browse files Browse the repository at this point in the history
add: kpp code
  • Loading branch information
sshaplygin authored Jan 6, 2024
2 parents 8decd00 + eb4b514 commit 237ac15
Show file tree
Hide file tree
Showing 32 changed files with 2,901 additions and 480 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.xml
*.csv
*.html
*.txt

.DS_Store

Expand Down
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ It is not production ready public API! It is API could be change it the future.

Status of implementation by code package:

- [x] BIK
- [x] Validate method
- [x] Generate method
- [x] Exists method
- [x] INN
- [x] Generate method
- [x] Validate method
- [ ] KPP
- [ ] Generate method
- [x] Validate method
- [x] OGRN
- [ ] OGRN
- [x] Generate method
- [ ] Validate method
- [ ] OGRNIP
Expand All @@ -37,13 +27,15 @@ Status of implementation by code package:
- [ ] SNILS
- [ ] Generate method
- [x] Validate method
- [ ] Swift
- [ ] SWIFT
- [ ] Generate method
- [ ] Validate method
- [ ] KS
- [ ] Generate method
- [ ] Validate method

Full supported codes: BIK, INN, KPP

## Usage

``` bash
Expand All @@ -57,17 +49,19 @@ go get github.com/sshaplygin/docs-code
import (
"log"

"github.com/sshaplygin/docs-code/inn"
"github.com/sshaplygin/docs-code"
)

...

isValid, err := inn.Validate("526317984689")
isValid, err := docs_code.Validate(docs_code.INN, "526317984689")
if err != nil {
log.Error(err)
log.Error(err)
}
if isValid {
log.Println("INN is valid")
if !isValid {
log.Println("INN is invalid")
} else {
log.Println("INN is valid")
}
```

Expand Down
8 changes: 4 additions & 4 deletions bik/bik.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func Validate(bik string) (bool, error) {
bikData, err := ParseBIK(bik)
if err != nil {
return false, fmt.Errorf("create %s model: %w", packageName, err)
return false, fmt.Errorf("parse %s model: %w", packageName, err)
}

return bikData.IsValid()
Expand All @@ -25,7 +25,7 @@ func Exists(bik string) (bool, error) {

bikData, err := ParseBIK(bik)
if err != nil {
return false, fmt.Errorf("create %s model: %w", packageName, err)
return false, fmt.Errorf("parse %s model: %w", packageName, err)
}

isValid, err := bikData.IsValid()
Expand All @@ -42,6 +42,6 @@ func Exists(bik string) (bool, error) {

// Generate method generate a valid BIK code, but possible usaged or not usaged in reality.
// Method guaranteed that code will be valid, but not guaranteed that code will be exists.
func Generate(opts ...GenerateOpt) string {
return NewBIK(opts...).String()
func Generate() string {
return NewBIK().String()
}
4 changes: 2 additions & 2 deletions bik/bik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ 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))
require.True(t, isValid)

assert.True(t, isValid)
}

func Test_Exists(t *testing.T) {
Expand Down
165 changes: 83 additions & 82 deletions bik/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
)

type (
// CountryCode Required length 2.
// CountryCode required length 2.
CountryCode int

// UnitConditionalNumber required length 2.
Expand All @@ -62,40 +62,74 @@ type (
// or the conditional number of the structural division of the Bank of Russia.
UnitConditionalNumber int

// LastAccountNumbers required length 3. It is last correspondent account of the bank. Possible values [050, 999]
// LastAccountNumbers required length 3.
// It is last correspondent account of the bank. Possible values [050, 999]
LastAccountNumbers int
)

const codeLength = 9
func (cc CountryCode) IsValid() bool {
if cc < minCountryCodeLength || cc > maxCountryCodeLength {
return false
}

type BIKStruct struct {
country CountryCode
territoryCode okato.StateCode
unitNumber UnitConditionalNumber
lastNumber LastAccountNumbers
_, ok := supportedCountryCodes[cc]
return ok
}

// generateOptions TODO
type generateOptions struct {
func (cc CountryCode) String() string {
_, ok := supportedCountryCodes[cc]
if !ok {
return RussiaCountryCode.String()
}

return utils.StrCode(int(cc), countryCodeLength)
}

type GenerateOpt func(options *generateOptions)
func (cc CountryCode) GetName() string {
codeName, ok := supportedCountryCodes[cc]
if !ok {
return unspecifiedCountryCode
}

func NewBIK(opts ...GenerateOpt) *BIKStruct {
var options generateOptions
return codeName
}

for _, o := range opts {
o(&options)
}
func GenerateCountryCode() CountryCode {
return countryCodes[utils.Random(0, len(countryCodes)-1)]
}

return &BIKStruct{
country: GenerateCountryCode(),
territoryCode: okato.GenerateStateCode(),
unitNumber: GenerateUnitConditionalNumber(),
lastNumber: GenerateLastAccountNumbers(),
func (ucn UnitConditionalNumber) IsValid() bool {
return ucn >= minUnitConditionalNumber && ucn <= maxUnitConditionalNumber
}

func (ucn UnitConditionalNumber) String() string {
return utils.StrCode(int(ucn), unitConditionalNumberLength)
}

func GenerateUnitConditionalNumber() UnitConditionalNumber {
return UnitConditionalNumber(utils.Random(minUnitConditionalNumber, maxUnitConditionalNumber))
}

const specialCode = 12

func (lan LastAccountNumbers) IsValid() bool {
if lan == specialCode {
return true
}

return lan >= minLastAccountNumbers && lan <= maxLastAccountNumbers
}

func (lan LastAccountNumbers) String() string {
return utils.StrCode(int(lan), lastAccountNumbersLength)
}

func GenerateLastAccountNumbers() LastAccountNumbers {
return LastAccountNumbers(utils.Random(minLastAccountNumbers, maxLastAccountNumbers))
}

const codeLength = 9

func ParseBIK(bik string) (*BIKStruct, error) {
if len(bik) != codeLength {
return nil, &models.CommonError{
Expand All @@ -117,6 +151,34 @@ func ParseBIK(bik string) (*BIKStruct, error) {
}, nil
}

type BIKStruct struct {
country CountryCode
territoryCode okato.StateCode
unitNumber UnitConditionalNumber
lastNumber LastAccountNumbers
}

// generateOptions TODO
type generateOptions struct {
}

type GenerateOpt func(options *generateOptions)

func NewBIK(opts ...GenerateOpt) *BIKStruct {
var options generateOptions

for _, o := range opts {
o(&options)
}

return &BIKStruct{
country: GenerateCountryCode(),
territoryCode: okato.GenerateStateCode(),
unitNumber: GenerateUnitConditionalNumber(),
lastNumber: GenerateLastAccountNumbers(),
}
}

func (bs *BIKStruct) IsValid() (bool, error) {
if bs == nil {
return false, ErrNilBIK
Expand Down Expand Up @@ -161,64 +223,3 @@ func (bs *BIKStruct) Exists() (bool, error) {
_, ok := existsBIKs[bs.String()]
return ok, nil
}

func GenerateCountryCode() CountryCode {
return countryCodes[utils.Random(0, len(countryCodes)-1)]
}

func GenerateUnitConditionalNumber() UnitConditionalNumber {
return UnitConditionalNumber(utils.Random(minUnitConditionalNumber, maxUnitConditionalNumber))
}

func GenerateLastAccountNumbers() LastAccountNumbers {
return LastAccountNumbers(utils.Random(minLastAccountNumbers, maxLastAccountNumbers))
}

func (cc CountryCode) IsValid() bool {
if cc < minCountryCodeLength || cc > maxCountryCodeLength {
return false
}

_, ok := supportedCountryCodes[cc]
return ok
}

func (cc CountryCode) String() string {
_, ok := supportedCountryCodes[cc]
if !ok {
return RussiaCountryCode.String()
}

return utils.StrCode(int(cc), countryCodeLength)
}

func (cc CountryCode) GetName() string {
codeName, ok := supportedCountryCodes[cc]
if !ok {
return unspecifiedCountryCode
}

return codeName
}

func (ucn UnitConditionalNumber) IsValid() bool {
return ucn >= minUnitConditionalNumber && ucn <= maxUnitConditionalNumber
}

func (ucn UnitConditionalNumber) String() string {
return utils.StrCode(int(ucn), unitConditionalNumberLength)
}

const specialCode = 12

func (lan LastAccountNumbers) IsValid() bool {
if lan == specialCode {
return true
}

return lan >= minLastAccountNumbers && lan <= maxLastAccountNumbers
}

func (lan LastAccountNumbers) String() string {
return utils.StrCode(int(lan), lastAccountNumbersLength)
}
Loading

0 comments on commit 237ac15

Please sign in to comment.