@@ -11,38 +11,46 @@ import (
1111
1212const packageName = "bik"
1313
14+ const validateErrorTmpl = "%w: %s"
15+
1416const (
15- maxCountryCodeLength = 99
16- minInitConditionalNumber = 0
17- maxInitConditionalNumber = 99
18- minLastAccountNumbers = 50
19- maxLastAccountNumbers = 999
17+ countryCodeLength = 2
18+
19+ minCountryCodeLength = 0
20+ maxCountryCodeLength = 99
21+ )
22+
23+ const (
24+ unitConditionalNumberLength = 2
25+
26+ minUnitConditionalNumber = 0
27+ maxUnitConditionalNumber = 99
28+ )
29+
30+ const (
31+ lastAccountNumbersLength = 3
32+
33+ minLastAccountNumbers = 50
34+ maxLastAccountNumbers = 999
2035)
2136
2237const (
2338 unspecifiedCountryCode = "Неопределенный код страны"
2439)
2540
2641var (
27- // directParticipationCounty - участник платежной системы с прямым участием
28- directParticipationCounty CountryCode = 0
42+ // DirectParticipationCounty - участник платежной системы с прямым участием
43+ DirectParticipationCounty CountryCode = 0
2944
30- // indirectParticipationCounty - участник платежной системы с косвенным участием
31- indirectParticipationCounty CountryCode = 1
45+ // IndirectParticipationCounty - участник платежной системы с косвенным участием
46+ IndirectParticipationCounty CountryCode = 1
3247
33- // notMemberClientCBRF - клиент Банка России, не являющийся участником платежной системы
34- notMemberClientCBRF CountryCode = 2
48+ // NotMemberClientCBRF - клиент Банка России, не являющийся участником платежной системы
49+ NotMemberClientCBRF CountryCode = 2
3550
36- russiaCountryCode CountryCode = 4
51+ RussiaCountryCode CountryCode = 4
3752)
3853
39- var supportedCountryCodes = map [CountryCode ]string {
40- directParticipationCounty : "Участник платежной системы с прямым участием" ,
41- indirectParticipationCounty : "Участник платежной системы с косвенным участием" ,
42- notMemberClientCBRF : "Клиент Банка России, не являющийся участником платежной системы" ,
43- russiaCountryCode : "Код Российской Федерации" ,
44- }
45-
4654type (
4755 // CountryCode Required length 2.
4856 CountryCode int
@@ -67,7 +75,19 @@ type BIKStruct struct {
6775 lastNumber LastAccountNumbers
6876}
6977
70- func NewBIK () * BIKStruct {
78+ // generateOptions TODO
79+ type generateOptions struct {
80+ }
81+
82+ type GenerateOpt func (options * generateOptions )
83+
84+ func NewBIK (opts ... GenerateOpt ) * BIKStruct {
85+ var options generateOptions
86+
87+ for _ , o := range opts {
88+ o (& options )
89+ }
90+
7191 return & BIKStruct {
7292 country : GenerateCountryCode (),
7393 territoryCode : okato .GenerateStateCode (),
@@ -103,19 +123,19 @@ func (bs *BIKStruct) IsValid() (bool, error) {
103123 }
104124
105125 if ! bs .country .IsValid () {
106- return false , ErrInvalidCountryCode
126+ return false , fmt . Errorf ( validateErrorTmpl , ErrInvalidCountryCode , bs . country )
107127 }
108128
109129 if ! bs .territoryCode .IsValid () {
110- return false , ErrInvalidTerritoryCode
130+ return false , fmt . Errorf ( validateErrorTmpl , ErrInvalidTerritoryCode , bs . territoryCode )
111131 }
112132
113133 if ! bs .unitNumber .IsValid () {
114- return false , ErrInvalidUnitConditionalNumber
134+ return false , fmt . Errorf ( validateErrorTmpl , ErrInvalidUnitConditionalNumber , bs . unitNumber )
115135 }
116136
117137 if ! bs .lastNumber .IsValid () {
118- return false , ErrInvalidLastAccountNumbers
138+ return false , fmt . Errorf ( validateErrorTmpl , ErrInvalidLastAccountNumbers , bs . lastNumber )
119139 }
120140
121141 return true , nil
@@ -125,10 +145,10 @@ func (bs *BIKStruct) String() string {
125145 var res strings.Builder
126146 res .Grow (codeLength )
127147
128- res .WriteString (bs .country .ToString ())
129- res .WriteString (bs .territoryCode .ToString ())
130- res .WriteString (bs .unitNumber .ToString ())
131- res .WriteString (bs .lastNumber .ToString ())
148+ res .WriteString (bs .country .String ())
149+ res .WriteString (bs .territoryCode .String ())
150+ res .WriteString (bs .unitNumber .String ())
151+ res .WriteString (bs .lastNumber .String ())
132152
133153 return res .String ()
134154}
@@ -143,47 +163,50 @@ func (bs *BIKStruct) Exists() (bool, error) {
143163}
144164
145165func GenerateCountryCode () CountryCode {
146- // len(supportedCountryCodes)
147- return russiaCountryCode
166+ return countryCodes [utils .Random (0 , len (countryCodes )- 1 )]
148167}
149168
150169func GenerateUnitConditionalNumber () UnitConditionalNumber {
151- return 0
170+ return UnitConditionalNumber ( utils . Random ( minUnitConditionalNumber , maxUnitConditionalNumber ))
152171}
153172
154173func GenerateLastAccountNumbers () LastAccountNumbers {
155- return 0
174+ return LastAccountNumbers ( utils . Random ( minLastAccountNumbers , maxLastAccountNumbers ))
156175}
157176
158177func (cc CountryCode ) IsValid () bool {
159- if cc > maxCountryCodeLength {
178+ if cc < minCountryCodeLength || cc > maxCountryCodeLength {
160179 return false
161180 }
162181
163182 _ , ok := supportedCountryCodes [cc ]
164-
165183 return ok
166184}
167185
168186func (cc CountryCode ) String () string {
169- res , ok := supportedCountryCodes [cc ]
187+ _ , ok := supportedCountryCodes [cc ]
170188 if ! ok {
171- return unspecifiedCountryCode
189+ return RussiaCountryCode . String ()
172190 }
173191
174- return res
192+ return utils . StrCode ( int ( cc ), countryCodeLength )
175193}
176194
177- func (cc CountryCode ) ToString () string {
178- return ""
195+ func (cc CountryCode ) GetName () string {
196+ codeName , ok := supportedCountryCodes [cc ]
197+ if ! ok {
198+ return unspecifiedCountryCode
199+ }
200+
201+ return codeName
179202}
180203
181204func (ucn UnitConditionalNumber ) IsValid () bool {
182- return ucn >= minInitConditionalNumber && ucn <= maxInitConditionalNumber
205+ return ucn >= minUnitConditionalNumber && ucn <= maxUnitConditionalNumber
183206}
184207
185- func (ucn UnitConditionalNumber ) ToString () string {
186- return ""
208+ func (ucn UnitConditionalNumber ) String () string {
209+ return utils . StrCode ( int ( ucn ), unitConditionalNumberLength )
187210}
188211
189212const specialCode = 12
@@ -196,6 +219,6 @@ func (lan LastAccountNumbers) IsValid() bool {
196219 return lan >= minLastAccountNumbers && lan <= maxLastAccountNumbers
197220}
198221
199- func (lan LastAccountNumbers ) ToString () string {
200- return ""
222+ func (lan LastAccountNumbers ) String () string {
223+ return utils . StrCode ( int ( lan ), lastAccountNumbersLength )
201224}
0 commit comments