Skip to content

Commit

Permalink
test + document
Browse files Browse the repository at this point in the history
  • Loading branch information
okieraised authored Aug 26, 2024
1 parent 3ac1c32 commit a85cf53
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 11 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
# go-mrz-scanner
# go-mrz-scanner

Utility package to validate and parse the MRZ string.

## Installation
To install this package, run:
```shell
go get -u github.com/okieraised/go-mrz-scanner
```

## Usage
Below is an example to parse the MRZ components:
```go
package main

import (
"fmt"
go_mrz_parser "github.com/okieraised/go-mrz-scanner"
"log"
)

func main() {

input := []string{
"I<UTOD231458907<<<<<<<<<<<<<<<",
"7408122F1204159UTO<<<<<<<<<<<6",
"ERIKSSON<<ANNA<MARIA<<<<<<<<<<",
}

parser := go_mrz_parser.NewMRZLineParser(input)
result, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
```
23 changes: 23 additions & 0 deletions example/parser/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
go_mrz_parser "github.com/okieraised/go-mrz-scanner"
"log"
)

func main() {

input := []string{
"I<UTOD231458907<<<<<<<<<<<<<<<",
"7408122F1204159UTO<<<<<<<<<<<6",
"ERIKSSON<<ANNA<MARIA<<<<<<<<<<",
}

parser := go_mrz_parser.NewMRZLineParser(input)
result, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
16 changes: 8 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package go_mrz_parser

import (
"fmt"
"github.com/okieraised/go-mrz-scanner/constants"
"github.com/okieraised/go-mrz-scanner/internal/parser"
"github.com/okieraised/go-mrz-scanner/mrz_errors"
Expand All @@ -22,17 +21,19 @@ func NewMRZStringParser(mrzStr string) *MRZParser {
}
}

// NewMRZLineParser receives the mrz string slices
func NewMRZLineParser(mrzLines []string) *MRZParser {
return &MRZParser{
components: mrzLines,
}
}

func (p *MRZParser) Parse() error {
// Parse validates and parses the MRZ information
func (p *MRZParser) Parse() (*parser.ParserResult, error) {

err := p.validate()
if err != nil {
return err
return &parser.ParserResult{}, err
}

var mrzParser parser.IMRZParser
Expand All @@ -44,19 +45,18 @@ func (p *MRZParser) Parse() error {
case constants.MRZType3:
mrzParser = parser.NewTD3()
default:
return mrz_errors.ErrInvalidMRZType
return &parser.ParserResult{}, mrz_errors.ErrInvalidMRZType
}

parse, err := mrzParser.Parse(p.components)
if err != nil {
return err
return &parser.ParserResult{}, err
}

fmt.Println(parse)

return nil
return parse, nil
}

// validate checks the input MRZ for formatting errors
func (p *MRZParser) validate() error {
mrzType := 0

Expand Down
23 changes: 23 additions & 0 deletions parser/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
go_mrz_parser "github.com/okieraised/go-mrz-scanner"
"log"
)

func main() {

input := []string{
"I<UTOD231458907<<<<<<<<<<<<<<<",
"7408122F1204159UTO<<<<<<<<<<<6",
"ERIKSSON<<ANNA<MARIA<<<<<<<<<<",
}

parser := go_mrz_parser.NewMRZLineParser(input)
result, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
31 changes: 29 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package go_mrz_parser

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -12,8 +13,9 @@ func TestMRZParser_Parse_TD1(t *testing.T) {
"ERIKSSON<<ANNA<MARIA<<<<<<<<<<",
})

err := parser.Parse()
result, err := parser.Parse()
assert.NoError(t, err)
fmt.Println(result)
}

func TestMRZParser_Parse_TD2(t *testing.T) {
Expand All @@ -22,6 +24,31 @@ func TestMRZParser_Parse_TD2(t *testing.T) {
"D231458907UTO7408122F1204159<<<<<<<6",
})

err := parser.Parse()
result, err := parser.Parse()
assert.NoError(t, err)
fmt.Println(result)
}

func TestMRZParser_Parse_TD3(t *testing.T) {
parser := NewMRZLineParser([]string{
"P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<",
"L898902C36UTO7408122F1204159ZE184226B<<<<<10",
})

result, err := parser.Parse()
assert.NoError(t, err)
assert.Equal(t, false, result.IsVISA)
assert.Equal(t, true, result.IsValid)
}

func TestMRZParser_Parse_TD3_VISA(t *testing.T) {
parser := NewMRZLineParser([]string{
"V<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<",
"L8988901C4XXX4009078F96121096ZE184226B<<<<<<",
})

result, err := parser.Parse()
assert.NoError(t, err)
assert.Equal(t, true, result.IsVISA)
assert.Equal(t, true, result.IsValid)
}

0 comments on commit a85cf53

Please sign in to comment.