From 0cd1dd1c990e12bb4a2aec7c758811375ccc3198 Mon Sep 17 00:00:00 2001 From: LTNs35 Date: Sun, 24 Oct 2021 11:18:25 +0200 Subject: [PATCH] Refactor and bugs fixed. --- countries/{albania => }/albania.go | 18 +- countries/{andorra => }/andorra.go | 18 +- countries/argentina.go | 29 +++ countries/argentina/argentina.go | 33 ---- countries/{austria => }/austria.go | 15 +- countries/belarus.go | 29 +++ countries/belarus/belarus.go | 33 ---- countries/{belgium => }/belgium.go | 15 +- countries/bolivia.go | 29 +++ countries/bolivia/bolivia.go | 33 ---- countries/{brazil => }/brazil.go | 20 +- countries/{bulgaria => }/bulgaria.go | 15 +- countries/{canada => }/canada.go | 16 +- countries/country.go | 2 +- countries/{croatia => }/croatia.go | 15 +- countries/{cyprus => }/cyprus.go | 15 +- .../{czech_republic => }/czech_republic.go | 15 +- countries/{denmark => }/denmark.go | 15 +- countries/{estonia => }/estonia.go | 15 +- countries/{finland => }/finland.go | 15 +- countries/{france => }/france.go | 16 +- countries/{germany => }/germany.go | 15 +- countries/{greece => }/greece.go | 15 +- countries/hong_kong.go | 29 +++ countries/hong_kong/hong_kong.go | 33 ---- countries/{hungary => }/hungary.go | 15 +- countries/{ireland => }/ireland.go | 15 +- countries/{italy => }/italy.go | 15 +- countries/kazakhstan.go | 29 +++ countries/kazakhstan/kazakhstan.go | 33 ---- countries/{latvia => }/latvia.go | 15 +- countries/liechtenstein.go | 29 +++ countries/liechtenstein/liechtenstein.go | 33 ---- countries/{lithuania => }/lithuania.go | 31 ++-- countries/{luxembourg => }/luxembourg.go | 18 +- countries/{malta => }/malta.go | 15 +- countries/{netherlands => }/netherlands.go | 15 +- countries/north_macedonia.go | 29 +++ countries/north_macedonia/north_macedonia.go | 33 ---- countries/{norway => }/norway.go | 15 +- countries/peru.go | 29 +++ countries/peru/peru.go | 33 ---- countries/{poland => }/poland.go | 15 +- countries/{portugal => }/portugal.go | 16 +- countries/{romania => }/romania.go | 15 +- countries/{russia => }/russia.go | 19 +- countries/san_marino.go | 29 +++ countries/san_marino/san_marino.go | 33 ---- countries/{serbia => }/serbia.go | 15 +- countries/{slovakia => }/slovakia.go | 16 +- countries/{slovenia => }/slovenia.go | 15 +- countries/{spain => }/spain.go | 15 +- countries/{sweden => }/sweden.go | 15 +- countries/{switzerland => }/switzerland.go | 15 +- countries/turkey.go | 29 +++ countries/turkey/turkey.go | 33 ---- countries/ukraine.go | 29 +++ countries/ukraine/ukraine.go | 33 ---- .../{united_kingdom => }/united_kingdom.go | 15 +- types.go | 8 +- validator.go | 167 ++++++----------- validator_test.go | 173 ++++++++---------- 62 files changed, 726 insertions(+), 884 deletions(-) rename countries/{albania => }/albania.go (50%) rename countries/{andorra => }/andorra.go (54%) create mode 100644 countries/argentina.go delete mode 100644 countries/argentina/argentina.go rename countries/{austria => }/austria.go (77%) create mode 100644 countries/belarus.go delete mode 100644 countries/belarus/belarus.go rename countries/{belgium => }/belgium.go (74%) create mode 100644 countries/bolivia.go delete mode 100644 countries/bolivia/bolivia.go rename countries/{brazil => }/brazil.go (80%) rename countries/{bulgaria => }/bulgaria.go (93%) rename countries/{canada => }/canada.go (85%) rename countries/{croatia => }/croatia.go (75%) rename countries/{cyprus => }/cyprus.go (84%) rename countries/{czech_republic => }/czech_republic.go (92%) rename countries/{denmark => }/denmark.go (70%) rename countries/{estonia => }/estonia.go (78%) rename countries/{finland => }/finland.go (77%) rename countries/{france => }/france.go (79%) rename countries/{germany => }/germany.go (79%) rename countries/{greece => }/greece.go (80%) create mode 100644 countries/hong_kong.go delete mode 100644 countries/hong_kong/hong_kong.go rename countries/{hungary => }/hungary.go (78%) rename countries/{ireland => }/ireland.go (89%) rename countries/{italy => }/italy.go (85%) create mode 100644 countries/kazakhstan.go delete mode 100644 countries/kazakhstan/kazakhstan.go rename countries/{latvia => }/latvia.go (86%) create mode 100644 countries/liechtenstein.go delete mode 100644 countries/liechtenstein/liechtenstein.go rename countries/{lithuania => }/lithuania.go (80%) rename countries/{luxembourg => }/luxembourg.go (60%) rename countries/{malta => }/malta.go (78%) rename countries/{netherlands => }/netherlands.go (90%) create mode 100644 countries/north_macedonia.go delete mode 100644 countries/north_macedonia/north_macedonia.go rename countries/{norway => }/norway.go (81%) create mode 100644 countries/peru.go delete mode 100644 countries/peru/peru.go rename countries/{poland => }/poland.go (79%) rename countries/{portugal => }/portugal.go (79%) rename countries/{romania => }/romania.go (80%) rename countries/{russia => }/russia.go (83%) create mode 100644 countries/san_marino.go delete mode 100644 countries/san_marino/san_marino.go rename countries/{serbia => }/serbia.go (76%) rename countries/{slovakia => }/slovakia.go (60%) rename countries/{slovenia => }/slovenia.go (79%) rename countries/{spain => }/spain.go (94%) rename countries/{sweden => }/sweden.go (78%) rename countries/{switzerland => }/switzerland.go (77%) create mode 100644 countries/turkey.go delete mode 100644 countries/turkey/turkey.go create mode 100644 countries/ukraine.go delete mode 100644 countries/ukraine/ukraine.go rename countries/{united_kingdom => }/united_kingdom.go (91%) diff --git a/countries/albania/albania.go b/countries/albania.go similarity index 50% rename from countries/albania/albania.go rename to countries/albania.go index ef49e61..dd51e8f 100644 --- a/countries/albania/albania.go +++ b/countries/albania.go @@ -1,22 +1,18 @@ -package albania - -import ( - "github.com/ltns35/go-vat/countries" -) +package countries type albania struct { - countries.Country + Country } -var VAT = albania{ - Country: countries.Country{ +var Albania = albania{ + Country: Country{ Name: "Albania", Codes: []string{ "AL", "ALB", "008", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(AL)([JKL]\\d{8}[A-Z])$", }, @@ -28,6 +24,6 @@ func (a albania) Calc(vat string) bool { return len(vat) == 10 } -func (a albania) GetCountry() *countries.Country { - return &a.Country +func (a albania) GetCountry() Country { + return a.Country } diff --git a/countries/andorra/andorra.go b/countries/andorra.go similarity index 54% rename from countries/andorra/andorra.go rename to countries/andorra.go index b1dae63..f340a19 100644 --- a/countries/andorra/andorra.go +++ b/countries/andorra.go @@ -1,22 +1,18 @@ -package andorra - -import ( - "github.com/ltns35/go-vat/countries" -) +package countries type andorra struct { - countries.Country + Country } -var VAT = andorra{ - Country: countries.Country{ +var Andorra = andorra{ + Country: Country{ Name: "Andorra", Codes: []string{ "AD", "AND", "020", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(AD)([fealecdgopuFEALECDGOPU]{1}\\d{6}[fealecdgopuFEALECDGOPU]{1})$", }, @@ -28,6 +24,6 @@ func (a andorra) Calc(vat string) bool { return len(vat) == 8 } -func (a andorra) GetCountry() *countries.Country { - return &a.Country +func (a andorra) GetCountry() Country { + return a.Country } diff --git a/countries/argentina.go b/countries/argentina.go new file mode 100644 index 0000000..741debb --- /dev/null +++ b/countries/argentina.go @@ -0,0 +1,29 @@ +package countries + +type argentina struct { + Country +} + +var Argentina = argentina{ + Country: Country{ + Name: "Argentina", + Codes: []string{ + "AR", + "ARG", + "032", + }, + Rules: CountryRules{ + Regex: []string{ + "^(AR)(\\d{11})$", + }, + }, + }, +} + +func (a argentina) Calc(vat string) bool { + return len(vat) == 11 +} + +func (a argentina) GetCountry() Country { + return a.Country +} diff --git a/countries/argentina/argentina.go b/countries/argentina/argentina.go deleted file mode 100644 index c4a1650..0000000 --- a/countries/argentina/argentina.go +++ /dev/null @@ -1,33 +0,0 @@ -package argentina - -import ( - "github.com/ltns35/go-vat/countries" -) - -type argentina struct { - countries.Country -} - -var VAT = argentina{ - Country: countries.Country{ - Name: "Argentina", - Codes: []string{ - "AR", - "ARG", - "032", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(AR)(\\d{11})$", - }, - }, - }, -} - -func (a argentina) Calc(vat string) bool { - return len(vat) == 11 -} - -func (a argentina) GetCountry() *countries.Country { - return &a.Country -} diff --git a/countries/austria/austria.go b/countries/austria.go similarity index 77% rename from countries/austria/austria.go rename to countries/austria.go index 79b05c6..17334c0 100644 --- a/countries/austria/austria.go +++ b/countries/austria.go @@ -1,25 +1,24 @@ -package austria +package countries import ( "math" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type austria struct { - countries.Country + Country } -var VAT = austria{ - Country: countries.Country{ +var Austria = austria{ + Country: Country{ Name: "Austria", Codes: []string{ "AT", "AUT", "040", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 1, @@ -65,6 +64,6 @@ func (a austria) Calc(vat string) bool { return int(total) == lastNum } -func (a austria) GetCountry() *countries.Country { - return &a.Country +func (a austria) GetCountry() Country { + return a.Country } diff --git a/countries/belarus.go b/countries/belarus.go new file mode 100644 index 0000000..30a7f0d --- /dev/null +++ b/countries/belarus.go @@ -0,0 +1,29 @@ +package countries + +type belarus struct { + Country +} + +var Belarus = belarus{ + Country: Country{ + Name: "Belarus", + Codes: []string{ + "BY", + "BLR", + "112", + }, + Rules: CountryRules{ + Regex: []string{ + "^(BY)(\\d{9})$", + }, + }, + }, +} + +func (b belarus) Calc(vat string) bool { + return len(vat) == 9 +} + +func (b belarus) GetCountry() Country { + return b.Country +} diff --git a/countries/belarus/belarus.go b/countries/belarus/belarus.go deleted file mode 100644 index d4d831d..0000000 --- a/countries/belarus/belarus.go +++ /dev/null @@ -1,33 +0,0 @@ -package belarus - -import ( - "github.com/ltns35/go-vat/countries" -) - -type belarus struct { - countries.Country -} - -var VAT = belarus{ - Country: countries.Country{ - Name: "Belarus", - Codes: []string{ - "BY", - "BLR", - "112", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(BY)(\\d{9})$", - }, - }, - }, -} - -func (b belarus) Calc(vat string) bool { - return len(vat) == 9 -} - -func (b belarus) GetCountry() *countries.Country { - return &b.Country -} diff --git a/countries/belgium/belgium.go b/countries/belgium.go similarity index 74% rename from countries/belgium/belgium.go rename to countries/belgium.go index 4d59ddf..5a47bcd 100644 --- a/countries/belgium/belgium.go +++ b/countries/belgium.go @@ -1,26 +1,25 @@ -package belgium +package countries import ( "math" "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type belgium struct { - countries.Country + Country } -var VAT = belgium{ - Country: countries.Country{ +var Belgium = belgium{ + Country: Country{ Name: "Belgium", Codes: []string{ "BE", "BEL", "056", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(BE)(0?\\d{9})$", }, @@ -51,6 +50,6 @@ func (b belgium) Calc(vat string) bool { return check == float64(lastDigits) } -func (b belgium) GetCountry() *countries.Country { - return &b.Country +func (b belgium) GetCountry() Country { + return b.Country } diff --git a/countries/bolivia.go b/countries/bolivia.go new file mode 100644 index 0000000..f4041e7 --- /dev/null +++ b/countries/bolivia.go @@ -0,0 +1,29 @@ +package countries + +type bolivia struct { + Country +} + +var Bolivia = bolivia{ + Country: Country{ + Name: "Bolivia", + Codes: []string{ + "BO", + "BOL", + "068", + }, + Rules: CountryRules{ + Regex: []string{ + "^(BO)(\\d{7})$", + }, + }, + }, +} + +func (b bolivia) Calc(vat string) bool { + return len(vat) == 7 +} + +func (b bolivia) GetCountry() Country { + return b.Country +} diff --git a/countries/bolivia/bolivia.go b/countries/bolivia/bolivia.go deleted file mode 100644 index 3ae064a..0000000 --- a/countries/bolivia/bolivia.go +++ /dev/null @@ -1,33 +0,0 @@ -package bolivia - -import ( - "github.com/ltns35/go-vat/countries" -) - -type bolivia struct { - countries.Country -} - -var VAT = bolivia{ - Country: countries.Country{ - Name: "Bolivia", - Codes: []string{ - "BO", - "BOL", - "068", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(BO)(\\d{7})$", - }, - }, - }, -} - -func (b bolivia) Calc(vat string) bool { - return len(vat) == 7 -} - -func (b bolivia) GetCountry() *countries.Country { - return &b.Country -} diff --git a/countries/brazil/brazil.go b/countries/brazil.go similarity index 80% rename from countries/brazil/brazil.go rename to countries/brazil.go index 39b9101..fa0b72c 100644 --- a/countries/brazil/brazil.go +++ b/countries/brazil.go @@ -1,25 +1,23 @@ -package brazil +package countries import ( "strconv" "strings" - - "github.com/ltns35/go-vat/countries" ) type brazil struct { - countries.Country + Country } -var VAT = brazil{ - Country: countries.Country{ +var Brazil = brazil{ + Country: Country{ Name: "Brazil", Codes: []string{ "BR", "BRA", "076", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(BR)?(\\d{14}|\\d{2}\\.\\d{3}\\.\\d{3}\\/\\d{4}-\\d{2})$", }, @@ -64,11 +62,11 @@ func (b brazil) Calc(vat string) bool { checkers := generateCheckSums(numbers, validators) - return numbers[12] == getRemaining(checkers[0]) && numbers[13] == getRemaining(checkers[1]) + return numbers[12] == remaining(checkers[0]) && numbers[13] == remaining(checkers[1]) } -func (b brazil) GetCountry() *countries.Country { - return &b.Country +func (b brazil) GetCountry() Country { + return b.Country } // Generate check sums. Multiply numbers to validators and sum them to generate @@ -107,7 +105,7 @@ func isRepeatedArray(numbers []int) bool { return true } -func getRemaining(num int) int { +func remaining(num int) int { if num%11 < 2 { return 0 diff --git a/countries/bulgaria/bulgaria.go b/countries/bulgaria.go similarity index 93% rename from countries/bulgaria/bulgaria.go rename to countries/bulgaria.go index ad1bfa5..33ec38e 100644 --- a/countries/bulgaria/bulgaria.go +++ b/countries/bulgaria.go @@ -1,27 +1,26 @@ -package bulgaria +package countries import ( "math" "regexp" "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type bulgaria struct { - countries.Country + Country } -var VAT = bulgaria{ - Country: countries.Country{ +var Bulgaria = bulgaria{ + Country: Country{ Name: "Bulgaria", Codes: []string{ "BG", "BGR", "100", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "physical": { 2, @@ -75,8 +74,8 @@ func (b bulgaria) Calc(vat string) bool { miscellaneousVAT(vat, b.Rules.Multipliers) } -func (b bulgaria) GetCountry() *countries.Country { - return &b.Country +func (b bulgaria) GetCountry() Country { + return b.Country } func increase(value int, vat string, from int, to int, incr int) int { diff --git a/countries/canada/canada.go b/countries/canada.go similarity index 85% rename from countries/canada/canada.go rename to countries/canada.go index c9d84d6..b97080d 100644 --- a/countries/canada/canada.go +++ b/countries/canada.go @@ -1,25 +1,23 @@ -package canada +package countries import ( "fmt" "regexp" - - "github.com/ltns35/go-vat/countries" ) type canada struct { - countries.Country + Country } -var VAT = canada{ - Country: countries.Country{ +var Canada = canada{ + Country: Country{ Name: "Canada", Codes: []string{ "CA", "CAN", "124", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(CA)(\\d{7})$", "^(CA)(\\d{9})$", @@ -43,8 +41,8 @@ func (c canada) Calc(vat string) bool { c.isQSTQuebec(vat) } -func (c canada) GetCountry() *countries.Country { - return &c.Country +func (c canada) GetCountry() Country { + return c.Country } func (c canada) isBusinessName(vat string) bool { diff --git a/countries/country.go b/countries/country.go index 94f6dad..265dd58 100644 --- a/countries/country.go +++ b/countries/country.go @@ -8,7 +8,7 @@ type Country struct { type Calculer interface { Calc(vat string) bool - GetCountry() *Country + GetCountry() Country } type CountryRules struct { diff --git a/countries/croatia/croatia.go b/countries/croatia.go similarity index 75% rename from countries/croatia/croatia.go rename to countries/croatia.go index 2584556..9f72307 100644 --- a/countries/croatia/croatia.go +++ b/countries/croatia.go @@ -1,23 +1,22 @@ -package croatia +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type croatia struct { - countries.Country + Country } -var VAT = croatia{ - Country: countries.Country{ +var Croatia = croatia{ + Country: Country{ Name: "Croatia", Codes: []string{ "HR", "HRV", "191", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(HR)(\\d{11})$", }, @@ -49,6 +48,6 @@ func (c croatia) Calc(vat string) bool { return (product+expect)%10 == 1 } -func (c croatia) GetCountry() *countries.Country { - return &c.Country +func (c croatia) GetCountry() Country { + return c.Country } diff --git a/countries/cyprus/cyprus.go b/countries/cyprus.go similarity index 84% rename from countries/cyprus/cyprus.go rename to countries/cyprus.go index f2eaa36..6aa8a36 100644 --- a/countries/cyprus/cyprus.go +++ b/countries/cyprus.go @@ -1,25 +1,24 @@ -package cyprus +package countries import ( "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type cyprus struct { - countries.Country + Country } -var VAT = cyprus{ - Country: countries.Country{ +var Cyprus = cyprus{ + Country: Country{ Name: "Cyprus", Codes: []string{ "CY", "CYP", "196", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(CY)([0-59]\\d{7}[A-Z])$", }, @@ -50,8 +49,8 @@ func (c cyprus) Calc(vat string) bool { return totalChar == expect } -func (c cyprus) GetCountry() *countries.Country { - return &c.Country +func (c cyprus) GetCountry() Country { + return c.Country } func extractAndMultiplyByCounter(vat string, total int) int { diff --git a/countries/czech_republic/czech_republic.go b/countries/czech_republic.go similarity index 92% rename from countries/czech_republic/czech_republic.go rename to countries/czech_republic.go index 81b8588..29746b1 100644 --- a/countries/czech_republic/czech_republic.go +++ b/countries/czech_republic.go @@ -1,27 +1,26 @@ -package czech_republic +package countries import ( "math" "regexp" "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type czechRepublic struct { - countries.Country + Country } -var VAT = czechRepublic{ - Country: countries.Country{ +var CzechRepublic = czechRepublic{ + Country: Country{ Name: "Czech Republic", Codes: []string{ "CZ", "CZE", "203", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 8, @@ -72,8 +71,8 @@ func (c czechRepublic) Calc(vat string) bool { } -func (c czechRepublic) GetCountry() *countries.Country { - return &c.Country +func (c czechRepublic) GetCountry() Country { + return c.Country } func isLegalEntities(vat string, multipliers []int, additional string) bool { diff --git a/countries/denmark/denmark.go b/countries/denmark.go similarity index 70% rename from countries/denmark/denmark.go rename to countries/denmark.go index 567b976..e987f98 100644 --- a/countries/denmark/denmark.go +++ b/countries/denmark.go @@ -1,23 +1,22 @@ -package denmark +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type denmark struct { - countries.Country + Country } -var VAT = denmark{ - Country: countries.Country{ +var Denmark = denmark{ + Country: Country{ Name: "Denmark", Codes: []string{ "DK", "DNK", "208", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 2, @@ -49,6 +48,6 @@ func (d denmark) Calc(vat string) bool { return total%11 == 0 } -func (d denmark) GetCountry() *countries.Country { - return &d.Country +func (d denmark) GetCountry() Country { + return d.Country } diff --git a/countries/estonia/estonia.go b/countries/estonia.go similarity index 78% rename from countries/estonia/estonia.go rename to countries/estonia.go index 25762d2..74917c6 100644 --- a/countries/estonia/estonia.go +++ b/countries/estonia.go @@ -1,23 +1,22 @@ -package estonia +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type estonia struct { - countries.Country + Country } -var VAT = estonia{ - Country: countries.Country{ +var Estonia = estonia{ + Country: Country{ Name: "Estonia", Codes: []string{ "EE", "EST", "233", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 3, @@ -58,6 +57,6 @@ func (e estonia) Calc(vat string) bool { return total == expect } -func (e estonia) GetCountry() *countries.Country { - return &e.Country +func (e estonia) GetCountry() Country { + return e.Country } diff --git a/countries/finland/finland.go b/countries/finland.go similarity index 77% rename from countries/finland/finland.go rename to countries/finland.go index 449dd26..d99c41a 100644 --- a/countries/finland/finland.go +++ b/countries/finland.go @@ -1,23 +1,22 @@ -package finland +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type finland struct { - countries.Country + Country } -var VAT = finland{ - Country: countries.Country{ +var Finland = finland{ + Country: Country{ Name: "Finland", Codes: []string{ "FI", "FIN", "246", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 7, @@ -58,6 +57,6 @@ func (f finland) Calc(vat string) bool { return total == expect } -func (f finland) GetCountry() *countries.Country { - return &f.Country +func (f finland) GetCountry() Country { + return f.Country } diff --git a/countries/france/france.go b/countries/france.go similarity index 79% rename from countries/france/france.go rename to countries/france.go index e0d6777..2b4e988 100644 --- a/countries/france/france.go +++ b/countries/france.go @@ -1,25 +1,23 @@ -package france +package countries import ( "regexp" "strconv" - - "github.com/ltns35/go-vat/countries" ) type france struct { - countries.Country + Country } -var VAT = france{ - Country: countries.Country{ +var France = france{ + Country: Country{ Name: "France", Codes: []string{ "FR", "FRA", "250", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(FR)(\\d{11})$", "^(FR)([A-HJ-NP-Z]\\d{10})$", @@ -54,6 +52,6 @@ func (f france) Calc(vat string) bool { return total == expect } -func (f france) GetCountry() *countries.Country { - return &f.Country +func (f france) GetCountry() Country { + return f.Country } diff --git a/countries/germany/germany.go b/countries/germany.go similarity index 79% rename from countries/germany/germany.go rename to countries/germany.go index 1680c5b..03364aa 100644 --- a/countries/germany/germany.go +++ b/countries/germany.go @@ -1,25 +1,24 @@ -package germany +package countries import ( "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type germany struct { - countries.Country + Country } -var VAT = germany{ - Country: countries.Country{ +var Germany = germany{ + Country: Country{ Name: "Germany", Codes: []string{ "DE", "DEU", "276", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(DE)([1-9]\\d{8})$", }, @@ -61,6 +60,6 @@ func (g germany) Calc(vat string) bool { return checkDigit == expect } -func (g germany) GetCountry() *countries.Country { - return &g.Country +func (g germany) GetCountry() Country { + return g.Country } diff --git a/countries/greece/greece.go b/countries/greece.go similarity index 80% rename from countries/greece/greece.go rename to countries/greece.go index f3aa43b..6144022 100644 --- a/countries/greece/greece.go +++ b/countries/greece.go @@ -1,23 +1,22 @@ -package greece +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type greece struct { - countries.Country + Country } -var VAT = greece{ - Country: countries.Country{ +var Greece = greece{ + Country: Country{ Name: "Greece", Codes: []string{ "GR", "GRC", "300", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 256, @@ -67,6 +66,6 @@ func (g greece) Calc(vat string) bool { return total == expect } -func (g greece) GetCountry() *countries.Country { - return &g.Country +func (g greece) GetCountry() Country { + return g.Country } diff --git a/countries/hong_kong.go b/countries/hong_kong.go new file mode 100644 index 0000000..6b05d46 --- /dev/null +++ b/countries/hong_kong.go @@ -0,0 +1,29 @@ +package countries + +type hongKong struct { + Country +} + +var HongKong = hongKong{ + Country: Country{ + Name: "Hong Kong", + Codes: []string{ + "HK", + "HKG", + "344", + }, + Rules: CountryRules{ + Regex: []string{ + "^(HK)(\\d{8})$", + }, + }, + }, +} + +func (h hongKong) Calc(vat string) bool { + return len(vat) == 8 +} + +func (h hongKong) GetCountry() Country { + return h.Country +} diff --git a/countries/hong_kong/hong_kong.go b/countries/hong_kong/hong_kong.go deleted file mode 100644 index 8dbde22..0000000 --- a/countries/hong_kong/hong_kong.go +++ /dev/null @@ -1,33 +0,0 @@ -package hong_kong - -import ( - "github.com/ltns35/go-vat/countries" -) - -type hongKong struct { - countries.Country -} - -var VAT = hongKong{ - Country: countries.Country{ - Name: "Hong Kong", - Codes: []string{ - "HK", - "HKG", - "344", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(HK)(\\d{8})$", - }, - }, - }, -} - -func (h hongKong) Calc(vat string) bool { - return len(vat) == 8 -} - -func (h hongKong) GetCountry() *countries.Country { - return &h.Country -} diff --git a/countries/hungary/hungary.go b/countries/hungary.go similarity index 78% rename from countries/hungary/hungary.go rename to countries/hungary.go index 45fa377..54c74d0 100644 --- a/countries/hungary/hungary.go +++ b/countries/hungary.go @@ -1,23 +1,22 @@ -package hungary +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type hungary struct { - countries.Country + Country } -var VAT = hungary{ - Country: countries.Country{ +var Hungary = hungary{ + Country: Country{ Name: "Hungary", Codes: []string{ "HU", "HUN", "348", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 9, @@ -58,6 +57,6 @@ func (h hungary) Calc(vat string) bool { return total == expect } -func (h hungary) GetCountry() *countries.Country { - return &h.Country +func (h hungary) GetCountry() Country { + return h.Country } diff --git a/countries/ireland/ireland.go b/countries/ireland.go similarity index 89% rename from countries/ireland/ireland.go rename to countries/ireland.go index b030a34..4e08131 100644 --- a/countries/ireland/ireland.go +++ b/countries/ireland.go @@ -1,25 +1,24 @@ -package ireland +package countries import ( "regexp" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type ireland struct { - countries.Country + Country } -var VAT = ireland{ - Country: countries.Country{ +var Ireland = ireland{ + Country: Country{ Name: "Ireland", Codes: []string{ "IE", "IRL", "372", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 8, @@ -103,6 +102,6 @@ func (i ireland) Calc(vat string) bool { return totalStr == expect } -func (i ireland) GetCountry() *countries.Country { - return &i.Country +func (i ireland) GetCountry() Country { + return i.Country } diff --git a/countries/italy/italy.go b/countries/italy.go similarity index 85% rename from countries/italy/italy.go rename to countries/italy.go index 658292a..7826953 100644 --- a/countries/italy/italy.go +++ b/countries/italy.go @@ -1,26 +1,25 @@ -package italy +package countries import ( "math" "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type italy struct { - countries.Country + Country } -var VAT = italy{ - Country: countries.Country{ +var Italy = italy{ + Country: Country{ Name: "Italy", Codes: []string{ "IT", "ITA", "380", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 1, @@ -87,6 +86,6 @@ func (i italy) Calc(vat string) bool { return total == float64(expect) } -func (i italy) GetCountry() *countries.Country { - return &i.Country +func (i italy) GetCountry() Country { + return i.Country } diff --git a/countries/kazakhstan.go b/countries/kazakhstan.go new file mode 100644 index 0000000..1ba6a88 --- /dev/null +++ b/countries/kazakhstan.go @@ -0,0 +1,29 @@ +package countries + +type kazakhstan struct { + Country +} + +var Kazakhstan = kazakhstan{ + Country: Country{ + Name: "Kazakhstan", + Codes: []string{ + "KZ", + "KAZ", + "398", + }, + Rules: CountryRules{ + Regex: []string{ + "^(KZ)(\\d{12})$", + }, + }, + }, +} + +func (k kazakhstan) Calc(vat string) bool { + return len(vat) == 12 +} + +func (k kazakhstan) GetCountry() Country { + return k.Country +} diff --git a/countries/kazakhstan/kazakhstan.go b/countries/kazakhstan/kazakhstan.go deleted file mode 100644 index f849b2d..0000000 --- a/countries/kazakhstan/kazakhstan.go +++ /dev/null @@ -1,33 +0,0 @@ -package kazakhstan - -import ( - "github.com/ltns35/go-vat/countries" -) - -type kazakhstan struct { - countries.Country -} - -var VAT = kazakhstan{ - Country: countries.Country{ - Name: "Kazakhstan", - Codes: []string{ - "KZ", - "KAZ", - "398", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(KZ)(\\d{12})$", - }, - }, - }, -} - -func (k kazakhstan) Calc(vat string) bool { - return len(vat) == 12 -} - -func (k kazakhstan) GetCountry() *countries.Country { - return &k.Country -} diff --git a/countries/latvia/latvia.go b/countries/latvia.go similarity index 86% rename from countries/latvia/latvia.go rename to countries/latvia.go index c095634..34ebbf1 100644 --- a/countries/latvia/latvia.go +++ b/countries/latvia.go @@ -1,25 +1,24 @@ -package latvia +package countries import ( "regexp" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type latvia struct { - countries.Country + Country } -var VAT = latvia{ - Country: countries.Country{ +var Latvia = latvia{ + Country: Country{ Name: "Latvia", Codes: []string{ "LV", "LVA", "428", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 9, @@ -86,6 +85,6 @@ func (l latvia) Calc(vat string) bool { } } -func (l latvia) GetCountry() *countries.Country { - return &l.Country +func (l latvia) GetCountry() Country { + return l.Country } diff --git a/countries/liechtenstein.go b/countries/liechtenstein.go new file mode 100644 index 0000000..86e2dcb --- /dev/null +++ b/countries/liechtenstein.go @@ -0,0 +1,29 @@ +package countries + +type liechtenstein struct { + Country +} + +var Liechtenstein = liechtenstein{ + Country: Country{ + Name: "Liechtenstein", + Codes: []string{ + "LI", + "LIE", + "807", + }, + Rules: CountryRules{ + Regex: []string{ + "^(FL)(\\d{11})$", + }, + }, + }, +} + +func (l liechtenstein) Calc(vat string) bool { + return len(vat) == 11 +} + +func (l liechtenstein) GetCountry() Country { + return l.Country +} diff --git a/countries/liechtenstein/liechtenstein.go b/countries/liechtenstein/liechtenstein.go deleted file mode 100644 index f062bba..0000000 --- a/countries/liechtenstein/liechtenstein.go +++ /dev/null @@ -1,33 +0,0 @@ -package liechtenstein - -import ( - "github.com/ltns35/go-vat/countries" -) - -type liechtenstein struct { - countries.Country -} - -var VAT = liechtenstein{ - Country: countries.Country{ - Name: "Liechtenstein", - Codes: []string{ - "LI", - "LIE", - "807", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(FL)(\\d{11})$", - }, - }, - }, -} - -func (l liechtenstein) Calc(vat string) bool { - return len(vat) == 11 -} - -func (l liechtenstein) GetCountry() *countries.Country { - return &l.Country -} diff --git a/countries/lithuania/lithuania.go b/countries/lithuania.go similarity index 80% rename from countries/lithuania/lithuania.go rename to countries/lithuania.go index 4aec510..21cc531 100644 --- a/countries/lithuania/lithuania.go +++ b/countries/lithuania.go @@ -1,25 +1,24 @@ -package lithuania +package countries import ( "regexp" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type lithuania struct { - countries.Country + Country } -var VAT = lithuania{ - Country: countries.Country{ +var Lithuania = lithuania{ + Country: Country{ Name: "Lithuania", Codes: []string{ "LT", "LTU", "440", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "short": { 3, @@ -70,8 +69,8 @@ func (l lithuania) Calc(vat string) bool { return check9DigitVat(vat, l.Rules) || check12DigitVat(vat, l.Rules) } -func (l lithuania) GetCountry() *countries.Country { - return &l.Country +func (l lithuania) GetCountry() Country { + return l.Country } func extractDigit1(vat string, multiplierList []int, key int) int { @@ -81,7 +80,7 @@ func extractDigit1(vat string, multiplierList []int, key int) int { return num * multiplierList[key] } -func doubleCheckCalculation(vat string, total int, rules countries.CountryRules) int { +func doubleCheckCalculation(vat string, total int, rules CountryRules) int { result := total if result%11 == 10 { @@ -103,7 +102,7 @@ func extractDigit2(vat string, total int) int { return result } -func checkDigit(total int) int { +func checkDigitLithuania(total int) int { result := total % 11 if result == 10 { result = 0 @@ -112,7 +111,7 @@ func checkDigit(total int) int { return result } -func check9DigitVat(vat string, rules countries.CountryRules) bool { +func check9DigitVat(vat string, rules CountryRules) bool { // 9 character VAT numbers are for legal persons total := 0 @@ -132,7 +131,7 @@ func check9DigitVat(vat string, rules countries.CountryRules) bool { total = doubleCheckCalculation(vat, total, rules) // Establish check digit. - total = checkDigit(total) + total = checkDigitLithuania(total) // Compare it with the last character of the VAT number. If it's the same, then it's valid. expect := utils.IntAt(vat, 8) @@ -143,7 +142,7 @@ func check9DigitVat(vat string, rules countries.CountryRules) bool { return false } -func extractDigit12(vat string, total int, rules countries.CountryRules) int { +func extractDigit12(vat string, total int, rules CountryRules) int { result := total @@ -154,7 +153,7 @@ func extractDigit12(vat string, total int, rules countries.CountryRules) int { return result } -func doubleCheckCalculation12(vat string, total int, rules countries.CountryRules) int { +func doubleCheckCalculation12(vat string, total int, rules CountryRules) int { result := total @@ -169,7 +168,7 @@ func doubleCheckCalculation12(vat string, total int, rules countries.CountryRule return result } -func check12DigitVat(vat string, rules countries.CountryRules) bool { +func check12DigitVat(vat string, rules CountryRules) bool { total := 0 @@ -194,7 +193,7 @@ func check12DigitVat(vat string, rules countries.CountryRules) bool { total = doubleCheckCalculation12(vat, total, rules) // Establish check digit. - total = checkDigit(total) + total = checkDigitLithuania(total) // Compare it with the last character of the VAT number. If it's the same, then it's valid. expect := utils.IntAt(vat, 11) diff --git a/countries/luxembourg/luxembourg.go b/countries/luxembourg.go similarity index 60% rename from countries/luxembourg/luxembourg.go rename to countries/luxembourg.go index 57667e0..aabcde6 100644 --- a/countries/luxembourg/luxembourg.go +++ b/countries/luxembourg.go @@ -1,24 +1,22 @@ -package luxembourg +package countries import ( "strconv" - - "github.com/ltns35/go-vat/countries" ) type luxembourg struct { - countries.Country + Country } -var VAT = luxembourg{ - Country: countries.Country{ +var Luxembourg = luxembourg{ + Country: Country{ Name: "Luxembourg", Codes: []string{ "LU", "LUX", "442", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(LU)(\\d{8})$", }, @@ -31,7 +29,7 @@ func (l luxembourg) Calc(vat string) bool { expectStr := vat[6:8] expect, _ := strconv.Atoi(expectStr) - // Checks the check digits of a VAT VAT number. + // Checks the check digits of a VAT number. checkDigitStr := vat[:6] checkDigit, _ := strconv.Atoi(checkDigitStr) checkDigit = checkDigit % 89 @@ -39,6 +37,6 @@ func (l luxembourg) Calc(vat string) bool { return checkDigit == expect } -func (l luxembourg) GetCountry() *countries.Country { - return &l.Country +func (l luxembourg) GetCountry() Country { + return l.Country } diff --git a/countries/malta/malta.go b/countries/malta.go similarity index 78% rename from countries/malta/malta.go rename to countries/malta.go index c965e80..76e7aaa 100644 --- a/countries/malta/malta.go +++ b/countries/malta.go @@ -1,25 +1,24 @@ -package malta +package countries import ( "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type malta struct { - countries.Country + Country } -var VAT = malta{ - Country: countries.Country{ +var Malta = malta{ + Country: Country{ Name: "Malta", Codes: []string{ "MT", "MLT", "470", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 3, @@ -57,6 +56,6 @@ func (m malta) Calc(vat string) bool { return total == expect } -func (m malta) GetCountry() *countries.Country { - return &m.Country +func (m malta) GetCountry() Country { + return m.Country } diff --git a/countries/netherlands/netherlands.go b/countries/netherlands.go similarity index 90% rename from countries/netherlands/netherlands.go rename to countries/netherlands.go index faac743..640591e 100644 --- a/countries/netherlands/netherlands.go +++ b/countries/netherlands.go @@ -1,27 +1,26 @@ -package netherlands +package countries import ( "fmt" "regexp" "strings" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type netherlands struct { - countries.Country + Country } -var VAT = netherlands{ - Country: countries.Country{ +var Netherlands = netherlands{ + Country: Country{ Name: "Netherlands", Codes: []string{ "NL", "NLD", "528", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 9, @@ -98,8 +97,8 @@ func (n netherlands) Calc(vat string) bool { return total == expect || isNinetySevenMod(concat) } -func (n netherlands) GetCountry() *countries.Country { - return &n.Country +func (n netherlands) GetCountry() Country { + return n.Country } func getCharValue(char rune) int { diff --git a/countries/north_macedonia.go b/countries/north_macedonia.go new file mode 100644 index 0000000..b18c780 --- /dev/null +++ b/countries/north_macedonia.go @@ -0,0 +1,29 @@ +package countries + +type northMacedonia struct { + Country +} + +var NorthMacedonia = northMacedonia{ + Country: Country{ + Name: "North Macedonia", + Codes: []string{ + "MK", + "MKD", + "807", + }, + Rules: CountryRules{ + Regex: []string{ + "^(MK)(\\d{13})$", + }, + }, + }, +} + +func (n northMacedonia) Calc(vat string) bool { + return len(vat) == 13 +} + +func (n northMacedonia) GetCountry() Country { + return n.Country +} diff --git a/countries/north_macedonia/north_macedonia.go b/countries/north_macedonia/north_macedonia.go deleted file mode 100644 index 8572731..0000000 --- a/countries/north_macedonia/north_macedonia.go +++ /dev/null @@ -1,33 +0,0 @@ -package north_macedonia - -import ( - "github.com/ltns35/go-vat/countries" -) - -type northMacedonia struct { - countries.Country -} - -var VAT = northMacedonia{ - Country: countries.Country{ - Name: "North Macedonia", - Codes: []string{ - "MK", - "MKD", - "807", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(MK)(\\d{13})$", - }, - }, - }, -} - -func (n northMacedonia) Calc(vat string) bool { - return len(vat) == 13 -} - -func (n northMacedonia) GetCountry() *countries.Country { - return &n.Country -} diff --git a/countries/norway/norway.go b/countries/norway.go similarity index 81% rename from countries/norway/norway.go rename to countries/norway.go index 37d3e7b..d420453 100644 --- a/countries/norway/norway.go +++ b/countries/norway.go @@ -1,23 +1,22 @@ -package norway +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type norway struct { - countries.Country + Country } -var VAT = norway{ - Country: countries.Country{ +var Norway = norway{ + Country: Country{ Name: "Norway", Codes: []string{ "NO", "NOR", "578", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 3, @@ -66,6 +65,6 @@ func (n norway) Calc(vat string) bool { return false } -func (n norway) GetCountry() *countries.Country { - return &n.Country +func (n norway) GetCountry() Country { + return n.Country } diff --git a/countries/peru.go b/countries/peru.go new file mode 100644 index 0000000..d9836e4 --- /dev/null +++ b/countries/peru.go @@ -0,0 +1,29 @@ +package countries + +type peru struct { + Country +} + +var Peru = peru{ + Country: Country{ + Name: "Peru", + Codes: []string{ + "PE", + "PER", + "604", + }, + Rules: CountryRules{ + Regex: []string{ + "^(PE)(\\d{11})$", + }, + }, + }, +} + +func (p peru) Calc(vat string) bool { + return len(vat) == 11 +} + +func (p peru) GetCountry() Country { + return p.Country +} diff --git a/countries/peru/peru.go b/countries/peru/peru.go deleted file mode 100644 index bdf8392..0000000 --- a/countries/peru/peru.go +++ /dev/null @@ -1,33 +0,0 @@ -package peru - -import ( - "github.com/ltns35/go-vat/countries" -) - -type peru struct { - countries.Country -} - -var VAT = peru{ - Country: countries.Country{ - Name: "Peru", - Codes: []string{ - "PE", - "PER", - "604", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(PE)(\\d{11})$", - }, - }, - }, -} - -func (p peru) Calc(vat string) bool { - return len(vat) == 11 -} - -func (p peru) GetCountry() *countries.Country { - return &p.Country -} diff --git a/countries/poland/poland.go b/countries/poland.go similarity index 79% rename from countries/poland/poland.go rename to countries/poland.go index 4c3c16c..328dc44 100644 --- a/countries/poland/poland.go +++ b/countries/poland.go @@ -1,23 +1,22 @@ -package poland +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type poland struct { - countries.Country + Country } -var VAT = poland{ - Country: countries.Country{ +var Poland = poland{ + Country: Country{ Name: "Poland", Codes: []string{ "PL", "POL", "616", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 6, @@ -60,6 +59,6 @@ func (p poland) Calc(vat string) bool { return total == expect } -func (p poland) GetCountry() *countries.Country { - return &p.Country +func (p poland) GetCountry() Country { + return p.Country } diff --git a/countries/portugal/portugal.go b/countries/portugal.go similarity index 79% rename from countries/portugal/portugal.go rename to countries/portugal.go index 774555d..6f6de0b 100644 --- a/countries/portugal/portugal.go +++ b/countries/portugal.go @@ -1,24 +1,22 @@ -package portugal +package countries import ( "strconv" - - "github.com/ltns35/go-vat/countries" ) type portugal struct { - countries.Country + Country } -var VAT = portugal{ - Country: countries.Country{ +var Portugal = portugal{ + Country: Country{ Name: "Portugal", Codes: []string{ "PT", "PRT", "020", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 9, @@ -38,8 +36,8 @@ var VAT = portugal{ }, } -func (p portugal) GetCountry() *countries.Country { - return &p.Country +func (p portugal) GetCountry() Country { + return p.Country } func (p portugal) Calc(vat string) bool { diff --git a/countries/romania/romania.go b/countries/romania.go similarity index 80% rename from countries/romania/romania.go rename to countries/romania.go index 3061eb6..c491d33 100644 --- a/countries/romania/romania.go +++ b/countries/romania.go @@ -1,23 +1,22 @@ -package romania +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type romania struct { - countries.Country + Country } -var VAT = romania{ - Country: countries.Country{ +var Romania = romania{ + Country: Country{ Name: "Romania", Codes: []string{ "RO", "ROU", "642", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 7, @@ -64,6 +63,6 @@ func (r romania) Calc(vat string) bool { return total == expect } -func (r romania) GetCountry() *countries.Country { - return &r.Country +func (r romania) GetCountry() Country { + return r.Country } diff --git a/countries/russia/russia.go b/countries/russia.go similarity index 83% rename from countries/russia/russia.go rename to countries/russia.go index d549f0e..f9b564f 100644 --- a/countries/russia/russia.go +++ b/countries/russia.go @@ -1,23 +1,22 @@ -package russia +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type russia struct { - countries.Country + Country } -var VAT = russia{ - Country: countries.Country{ +var Russia = russia{ + Country: Country{ Name: "Russia", Codes: []string{ "RU", "RUS", "643", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "m_1": { 2, @@ -70,11 +69,11 @@ func (r russia) Calc(vat string) bool { return check10DigitINN(vat, r.Rules) || check12DigitINN(vat, r.Rules) } -func (r russia) GetCountry() *countries.Country { - return &r.Country +func (r russia) GetCountry() Country { + return r.Country } -func check10DigitINN(vat string, rules countries.CountryRules) bool { +func check10DigitINN(vat string, rules CountryRules) bool { total := 0 @@ -98,7 +97,7 @@ func check10DigitINN(vat string, rules countries.CountryRules) bool { return false } -func check12DigitINN(vat string, rules countries.CountryRules) bool { +func check12DigitINN(vat string, rules CountryRules) bool { total1 := 0 total2 := 0 diff --git a/countries/san_marino.go b/countries/san_marino.go new file mode 100644 index 0000000..f6f0ae8 --- /dev/null +++ b/countries/san_marino.go @@ -0,0 +1,29 @@ +package countries + +type sanMarino struct { + Country +} + +var SanMarino = sanMarino{ + Country: Country{ + Name: "San Marino", + Codes: []string{ + "SM", + "SMR", + "674", + }, + Rules: CountryRules{ + Regex: []string{ + "^(SM)(\\d{5})$", + }, + }, + }, +} + +func (s sanMarino) Calc(vat string) bool { + return len(vat) == 5 +} + +func (s sanMarino) GetCountry() Country { + return s.Country +} diff --git a/countries/san_marino/san_marino.go b/countries/san_marino/san_marino.go deleted file mode 100644 index a697df5..0000000 --- a/countries/san_marino/san_marino.go +++ /dev/null @@ -1,33 +0,0 @@ -package san_marino - -import ( - "github.com/ltns35/go-vat/countries" -) - -type sanMarino struct { - countries.Country -} - -var VAT = sanMarino{ - Country: countries.Country{ - Name: "San Marino", - Codes: []string{ - "SM", - "SMR", - "674", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(SM)(\\d{5})$", - }, - }, - }, -} - -func (s sanMarino) Calc(vat string) bool { - return len(vat) == 5 -} - -func (s sanMarino) GetCountry() *countries.Country { - return &s.Country -} diff --git a/countries/serbia/serbia.go b/countries/serbia.go similarity index 76% rename from countries/serbia/serbia.go rename to countries/serbia.go index 3815dbf..5c3076a 100644 --- a/countries/serbia/serbia.go +++ b/countries/serbia.go @@ -1,23 +1,22 @@ -package serbia +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type serbia struct { - countries.Country + Country } -var VAT = serbia{ - Country: countries.Country{ +var Serbia = serbia{ + Country: Country{ Name: "Serbia", Codes: []string{ "RS", "SRB", "688", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(RS)(\\d{9})$", }, @@ -49,6 +48,6 @@ func (s serbia) Calc(vat string) bool { return checkDigit == expect } -func (s serbia) GetCountry() *countries.Country { - return &s.Country +func (s serbia) GetCountry() Country { + return s.Country } diff --git a/countries/slovakia/slovakia.go b/countries/slovakia.go similarity index 60% rename from countries/slovakia/slovakia.go rename to countries/slovakia.go index 18576a6..18f782a 100644 --- a/countries/slovakia/slovakia.go +++ b/countries/slovakia.go @@ -1,24 +1,22 @@ -package slovakia +package countries import ( "strconv" - - "github.com/ltns35/go-vat/countries" ) type slovakia struct { - countries.Country + Country } -var VAT = slovakia{ - Country: countries.Country{ +var Slovakia = slovakia{ + Country: Country{ Name: "Slovakia", Codes: []string{ "SK", "SVK", "703", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(SK)([1-9]\\d[2346-9]\\d{7})$", }, @@ -37,6 +35,6 @@ func (s slovakia) Calc(vat string) bool { return checkDigit == expect } -func (s slovakia) GetCountry() *countries.Country { - return &s.Country +func (s slovakia) GetCountry() Country { + return s.Country } diff --git a/countries/slovenia/slovenia.go b/countries/slovenia.go similarity index 79% rename from countries/slovenia/slovenia.go rename to countries/slovenia.go index fc933c7..09a62c2 100644 --- a/countries/slovenia/slovenia.go +++ b/countries/slovenia.go @@ -1,23 +1,22 @@ -package slovenia +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type slovenia struct { - countries.Country + Country } -var VAT = slovenia{ - Country: countries.Country{ +var Slovenia = slovenia{ + Country: Country{ Name: "Slovenia", Codes: []string{ "SI", "SVN", "705", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 8, @@ -59,6 +58,6 @@ func (s slovenia) Calc(vat string) bool { return !!(total != 11 && total == expect) } -func (s slovenia) GetCountry() *countries.Country { - return &s.Country +func (s slovenia) GetCountry() Country { + return s.Country } diff --git a/countries/spain/spain.go b/countries/spain.go similarity index 94% rename from countries/spain/spain.go rename to countries/spain.go index bb283e7..e57c8f3 100644 --- a/countries/spain/spain.go +++ b/countries/spain.go @@ -1,4 +1,4 @@ -package spain +package countries import ( "math" @@ -6,7 +6,6 @@ import ( "strconv" "strings" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) @@ -14,18 +13,18 @@ import ( const algorithm = "TRWAGMYFPDXBNJZSQVHLCKE" type spain struct { - countries.Country + Country } -var VAT = spain{ - Country: countries.Country{ +var Spain = spain{ + Country: Country{ Name: "Spain", Codes: []string{ "ES", "ESP", "724", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 2, @@ -90,8 +89,8 @@ func (s spain) Calc(vat string) bool { return false } -func (s spain) GetCountry() *countries.Country { - return &s.Country +func (s spain) GetCountry() Country { + return s.Country } func extractDigitAndMultiplyByCounter(vat string, multipliers []int, total float64) int { diff --git a/countries/sweden/sweden.go b/countries/sweden.go similarity index 78% rename from countries/sweden/sweden.go rename to countries/sweden.go index 7615a9e..ca0840d 100644 --- a/countries/sweden/sweden.go +++ b/countries/sweden.go @@ -1,25 +1,24 @@ -package sweden +package countries import ( "math" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type sweden struct { - countries.Country + Country } -var VAT = sweden{ - Country: countries.Country{ +var Sweden = sweden{ + Country: Country{ Name: "Sweden", Codes: []string{ "SE", "SWE", "752", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Regex: []string{ "^(SE)(\\d{10}01)$", }, @@ -51,6 +50,6 @@ func (s sweden) Calc(vat string) bool { return checkDigit == expect } -func (s sweden) GetCountry() *countries.Country { - return &s.Country +func (s sweden) GetCountry() Country { + return s.Country } diff --git a/countries/switzerland/switzerland.go b/countries/switzerland.go similarity index 77% rename from countries/switzerland/switzerland.go rename to countries/switzerland.go index 2f9c612..58c20bb 100644 --- a/countries/switzerland/switzerland.go +++ b/countries/switzerland.go @@ -1,23 +1,22 @@ -package switzerland +package countries import ( - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type switzerland struct { - countries.Country + Country } -var VAT = switzerland{ - Country: countries.Country{ +var Switzerland = switzerland{ + Country: Country{ Name: "Switzerland", Codes: []string{ "CH", "CHE", "756", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 5, @@ -62,6 +61,6 @@ func (s switzerland) Calc(vat string) bool { return total == expect } -func (s switzerland) GetCountry() *countries.Country { - return &s.Country +func (s switzerland) GetCountry() Country { + return s.Country } diff --git a/countries/turkey.go b/countries/turkey.go new file mode 100644 index 0000000..e7daed5 --- /dev/null +++ b/countries/turkey.go @@ -0,0 +1,29 @@ +package countries + +type turkey struct { + Country +} + +var Turkey = turkey{ + Country: Country{ + Name: "Turkey", + Codes: []string{ + "TR", + "TUR", + "792", + }, + Rules: CountryRules{ + Regex: []string{ + "^(TR)(\\d{10})$", + }, + }, + }, +} + +func (t turkey) Calc(vat string) bool { + return len(vat) == 10 +} + +func (t turkey) GetCountry() Country { + return t.Country +} diff --git a/countries/turkey/turkey.go b/countries/turkey/turkey.go deleted file mode 100644 index 0858186..0000000 --- a/countries/turkey/turkey.go +++ /dev/null @@ -1,33 +0,0 @@ -package turkey - -import ( - "github.com/ltns35/go-vat/countries" -) - -type turkey struct { - countries.Country -} - -var VAT = turkey{ - Country: countries.Country{ - Name: "Turkey", - Codes: []string{ - "TR", - "TUR", - "792", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(TR)(\\d{10})$", - }, - }, - }, -} - -func (t turkey) Calc(vat string) bool { - return len(vat) == 10 -} - -func (t turkey) GetCountry() *countries.Country { - return &t.Country -} diff --git a/countries/ukraine.go b/countries/ukraine.go new file mode 100644 index 0000000..d18d033 --- /dev/null +++ b/countries/ukraine.go @@ -0,0 +1,29 @@ +package countries + +type ukraine struct { + Country +} + +var Ukraine = ukraine{ + Country: Country{ + Name: "Ukraine", + Codes: []string{ + "UA", + "UKR", + "804", + }, + Rules: CountryRules{ + Regex: []string{ + "^(UA)(\\d{12})$", + }, + }, + }, +} + +func (u ukraine) Calc(vat string) bool { + return len(vat) == 12 +} + +func (u ukraine) GetCountry() Country { + return u.Country +} diff --git a/countries/ukraine/ukraine.go b/countries/ukraine/ukraine.go deleted file mode 100644 index 89d2c57..0000000 --- a/countries/ukraine/ukraine.go +++ /dev/null @@ -1,33 +0,0 @@ -package ukraine - -import ( - "github.com/ltns35/go-vat/countries" -) - -type ukraine struct { - countries.Country -} - -var VAT = ukraine{ - Country: countries.Country{ - Name: "Ukraine", - Codes: []string{ - "UA", - "UKR", - "804", - }, - Rules: countries.CountryRules{ - Regex: []string{ - "^(UA)(\\d{12})$", - }, - }, - }, -} - -func (u ukraine) Calc(vat string) bool { - return len(vat) == 12 -} - -func (u ukraine) GetCountry() *countries.Country { - return &u.Country -} diff --git a/countries/united_kingdom/united_kingdom.go b/countries/united_kingdom.go similarity index 91% rename from countries/united_kingdom/united_kingdom.go rename to countries/united_kingdom.go index 0ea1484..303536e 100644 --- a/countries/united_kingdom/united_kingdom.go +++ b/countries/united_kingdom.go @@ -1,26 +1,25 @@ -package united_kingdom +package countries import ( "math" "strconv" - "github.com/ltns35/go-vat/countries" "github.com/ltns35/go-vat/countries/utils" ) type unitedKingdom struct { - countries.Country + Country } -var VAT = unitedKingdom{ - Country: countries.Country{ +var UnitedKingdom = unitedKingdom{ + Country: Country{ Name: "United Kingdom", Codes: []string{ "GB", "GBR", "826", }, - Rules: countries.CountryRules{ + Rules: CountryRules{ Multipliers: map[string][]int{ "common": { 8, @@ -59,8 +58,8 @@ func (u unitedKingdom) Calc(vat string) bool { return isStandardOrCommercialNumber(vat, u.Rules.Multipliers["common"]) } -func (u unitedKingdom) GetCountry() *countries.Country { - return &u.Country +func (u unitedKingdom) GetCountry() Country { + return u.Country } func isGovernmentDepartment(vat string) bool { diff --git a/types.go b/types.go index b468963..de7cc92 100644 --- a/types.go +++ b/types.go @@ -5,8 +5,8 @@ import ( ) type CheckResult struct { - Value string `json:"value"` - IsValid bool `json:"isValid"` - IsSupportedCountry bool `json:"isSupportedCountry"` - Country *countries.Country `json:"country"` + Value string `json:"value"` + IsValid bool `json:"isValid"` + IsSupportedCountry bool `json:"isSupportedCountry"` + Country countries.Country `json:"country"` } diff --git a/validator.go b/validator.go index 29e7ff1..f2ab295 100644 --- a/validator.go +++ b/validator.go @@ -6,119 +6,72 @@ import ( "strings" "github.com/ltns35/go-vat/countries" - "github.com/ltns35/go-vat/countries/albania" - "github.com/ltns35/go-vat/countries/andorra" - "github.com/ltns35/go-vat/countries/argentina" - "github.com/ltns35/go-vat/countries/austria" - "github.com/ltns35/go-vat/countries/belarus" - "github.com/ltns35/go-vat/countries/belgium" - "github.com/ltns35/go-vat/countries/bolivia" - "github.com/ltns35/go-vat/countries/brazil" - "github.com/ltns35/go-vat/countries/bulgaria" - "github.com/ltns35/go-vat/countries/canada" - "github.com/ltns35/go-vat/countries/croatia" - "github.com/ltns35/go-vat/countries/cyprus" - "github.com/ltns35/go-vat/countries/czech_republic" - "github.com/ltns35/go-vat/countries/denmark" - "github.com/ltns35/go-vat/countries/estonia" - "github.com/ltns35/go-vat/countries/finland" - "github.com/ltns35/go-vat/countries/france" - "github.com/ltns35/go-vat/countries/germany" - "github.com/ltns35/go-vat/countries/greece" - "github.com/ltns35/go-vat/countries/hong_kong" - "github.com/ltns35/go-vat/countries/hungary" - "github.com/ltns35/go-vat/countries/ireland" - "github.com/ltns35/go-vat/countries/italy" - "github.com/ltns35/go-vat/countries/kazakhstan" - "github.com/ltns35/go-vat/countries/latvia" - "github.com/ltns35/go-vat/countries/liechtenstein" - "github.com/ltns35/go-vat/countries/lithuania" - "github.com/ltns35/go-vat/countries/luxembourg" - "github.com/ltns35/go-vat/countries/malta" - "github.com/ltns35/go-vat/countries/netherlands" - "github.com/ltns35/go-vat/countries/north_macedonia" - "github.com/ltns35/go-vat/countries/norway" - "github.com/ltns35/go-vat/countries/peru" - "github.com/ltns35/go-vat/countries/poland" - "github.com/ltns35/go-vat/countries/portugal" - "github.com/ltns35/go-vat/countries/romania" - "github.com/ltns35/go-vat/countries/russia" - "github.com/ltns35/go-vat/countries/san_marino" - "github.com/ltns35/go-vat/countries/serbia" - "github.com/ltns35/go-vat/countries/slovakia" - "github.com/ltns35/go-vat/countries/slovenia" - "github.com/ltns35/go-vat/countries/spain" - "github.com/ltns35/go-vat/countries/sweden" - "github.com/ltns35/go-vat/countries/switzerland" - "github.com/ltns35/go-vat/countries/turkey" - "github.com/ltns35/go-vat/countries/ukraine" - "github.com/ltns35/go-vat/countries/united_kingdom" ) var countriesVATDoesNotStartWithCountryCode = []string{ - brazil.VAT.Name, + countries.Brazil.Name, } var allCountries = []countries.Calculer{ - albania.VAT, - andorra.VAT, - argentina.VAT, - austria.VAT, - belarus.VAT, - belgium.VAT, - bolivia.VAT, - brazil.VAT, - bulgaria.VAT, - canada.VAT, - croatia.VAT, - cyprus.VAT, - czech_republic.VAT, - denmark.VAT, - estonia.VAT, - finland.VAT, - france.VAT, - germany.VAT, - greece.VAT, - hong_kong.VAT, - hungary.VAT, - italy.VAT, - ireland.VAT, - kazakhstan.VAT, - latvia.VAT, - liechtenstein.VAT, - lithuania.VAT, - luxembourg.VAT, - malta.VAT, - north_macedonia.VAT, - norway.VAT, - netherlands.VAT, - peru.VAT, - poland.VAT, - portugal.VAT, - romania.VAT, - russia.VAT, - san_marino.VAT, - serbia.VAT, - slovakia.VAT, - slovenia.VAT, - spain.VAT, - sweden.VAT, - switzerland.VAT, - turkey.VAT, - ukraine.VAT, - united_kingdom.VAT, + countries.Albania, + countries.Andorra, + countries.Argentina, + countries.Austria, + countries.Belarus, + countries.Belgium, + countries.Bolivia, + countries.Brazil, + countries.Bulgaria, + countries.Canada, + countries.Croatia, + countries.Cyprus, + countries.CzechRepublic, + countries.Denmark, + countries.Estonia, + countries.Finland, + countries.France, + countries.Germany, + countries.Greece, + countries.HongKong, + countries.Hungary, + countries.Ireland, + countries.Italy, + countries.Kazakhstan, + countries.Latvia, + countries.Liechtenstein, + countries.Lithuania, + countries.Luxembourg, + countries.Malta, + countries.Netherlands, + countries.NorthMacedonia, + countries.Norway, + countries.Peru, + countries.Poland, + countries.Portugal, + countries.Romania, + countries.Russia, + countries.SanMarino, + countries.Serbia, + countries.Slovakia, + countries.Slovenia, + countries.Spain, + countries.Sweden, + countries.Switzerland, + countries.Turkey, + countries.Ukraine, + countries.UnitedKingdom, } -func makeResult(vat string, isValid bool, country *countries.Country) *CheckResult { +func makeResult(vat string, isValid bool, isSupported bool, country countries.Country) *CheckResult { var checkResult CheckResult checkResult.Value = vat checkResult.IsValid = isValid - checkResult.Country = country + checkResult.IsSupportedCountry = isSupported - if country != nil { - checkResult.IsSupportedCountry = true + if isSupported { + checkResult.Country = country } return &checkResult @@ -134,13 +87,13 @@ func removeExtraChars(vat string) string { return vat } -func getCountryCodes(country *countries.Country) []string { +func getCountryCodes(country countries.Country) []string { codes := country.Codes - if country.Name == greece.VAT.Name { + if country.Name == countries.Greece.Name { codes = append(codes, "EL") - } else if country.Name == liechtenstein.VAT.Name { + } else if country.Name == countries.Liechtenstein.Name { codes = append(codes, "FL") } @@ -160,7 +113,7 @@ func getCountry(vat string, countriesList []countries.Calculer) (countries.Calcu return nil, errors.New("cannot retrieve a supported country") } -func startsWithCode(vat string, country *countries.Country) bool { +func startsWithCode(vat string, country countries.Country) bool { countryCodes := getCountryCodes(country) for _, code := range countryCodes { @@ -219,15 +172,15 @@ func isVatValid(vat string, country countries.Calculer) bool { return country.Calc(cleanVAT) } -func CheckVAT(vat string, countriesList ...countries.Calculer) (*CheckResult, error) { +func Validate(vat string, countriesList ...countries.Calculer) (*CheckResult, error) { if strings.Trim(vat, " ") == "" { - return makeResult(vat, false, nil), nil + return makeResult(vat, false, false, countries.Country{}), nil } cleanVAT := removeExtraChars(vat) - if len(countriesList) == 0 { + if countriesList[0] == nil { countriesList = allCountries } @@ -238,5 +191,5 @@ func CheckVAT(vat string, countriesList ...countries.Calculer) (*CheckResult, er isValid := isVatValid(cleanVAT, country) - return makeResult(cleanVAT, isValid, country.GetCountry()), nil + return makeResult(cleanVAT, isValid, true, country.GetCountry()), nil } diff --git a/validator_test.go b/validator_test.go index ba9877d..79f98d7 100644 --- a/validator_test.go +++ b/validator_test.go @@ -5,54 +5,7 @@ import ( "testing" "github.com/ltns35/go-vat/countries" - "github.com/ltns35/go-vat/countries/albania" - "github.com/ltns35/go-vat/countries/andorra" - "github.com/ltns35/go-vat/countries/argentina" - "github.com/ltns35/go-vat/countries/austria" - "github.com/ltns35/go-vat/countries/belarus" - "github.com/ltns35/go-vat/countries/belgium" - "github.com/ltns35/go-vat/countries/bolivia" - "github.com/ltns35/go-vat/countries/brazil" - "github.com/ltns35/go-vat/countries/bulgaria" - "github.com/ltns35/go-vat/countries/canada" - "github.com/ltns35/go-vat/countries/croatia" - "github.com/ltns35/go-vat/countries/cyprus" - "github.com/ltns35/go-vat/countries/czech_republic" - "github.com/ltns35/go-vat/countries/denmark" - "github.com/ltns35/go-vat/countries/estonia" - "github.com/ltns35/go-vat/countries/finland" - "github.com/ltns35/go-vat/countries/france" - "github.com/ltns35/go-vat/countries/germany" - "github.com/ltns35/go-vat/countries/greece" - "github.com/ltns35/go-vat/countries/hong_kong" - "github.com/ltns35/go-vat/countries/hungary" - "github.com/ltns35/go-vat/countries/ireland" - "github.com/ltns35/go-vat/countries/italy" - "github.com/ltns35/go-vat/countries/kazakhstan" - "github.com/ltns35/go-vat/countries/latvia" - "github.com/ltns35/go-vat/countries/liechtenstein" - "github.com/ltns35/go-vat/countries/lithuania" - "github.com/ltns35/go-vat/countries/luxembourg" - "github.com/ltns35/go-vat/countries/malta" "github.com/ltns35/go-vat/countries/mocks" - "github.com/ltns35/go-vat/countries/netherlands" - "github.com/ltns35/go-vat/countries/north_macedonia" - "github.com/ltns35/go-vat/countries/norway" - "github.com/ltns35/go-vat/countries/peru" - "github.com/ltns35/go-vat/countries/poland" - "github.com/ltns35/go-vat/countries/portugal" - "github.com/ltns35/go-vat/countries/romania" - "github.com/ltns35/go-vat/countries/russia" - "github.com/ltns35/go-vat/countries/san_marino" - "github.com/ltns35/go-vat/countries/serbia" - "github.com/ltns35/go-vat/countries/slovakia" - "github.com/ltns35/go-vat/countries/slovenia" - "github.com/ltns35/go-vat/countries/spain" - "github.com/ltns35/go-vat/countries/sweden" - "github.com/ltns35/go-vat/countries/switzerland" - "github.com/ltns35/go-vat/countries/turkey" - "github.com/ltns35/go-vat/countries/ukraine" - "github.com/ltns35/go-vat/countries/united_kingdom" ) func TestCheckVAT(t *testing.T) { @@ -79,7 +32,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &albania.VAT, + country: countries.Albania, }, { name: "Andorra", @@ -93,7 +46,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &andorra.VAT, + country: countries.Andorra, }, { name: "Argentina", @@ -107,7 +60,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &argentina.VAT, + country: countries.Argentina, }, { name: "Austria", @@ -121,7 +74,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &austria.VAT, + country: countries.Austria, }, { name: "Belarus", @@ -135,7 +88,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &belarus.VAT, + country: countries.Belarus, }, { name: "Belgium", @@ -149,7 +102,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &belgium.VAT, + country: countries.Belgium, }, { name: "Bolivia", @@ -163,7 +116,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &bolivia.VAT, + country: countries.Bolivia, }, { name: "Brazil", @@ -177,7 +130,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &brazil.VAT, + country: countries.Brazil, }, { name: "Bulgaria", @@ -191,7 +144,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &bulgaria.VAT, + country: countries.Bulgaria, }, { name: "Canada", @@ -205,7 +158,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &canada.VAT, + country: countries.Canada, }, { name: "Croatia", @@ -219,7 +172,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &croatia.VAT, + country: countries.Croatia, }, { name: "Cyprus", @@ -233,7 +186,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &cyprus.VAT, + country: countries.Cyprus, }, { name: "Czech Republic", @@ -247,7 +200,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &czech_republic.VAT, + country: countries.CzechRepublic, }, { name: "Denmark", @@ -261,7 +214,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &denmark.VAT, + country: countries.Denmark, }, { name: "Estonia", @@ -275,7 +228,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &estonia.VAT, + country: countries.Estonia, }, { name: "Finland", @@ -289,7 +242,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &finland.VAT, + country: countries.Finland, }, { name: "France", @@ -303,7 +256,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &france.VAT, + country: countries.France, }, { name: "Germany", @@ -317,7 +270,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &germany.VAT, + country: countries.Germany, }, { name: "Greece", @@ -331,7 +284,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &greece.VAT, + country: countries.Greece, }, { name: "Hong Kong", @@ -345,7 +298,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &hong_kong.VAT, + country: countries.HongKong, }, { name: "Hungary", @@ -359,7 +312,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &hungary.VAT, + country: countries.Hungary, }, { name: "Italy", @@ -373,7 +326,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &italy.VAT, + country: countries.Italy, }, { name: "Ireland", @@ -387,7 +340,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &ireland.VAT, + country: countries.Ireland, }, { name: "Kazakhstan", @@ -401,7 +354,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &kazakhstan.VAT, + country: countries.Kazakhstan, }, { name: "Latvia", @@ -415,7 +368,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &latvia.VAT, + country: countries.Latvia, }, { name: "Liechtenstein", @@ -429,7 +382,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &liechtenstein.VAT, + country: countries.Liechtenstein, }, { name: "Lithuania", @@ -443,7 +396,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &lithuania.VAT, + country: countries.Lithuania, }, { name: "Luxembourg", @@ -457,7 +410,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &luxembourg.VAT, + country: countries.Luxembourg, }, { name: "Malta", @@ -471,7 +424,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &malta.VAT, + country: countries.Malta, }, { name: "Netherlands", @@ -485,7 +438,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &netherlands.VAT, + country: countries.Netherlands, }, { name: "North Macedonia", @@ -499,7 +452,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &north_macedonia.VAT, + country: countries.NorthMacedonia, }, { name: "Norway", @@ -513,7 +466,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &norway.VAT, + country: countries.Norway, }, { name: "Peru", @@ -527,7 +480,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &peru.VAT, + country: countries.Peru, }, { name: "Poland", @@ -541,7 +494,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &poland.VAT, + country: countries.Poland, }, { name: "Portugal", @@ -555,7 +508,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &portugal.VAT, + country: countries.Portugal, }, { name: "Romania", @@ -569,7 +522,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &romania.VAT, + country: countries.Romania, }, { name: "Russia", @@ -583,7 +536,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &russia.VAT, + country: countries.Russia, }, { name: "San Marino", @@ -597,7 +550,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &san_marino.VAT, + country: countries.SanMarino, }, { name: "Serbia", @@ -611,7 +564,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &serbia.VAT, + country: countries.Serbia, }, { name: "Slovakia", @@ -625,7 +578,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &slovakia.VAT, + country: countries.Slovakia, }, { name: "Slovenia", @@ -639,7 +592,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &slovenia.VAT, + country: countries.Slovenia, }, { name: "Spain", @@ -653,7 +606,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &spain.VAT, + country: countries.Spain, }, { name: "Sweden", @@ -667,7 +620,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &sweden.VAT, + country: countries.Sweden, }, { name: "Switzerland", @@ -681,7 +634,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &switzerland.VAT, + country: countries.Switzerland, }, { name: "Turkey", @@ -695,7 +648,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &turkey.VAT, + country: countries.Turkey, }, { name: "Ukraine", @@ -709,7 +662,7 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &ukraine.VAT, + country: countries.Ukraine, }, { name: "United Kingdom", @@ -723,7 +676,28 @@ func TestCheckVAT(t *testing.T) { want: false, }, }, - country: &united_kingdom.VAT, + country: countries.UnitedKingdom, + }, + { + name: "Multiple countries", + args: []args{ + { + values: mocks.SpainValidTests, + want: true, + }, + { + values: mocks.FranceValidTests, + want: true, + }, + { + values: mocks.GermanyValidTests, + want: true, + }, + { + values: mocks.ItalyValidTests, + want: true, + }, + }, }, } for _, tt := range tests { @@ -746,9 +720,16 @@ func TestCheckVAT(t *testing.T) { t.Run( name, func(t *testing.T) { - got, _ := CheckVAT(value, tt.country) + got := new(CheckResult) + + if &tt.country == nil { + got, _ = Validate(value) + } else { + got, _ = Validate(value, tt.country) + } + if got.IsValid != arg.want { - t.Errorf("CheckVAT() got = %v, want %v", got, arg.want) + t.Errorf("Validate() got = %v, want %v", got, arg.want) } }, )