Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iLexN committed Oct 27, 2019
0 parents commit 2f8cd98
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
12 changes: 12 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package go_hkid

type patterNotMatchError struct {
}

func NewPatterNotMatchError() *patterNotMatchError {
return &patterNotMatchError{}
}

func (h patterNotMatchError) Error() string {
return "Patter not match"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "go_hkid"
84 changes: 84 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package go_hkid

import (
"regexp"
"strconv"
"strings"
)

func clearString(s string) string {
newString := strings.Trim(s, " ")
newString = strings.ToUpper(newString)
return newString
}

func getRemainder(hkid *Hkid) string {
charSum := getCharSum(hkid.part1)
hkidSum := calPart2Remainder(hkid.part2, charSum)

remainder := ""

switch hkidSum {
case 11:
remainder = "0"
case 10:
remainder = "A"
default:
remainder = strconv.Itoa(hkidSum)
}

return remainder
}

func calPart2Remainder(s string, i int) int {
sum := 0

for k, v := range s {
i, _ := strconv.Atoi(string(v))
sum += (7 - k) * i
}

mod := 11
return mod - ((i + sum) % mod)
}

func getCharSum(part1 string) int {
charMap := getCharMap()
if len(part1) == 1 {
return 324 + charMap[part1]*8
}

total := 0
weight := getWeight()
for k, v := range part1 {
total += weight[k] * charMap[string(v)]
}
return total
}

func getWeight() map[int]int {
weight := make(map[int]int)
weight[0] = 9
weight[1] = 8
return weight
}

func getCharMap() map[string]int {
asciiNum := 65 // Uppercase A
idCharMap := make(map[string]int)

for i := 0; i <= 26; i++ {
idCharMap[string(i+asciiNum)] = 10 + i
}

return idCharMap
}

func validatePatten(string string) (*Hkid, error) {
r, _ := regexp.Compile("^(?P<p1>\\D{1,2})(?P<p2>\\d{6})\\((?P<p3>[\\w{1}0-9aA])\\)$")
match := r.FindStringSubmatch(string)
if len(match) == 0 {
return NewHkidNil(), NewPatterNotMatchError()
}
return NewHkid(match[1], match[2], match[3]), nil
}
19 changes: 19 additions & 0 deletions hkid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package go_hkid

func CheckString(string string) *HkidResult {
hkid, e := validatePatten(string)
if e != nil {
return NewHkidResultWithValid(NewHkidNil(), false)
}

if getRemainder(hkid) == hkid.part3 {
return NewHkidResultWithValid(hkid, true)
}

return NewHkidResultWithValid(hkid, false)
}

func CheckPart(part1 string, part2 string, part3 string) *HkidResult {
hkid := NewHkid(part1, part2, part3)
return CheckString(hkid.Format())
}
8 changes: 8 additions & 0 deletions hkidInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package go_hkid

type hkidInterface interface {
GetPart1() string
GetPart2() string
GetPart3() string
Format() string
}
26 changes: 26 additions & 0 deletions hkid_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package go_hkid

type HkidResult struct {
hkid *Hkid
Valid bool
}

func (h *HkidResult) GetPart1() string {
return h.hkid.GetPart1()
}

func (h *HkidResult) GetPart2() string {
return h.hkid.GetPart2()
}

func (h *HkidResult) GetPart3() string {
return h.hkid.GetPart3()
}

func (h *HkidResult) Format() string {
return h.hkid.Format()
}

func NewHkidResultWithValid(hkid *Hkid, valid bool) *HkidResult {
return &HkidResult{hkid: hkid, Valid: valid}
}
33 changes: 33 additions & 0 deletions hkid_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package go_hkid

import "fmt"

type Hkid struct {
part1 string
part2 string
part3 string
}

func NewHkidNil() *Hkid {
return &Hkid{}
}

func NewHkid(part1 string, part2 string, part3 string) *Hkid {
return &Hkid{part1: clearString(part1), part2: part2, part3: clearString(part3)}
}

func (hkid *Hkid) GetPart1() string {
return hkid.part1
}

func (hkid *Hkid) GetPart2() string {
return hkid.part2
}

func (hkid *Hkid) GetPart3() string {
return hkid.part3
}

func (hkid *Hkid) Format() string {
return fmt.Sprintf("%s%s(%s)", hkid.part1, hkid.part2, hkid.part3)
}

0 comments on commit 2f8cd98

Please sign in to comment.