diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/checksum/checksum.go b/checksum/checksum.go index 810237d..c284cd1 100644 --- a/checksum/checksum.go +++ b/checksum/checksum.go @@ -2,8 +2,8 @@ package checksum type ( Checksum struct { - Value uint - Valid bool + Value uint `json:"value"` + Valid bool `json:"valid"` } ) @@ -11,32 +11,32 @@ func New(digits [13]uint) Checksum { // Assign a letter to each digit like: // abcdefg hi jkl m // - // The cheksum K is calculated using the following formula: + // The checksum K is calculated using the following formula: // m = 11 - ((7(a+g) + 6(b+h) + 5(c+i) + 4(d+j) + 3(e+k) + 2(f+l)) mod 11) // // if m is between 1 and 9 => K = m // if m is 10 or 11 => K = 0 - csum := digits[12] - calculated_csum := 11 - ((7*(digits[0]+digits[6]) + + checksum := digits[12] + calculatedChecksum := 11 - ((7*(digits[0]+digits[6]) + 6*(digits[1]+digits[7]) + 5*(digits[2]+digits[8]) + 4*(digits[3]+digits[9]) + 3*(digits[4]+digits[10]) + 2*(digits[5]+digits[11])) % 11) - if calculated_csum == 10 || calculated_csum == 11 { - calculated_csum = 0 + if calculatedChecksum == 10 || calculatedChecksum == 11 { + calculatedChecksum = 0 } - if calculated_csum != csum { + if calculatedChecksum != checksum { return Checksum{ - Value: csum, + Value: checksum, Valid: false, } } return Checksum{ - Value: csum, + Value: checksum, Valid: true, } } diff --git a/digits/digits.go b/digits/digits.go index 53a829d..8ca3a93 100644 --- a/digits/digits.go +++ b/digits/digits.go @@ -30,12 +30,12 @@ func New(jmbg string) ([13]uint, error) { } digits := make([]uint, 13) - for i, rune := range runes { - if !unicode.IsDigit(rune) { - return [13]uint{}, InvalidDigitError{input: rune} + for i, r := range runes { + if !unicode.IsDigit(r) { + return [13]uint{}, InvalidDigitError{input: r} } - digits[i] = uint(rune - '0') + digits[i] = uint(r - '0') } return [13]uint(digits), nil diff --git a/jmbg.go b/jmbg.go index fed3601..b8bca6f 100644 --- a/jmbg.go +++ b/jmbg.go @@ -12,37 +12,37 @@ import ( type ( JMBG struct { - Number string - Digits [13]uint - DateOfBirth time.Time - Region region.Region - Sex sex.Sex - Checksum checksum.Checksum + Number string `json:"number"` + Digits [13]uint `json:"digits"` + DateOfBirth time.Time `json:"date_of_birth"` + Region region.Region `json:"region"` + Sex sex.Sex `json:"sex"` + Checksum checksum.Checksum `json:"checksum"` } ) func New(jmbg string) (JMBG, error) { - digits, err := digits.New(jmbg) + d, err := digits.New(jmbg) if err != nil { return JMBG{}, err } - date, err := dob.New(digits) + date, err := dob.New(d) if err != nil { return JMBG{}, err } - region, err := region.New(digits) + r, err := region.New(d) if err != nil { return JMBG{}, err } return JMBG{ Number: jmbg, - Digits: digits, + Digits: d, DateOfBirth: date, - Region: region, - Sex: sex.New(digits), - Checksum: checksum.New(digits), + Region: r, + Sex: sex.New(d), + Checksum: checksum.New(d), }, nil } diff --git a/jmbg_test.go b/jmbg_test.go index 286b930..244bbb7 100644 --- a/jmbg_test.go +++ b/jmbg_test.go @@ -30,15 +30,15 @@ func TestShouldParseJmbg(t *testing.T) { Region: region.Region{ Code: region.NSD, Name: region.Name{ - Cyrilic: "Нови Сад", - Latin: "Novi Sad", + Cyrillic: "Нови Сад", + Latin: "Novi Sad", }, }, Sex: sex.Sex{ Code: sex.FEMALE, Name: sex.Name{ - Cyrilic: "женски", - Latin: "ženski", + Cyrillic: "женски", + Latin: "ženski", }, }, Checksum: checksum.Checksum{ diff --git a/region/region.go b/region/region.go index 5fc00aa..224a64c 100644 --- a/region/region.go +++ b/region/region.go @@ -34,13 +34,13 @@ const ( type ( Name struct { - Cyrilic string - Latin string + Cyrillic string `json:"cyrillic"` + Latin string `json:"latin"` } Region struct { - Code uint - Name Name + Code uint `json:"code"` + Name Name `json:"name"` } InvalidCodeError struct { @@ -168,8 +168,8 @@ func New(digits [13]uint) (Region, error) { return Region{ Code: code, Name: Name{ - Cyrilic: region[0], - Latin: region[1], + Cyrillic: region[0], + Latin: region[1], }, }, nil } diff --git a/region/region_test.go b/region/region_test.go index c38625d..ab4073a 100644 --- a/region/region_test.go +++ b/region/region_test.go @@ -41,8 +41,8 @@ func TestShouldPassWhenRegionFoundForCode(t *testing.T) { Region{ Code: code, Name: Name{ - Cyrilic: "Београд", - Latin: "Beograd", + Cyrillic: "Београд", + Latin: "Beograd", }, }, ) diff --git a/sex/sex.go b/sex/sex.go index f00f82d..59919ad 100644 --- a/sex/sex.go +++ b/sex/sex.go @@ -7,13 +7,13 @@ const ( type ( Name struct { - Cyrilic string - Latin string + Cyrillic string `json:"cyrillic"` + Latin string `json:"latin"` } Sex struct { - Code int - Name Name + Code int `json:"code"` + Name Name `json:"name"` } ) @@ -23,8 +23,8 @@ func New(digits [13]uint) Sex { return Sex{ Code: MALE, Name: Name{ - Cyrilic: "мушки", - Latin: "muški", + Cyrillic: "мушки", + Latin: "muški", }, } } @@ -32,8 +32,8 @@ func New(digits [13]uint) Sex { return Sex{ Code: FEMALE, Name: Name{ - Cyrilic: "женски", - Latin: "ženski", + Cyrillic: "женски", + Latin: "ženski", }, } } diff --git a/sex/sex_test.go b/sex/sex_test.go index f85c722..ea9eb51 100644 --- a/sex/sex_test.go +++ b/sex/sex_test.go @@ -22,8 +22,8 @@ func TestShouldParseSex(t *testing.T) { expected: Sex{ Code: MALE, Name: Name{ - Cyrilic: "мушки", - Latin: "muški", + Cyrillic: "мушки", + Latin: "muški", }, }, }, @@ -33,8 +33,8 @@ func TestShouldParseSex(t *testing.T) { expected: Sex{ Code: FEMALE, Name: Name{ - Cyrilic: "женски", - Latin: "ženski", + Cyrillic: "женски", + Latin: "ženski", }, }, },