From c54d17ee30a6d189899128e386ef9031a9d81357 Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 27 Mar 2024 12:51:48 +0500 Subject: [PATCH] Init. --- go.mod | 3 + inns/errors.go | 11 +++ inns/inns_test.go | 80 +++++++++++++++++++ inns/main.go | 193 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 7 ++ 5 files changed, 294 insertions(+) create mode 100644 go.mod create mode 100644 inns/errors.go create mode 100644 inns/inns_test.go create mode 100644 inns/main.go create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..badb8e6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module vultras.su/core/biz + +go 1.21.8 diff --git a/inns/errors.go b/inns/errors.go new file mode 100644 index 0000000..05d5871 --- /dev/null +++ b/inns/errors.go @@ -0,0 +1,11 @@ +package inns + +import ( + "errors" +) + +var ( + IncorrectFormatErr = errors.New("incorrect INN format") + IncorrectLenErr = errors.New("incorrect INN length") + SumsNotMatchErr = errors.New("control sums do not match") +) diff --git a/inns/inns_test.go b/inns/inns_test.go new file mode 100644 index 0000000..0f8d048 --- /dev/null +++ b/inns/inns_test.go @@ -0,0 +1,80 @@ +package inns + +import ( + "testing" +) + +var ( + testStrInns = []string{ + "663802 397223", + "8252 47222169", + "8712530 92145", + "0873 820 72512", + "227 62 3382327", + "886 62 2222010", + "464 350 209157", + "758956582077", + "212551260755", + "0432444 61455", + "428880301819", + "61309 59 33498", + "667344101205", + "6189115998", + "426 1455105", + "562 5340715", + "6588135629", + "253 7102466", + "8195196 357", + "23838 17246", + "812 5976843", + "4710 107563", + "3732 71726", + "2170 439945", + "2193865 820", + "8122 281402", + } + testStrInnsErr = []string{ + "66380a2397223", + "825247222eu169", + "8712530921457553", + "087753382072512", + "22762hetu3382327", + "886ao622222010", + "4643he50209157", + "75895 te 6582077", + "2asn12551260755", + "043abc244461455", + "428880err301819", + "0x613095933498", + "667344101205h", + "618911xerr5998", + "426x1455105", + "562x5340715", + "658813moreerr5629", + "253x7102466", + "8195196x357", + "23check838 17246", + "8check12x5976843", + "4710x107563", + "03732x71726", + "2170x439945", + "2193865x820", + "81shit2 281402", + } +) + +func TestInnFromStr(t *testing.T) { + for _, rawInn := range testStrInns { + _, err := NewFromStr(rawInn) + if err != nil { + t.Fatalf("InnFromStr(%q), err == %q", rawInn, err) + } + } + for _, rawInn := range testStrInnsErr { + _, err := NewFromStr(rawInn) + if err == nil { + t.Fatalf("InnFromStr(%q), err == %v, must be though", rawInn, err) + } + } +} + diff --git a/inns/main.go b/inns/main.go new file mode 100644 index 0000000..850638c --- /dev/null +++ b/inns/main.go @@ -0,0 +1,193 @@ +package inns + +// The package implements the basic +// Russian Federation INN format +// storing it in int64 to keep +// searching and storing as +// quick as possible. + +import ( + "strings" + "unicode" + "strconv" + "fmt" + "slices" +) + +type InnType uint8 +const ( + IncorrectInnType InnType = iota + LegalInnType + IndividualInnType +) + +func removeSpaces(s string) string { + return strings.Map(func(r rune) rune{ + if unicode.IsSpace(r) { + return -1 + } + return r + }, s) +} + +type Inn int64 + +func NewFromInt64(innRaw int64) (Inn, error) { + inn := Inn(innRaw) + if !inn.isCorrectLen() { + return -1, IncorrectLenErr + } + + if !inn.isSumCorrect() { + return -1, SumsNotMatchErr + } + + return inn, nil + +} +// Convert string with spaces into the INN +// and return error if anything is wrong with it. +func NewFromStr(str string) (Inn, error) { + str = removeSpaces(str) + innRaw, err := strconv.ParseInt(str, 10, 64) + if err != nil { + return -1, IncorrectFormatErr + } + return NewFromInt64(innRaw) +} + + +func (inn Inn) Type() InnType { + ln := inn.length() + switch ln { + case 9, 10: + return LegalInnType + case 11, 12: + return IndividualInnType + } + return IncorrectInnType +} + +func (inn Inn) length() int { + ln := 0 + for { + if inn == 0 { + break + } + inn /= 10 + ln++ + } + if ln == 9 || ln == 11 { + ln++ + } + return ln +} + +func (inn Inn) isCorrectLen() bool { + ln := inn.length() + if ln == 9 || ln == 11 { + ln++ + } + return ln == 10 || ln == 12 +} + +var ( + // Koefs for the control sum. + innSum10 = [9]int64{ + 2, 4, 10, 3, 5, 9, 4, 6, 8, + } + innSum11 = [10]int64{ + 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, + } + innSum12 = [11]int64{ + 3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, + } +) + +// Get the integer representation of digits in string INN. +func (inn Inn) digits() []int64 { + ln := inn.length() + v := int64(inn) + ret := make([]int64, ln) + for i := 0 ; i" +} + diff --git a/main.go b/main.go new file mode 100644 index 0000000..cce8563 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +// The package contains +// business related types and functions +// that are used across related applications +// and packages. +