diff options
Diffstat (limited to 'mintage')
-rw-r--r-- | mintage/parser.go | 33 | ||||
-rw-r--r-- | mintage/parser_test.go | 17 |
2 files changed, 28 insertions, 22 deletions
diff --git a/mintage/parser.go b/mintage/parser.go index 242a3bb..4c5e6f9 100644 --- a/mintage/parser.go +++ b/mintage/parser.go @@ -27,8 +27,9 @@ func (e SyntaxError) Error() string { } type Row struct { - Label string - Cols [8]int + Year int + Mintmark string + Cols [8]int } type Set struct { @@ -36,11 +37,17 @@ type Set struct { Circ, BU, Proof []Row } +func (r Row) Label() string { + if r.Mintmark != "" { + return fmt.Sprintf("%d %s", r.Year, r.Mintmark) + } + return strconv.Itoa(r.Year) +} + func Parse(reader io.Reader, file string) (Set, error) { var ( data Set // Our data struct slice *[]Row // Where to append mintages - year int // The current year we are at ) scanner := bufio.NewScanner(reader) @@ -86,8 +93,6 @@ func Parse(reader io.Reader, file string) (Set, error) { } data.StartYear, _ = strconv.Atoi(arg) } - - year = data.StartYear - 1 case isLabel(tokens[0]): n := len(tokens[0]) if n > 2 && tokens[0][n-2] == '*' { @@ -140,16 +145,14 @@ func Parse(reader io.Reader, file string) (Set, error) { } } - var row Row - switch { - case mintmark.s == "": - year++ - row.Label = strconv.Itoa(year) - case mintmark.star: - year++ - fallthrough - default: - row.Label = fmt.Sprintf("%d %s", year, mintmark.s) + row := Row{Mintmark: mintmark.s} + if len(*slice) == 0 { + row.Year = data.StartYear + } else { + row.Year = (*slice)[len(*slice)-1].Year + if row.Mintmark == "" || mintmark.star { + row.Year++ + } } for i, tok := range tokens { diff --git a/mintage/parser_test.go b/mintage/parser_test.go index 0a67685..dd78c71 100644 --- a/mintage/parser_test.go +++ b/mintage/parser_test.go @@ -128,14 +128,17 @@ func TestParserMintmarks(t *testing.T) { } } - if data.Circ[0].Label != "2020" { - t.Fatalf(`Expected data.Circ[0].Label="2020"; got %s`, data.Circ[0].Label) - } - if data.Circ[1].Label != "2021\u00A0KNM" { - t.Fatalf(`Expected data.Circ[1].Label="2021 KNM"; got %s`, data.Circ[1].Label) + for i, y := range [...]int{2020, 2021, 2021} { + if data.Circ[i].Year != y { + t.Fatalf("Expected data.Circ[%d].Year=%d; got %d", + i, y, data.Circ[i].Year) + } } - if data.Circ[2].Label != "2021\u00A0MdP" { - t.Fatalf(`Expected data.Circ[2].Label="2021 MdP"; got %s`, data.Circ[2].Label) + for i, s := range [...]string{"", "KNM", "MdP"} { + if data.Circ[i].Mintmark != s { + t.Fatalf(`Expected data.Circ[%d].Mintmark="%s"; got "%s"`, + i, s, data.Circ[i].Mintmark) + } } } |