Skip to content

Commit

Permalink
API changes, json tags and more
Browse files Browse the repository at this point in the history
Signed-off-by: Dušan Mitrović <[email protected]>
  • Loading branch information
dusnm committed Oct 15, 2023
1 parent 53771d8 commit 5f69b87
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
20 changes: 10 additions & 10 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ package checksum

type (
Checksum struct {
Value uint
Valid bool
Value uint `json:"value"`
Valid bool `json:"valid"`
}
)

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,
}
}
8 changes: 4 additions & 4 deletions digits/digits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions jmbg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions jmbg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
12 changes: 6 additions & 6 deletions region/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions region/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestShouldPassWhenRegionFoundForCode(t *testing.T) {
Region{
Code: code,
Name: Name{
Cyrilic: "Београд",
Latin: "Beograd",
Cyrillic: "Београд",
Latin: "Beograd",
},
},
)
Expand Down
16 changes: 8 additions & 8 deletions sex/sex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
)

Expand All @@ -23,17 +23,17 @@ func New(digits [13]uint) Sex {
return Sex{
Code: MALE,
Name: Name{
Cyrilic: "мушки",
Latin: "muški",
Cyrillic: "мушки",
Latin: "muški",
},
}
}

return Sex{
Code: FEMALE,
Name: Name{
Cyrilic: "женски",
Latin: "ženski",
Cyrillic: "женски",
Latin: "ženski",
},
}
}
8 changes: 4 additions & 4 deletions sex/sex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestShouldParseSex(t *testing.T) {
expected: Sex{
Code: MALE,
Name: Name{
Cyrilic: "мушки",
Latin: "muški",
Cyrillic: "мушки",
Latin: "muški",
},
},
},
Expand All @@ -33,8 +33,8 @@ func TestShouldParseSex(t *testing.T) {
expected: Sex{
Code: FEMALE,
Name: Name{
Cyrilic: "женски",
Latin: "ženski",
Cyrillic: "женски",
Latin: "ženski",
},
},
},
Expand Down

0 comments on commit 5f69b87

Please sign in to comment.