-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create base for customers parser * Define types for Customer * Write parser for Customer table * Add parser for invoices table
- Loading branch information
Showing
7 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Text.Parser.Customers where | ||
|
||
import Text.ParserCombinators.Parsec | ||
import Data.Bag | ||
|
||
type Identifier = Int | ||
type Name = String | ||
data Customer = C { cid :: Identifier, name :: Name } deriving (Show, Eq) | ||
|
||
csvFile :: GenParser Char st (Bag Customer) | ||
csvFile = do | ||
result <- many record | ||
eof | ||
return (Bag result) | ||
|
||
record :: GenParser Char st Customer | ||
record = do | ||
id <- cidCell | ||
separator | ||
name <- nameCell | ||
eol | ||
return (C id name) | ||
|
||
cidCell :: GenParser Char st Identifier | ||
cidCell = do | ||
id <- many digit | ||
return (read id) | ||
|
||
nameCell :: GenParser Char st Name | ||
nameCell = many (noneOf ",\n") | ||
|
||
separator :: GenParser Char st Char | ||
separator = char ',' | ||
|
||
eol :: GenParser Char st Char | ||
eol = char '\n' | ||
|
||
parseCSV :: String -> Either ParseError (Bag Customer) | ||
parseCSV input = parse csvFile "(unknown)" input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Text.Parser.Invoices where | ||
|
||
import Text.ParserCombinators.Parsec | ||
import Data.Bag | ||
|
||
type Identifier = Int | ||
type Date = Int | ||
type Amount = Int | ||
|
||
data Invoice = I | ||
{ iid :: Identifier | ||
, cust :: Identifier | ||
, due :: Date | ||
, amount :: Amount} deriving (Show, Eq) | ||
|
||
csvFile :: GenParser Char st (Bag Invoice) | ||
csvFile = do | ||
result <- many invoiceRecord | ||
eof | ||
return (Bag result) | ||
|
||
invoiceRecord :: GenParser Char st Invoice | ||
invoiceRecord = do | ||
iidCell <- identifier | ||
separator | ||
custIdCell <- identifier | ||
separator | ||
dueCell <- date | ||
separator | ||
amountCell <- price | ||
eol | ||
return (I iidCell custIdCell dueCell amountCell) | ||
|
||
identifier :: GenParser Char st Identifier | ||
identifier = do | ||
id <- many digit | ||
return (read id) | ||
|
||
price :: GenParser Char st Amount | ||
price = do | ||
id <- many digit | ||
return (read id) | ||
|
||
date :: GenParser Char st Date | ||
date = do | ||
id <- many digit | ||
return (read id) | ||
|
||
separator :: GenParser Char st Char | ||
separator = char ',' | ||
|
||
eol :: GenParser Char st Char | ||
eol = char '\n' | ||
|
||
parseCSV :: String -> Either ParseError (Bag Invoice) | ||
parseCSV input = parse csvFile "(unknown)" input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Text.Parser.CustomersSpec where | ||
|
||
import Test.Hspec | ||
import Text.Parser.Customers | ||
import Data.Bag as Bag | ||
import Text.ParserCombinators.Parsec | ||
import Data.Either | ||
|
||
expectedOutput = Bag [C 1 "John", C 2 "Kayla"] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "parseCSV" $ do | ||
it "can correctly parse the example" $ do | ||
testFile <- readFile "test/Text/Parser/customertest.csv" | ||
fromRight Bag.empty (parseCSV testFile) `shouldBe` expectedOutput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Text.Parser.InvoicesSpec where | ||
|
||
import Test.Hspec | ||
import Text.Parser.Invoices | ||
import Data.Bag as Bag | ||
import Text.ParserCombinators.Parsec | ||
import Data.Either | ||
|
||
expectedOutput = Bag [I 10 1 20160101 10, I 11 1 20160102 83, I 12 2 20160103 15] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "parseCSV" $ do | ||
it "can correctly parse the example" $ do | ||
testFile <- readFile "test/Text/Parser/invoicestest.csv" | ||
fromRight Bag.empty (parseCSV testFile) `shouldBe` expectedOutput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1,John | ||
2,Kayla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
10,1,20160101,10 | ||
11,1,20160102,83 | ||
12,2,20160103,15 |