diff options
Diffstat (limited to 'lib/mintage')
-rw-r--r-- | lib/mintage/parser.go | 32 | ||||
-rw-r--r-- | lib/mintage/parser_test.go | 44 |
2 files changed, 43 insertions, 33 deletions
diff --git a/lib/mintage/parser.go b/lib/mintage/parser.go index 4c5e6f9..fb92e64 100644 --- a/lib/mintage/parser.go +++ b/lib/mintage/parser.go @@ -9,6 +9,15 @@ import ( "unicode" ) +type CoinType int + +const ( + TypeCirculated CoinType = iota + TypeNIFC + TypeProof + coinTypes +) + const ( _ = -iota Unknown // Unknown mintage @@ -33,8 +42,8 @@ type Row struct { } type Set struct { - StartYear int - Circ, BU, Proof []Row + StartYear int + Tables [coinTypes][]Row } func (r Row) Label() string { @@ -46,8 +55,8 @@ func (r Row) Label() string { func Parse(reader io.Reader, file string) (Set, error) { var ( - data Set // Our data struct - slice *[]Row // Where to append mintages + data Set + ctype CoinType = -1 ) scanner := bufio.NewScanner(reader) @@ -77,11 +86,11 @@ func Parse(reader io.Reader, file string) (Set, error) { switch arg { case "CIRC": - slice = &data.Circ + ctype = TypeCirculated case "BU": - slice = &data.BU + ctype = TypeNIFC case "PROOF": - slice = &data.Proof + ctype = TypeProof default: if !isNumeric(arg, false) { return Set{}, SyntaxError{ @@ -113,7 +122,7 @@ func Parse(reader io.Reader, file string) (Set, error) { fallthrough case isNumeric(tokens[0], true), tokens[0] == "?": switch { - case slice == nil: + case ctype == -1: return Set{}, SyntaxError{ expected: "coin type declaration", got: tokens[0], @@ -146,10 +155,11 @@ func Parse(reader io.Reader, file string) (Set, error) { } row := Row{Mintmark: mintmark.s} - if len(*slice) == 0 { + if len(data.Tables[ctype]) == 0 { row.Year = data.StartYear } else { - row.Year = (*slice)[len(*slice)-1].Year + rows := data.Tables[ctype] + row.Year = rows[len(rows)-1].Year if row.Mintmark == "" || mintmark.star { row.Year++ } @@ -162,7 +172,7 @@ func Parse(reader io.Reader, file string) (Set, error) { row.Cols[i] = atoiWithDots(tok) } } - *slice = append(*slice, row) + data.Tables[ctype] = append(data.Tables[ctype], row) default: return Set{}, SyntaxError{ expected: "‘BEGIN’ directive or mintage row", diff --git a/lib/mintage/parser_test.go b/lib/mintage/parser_test.go index dd78c71..3cc8ea3 100644 --- a/lib/mintage/parser_test.go +++ b/lib/mintage/parser_test.go @@ -28,7 +28,7 @@ func TestParserComplete(t *testing.T) { data.StartYear) } - for i, row := range data.Circ { + for i, row := range data.Tables[TypeCirculated] { for j, col := range row.Cols { var n int if i == 1 && j == 1 { @@ -37,12 +37,12 @@ func TestParserComplete(t *testing.T) { n = 1000*i + j + 1000 } if col != n { - t.Fatalf("Expected data.Circ[i][j]=%d; got %d", n, col) + t.Fatalf("Expected data.Tables[TypeCirculated][i][j]=%d; got %d", n, col) } } } - for i, row := range data.BU { + for i, row := range data.Tables[TypeNIFC] { for j, col := range row.Cols { var n int if i == 1 && j == 1 { @@ -51,12 +51,12 @@ func TestParserComplete(t *testing.T) { n = 1000*i + j + 1100 } if col != n { - t.Fatalf("Expected data.BU[i][j]=%d; got %d", n, col) + t.Fatalf("Expected data.Tables[TypeNIFC][i][j]=%d; got %d", n, col) } } } - for i, row := range data.Proof { + for i, row := range data.Tables[TypeProof] { for j, col := range row.Cols { var n int if i == 1 && j == 1 { @@ -65,19 +65,19 @@ func TestParserComplete(t *testing.T) { n = 1000*i + j + 1200 } if col != n { - t.Fatalf("Expected data.Proof[i][j]=%d; got %d", n, col) + t.Fatalf("Expected data.Tables[TypeProof][i][j]=%d; got %d", n, col) } } } - if len(data.Circ) != 2 { - t.Fatalf("Expected len(data.Circ)=2; got %d", len(data.Circ)) + if len(data.Tables[TypeCirculated]) != 2 { + t.Fatalf("Expected len(data.Tables[TypeCirculated])=2; got %d", len(data.Tables[TypeCirculated])) } - if len(data.BU) != 2 { - t.Fatalf("Expected len(data.BU)=2; got %d", len(data.BU)) + if len(data.Tables[TypeNIFC]) != 2 { + t.Fatalf("Expected len(data.Tables[TypeNIFC])=2; got %d", len(data.Tables[TypeNIFC])) } - if len(data.Proof) != 2 { - t.Fatalf("Expected len(data.Proof)=2; got %d", len(data.Proof)) + if len(data.Tables[TypeProof]) != 2 { + t.Fatalf("Expected len(data.Tables[TypeProof])=2; got %d", len(data.Tables[TypeProof])) } } @@ -96,8 +96,8 @@ func TestParserNoProof(t *testing.T) { t.Fatalf(`Expected err=nil; got "%s"`, err) } - if len(data.Proof) != 0 { - t.Fatalf("Expected len(data.Proof)=0; got %d", len(data.Proof)) + if len(data.Tables[TypeProof]) != 0 { + t.Fatalf("Expected len(data.Tables[TypeProof])=0; got %d", len(data.Tables[TypeProof])) } } @@ -114,7 +114,7 @@ func TestParserMintmarks(t *testing.T) { t.Fatalf(`Expected err=nil; got "%s"`, err) } - for i, row := range data.Circ { + for i, row := range data.Tables[TypeCirculated] { for j, col := range row.Cols { var n int if i > 0 && j == 1 { @@ -123,21 +123,21 @@ func TestParserMintmarks(t *testing.T) { n = 1000*i + j + 1000 } if col != n { - t.Fatalf("Expected data.Circ[i][j]=%d; got %d", n, col) + t.Fatalf("Expected data.Tables[TypeCirculated][i][j]=%d; got %d", n, col) } } } 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.Tables[TypeCirculated][i].Year != y { + t.Fatalf("Expected data.Tables[TypeCirculated][%d].Year=%d; got %d", + i, y, data.Tables[TypeCirculated][i].Year) } } 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) + if data.Tables[TypeCirculated][i].Mintmark != s { + t.Fatalf(`Expected data.Tables[TypeCirculated][%d].Mintmark="%s"; got "%s"`, + i, s, data.Tables[TypeCirculated][i].Mintmark) } } } |