Skip to content

Commit

Permalink
Simplify json datatype parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rexcfnghk committed Jun 24, 2024
1 parent 79f4928 commit 33cc622
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions DataParser.Console/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Error =
| FileFormatNotFound of availableFormats: Set<FormatName> * givenFormat : FormatName
| DataFileNameFormatError of fileName: FileNameWithoutExtension
| UnexpectedFormatHeader of string
| UnexpectedJsonDataType of string
| UnexpectedFormatLine of string
| UnparsableValue of string
| DataFileLineLengthShorterThanSpec of string
Expand Down
7 changes: 4 additions & 3 deletions DataParser.Console/FormatFiles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ type FormatLine = FormatLine of columnName: string * width: int * dataType : Jso

let headerRegexLookup = dict [ ("\"column name\"", "(?<name>.+)"); ("width", "(?<width>\d+)"); ("datatype", "(?<type>.+)") ]

let parseJsonDataType line = function
let parseJsonDataType rawDataType =
match rawDataType with
| "TEXT" -> Ok JString
| "BOOLEAN" -> Ok JBool
| "INTEGER" -> Ok JInt
| _ -> Error [ UnexpectedFormatLine line ]
| _ -> Error [ UnexpectedJsonDataType rawDataType ]

let parseFormatLine (regex: Regex) line =
let regexMatch = regex.Match line
if regexMatch.Success
then
let regexGroups = regexMatch.Groups
let jsonDataType = parseJsonDataType line regexGroups["type"].Value
let jsonDataType = parseJsonDataType regexGroups["type"].Value
match jsonDataType with
| Ok jsonDataType ->
Ok <| FormatLine (regexGroups["name"].Value, Int32.Parse(regexGroups["width"].Value), jsonDataType)
Expand Down
4 changes: 2 additions & 2 deletions DataParser.Tests/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

open DataParser.Console.FormatFiles

let forceParseJsonType line s =
match parseJsonDataType line s with
let forceParseJsonType s =
match parseJsonDataType s with
| Ok x -> x
| Error _ -> raise (invalidArg (nameof s) "Invalid json data type cannot be parsed")
8 changes: 7 additions & 1 deletion DataParser.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ let ``parseFormatLine returns error when line does not conform to expected forma
let regex = Regex("^\n$")

parseFormatLine regex s =! expected

[<Property>]
let ``parseJsonDataType returns expected error when data type does not conform to expected format`` (s: string) =
let expected : Result<JsonDataType, Error list> = Error [ UnexpectedJsonDataType s ]

parseJsonDataType s =! expected

[<Xunit.Theory>]
[<Xunit.InlineData("name", 10, "TEXT")>]
[<Xunit.InlineData("valid", 1, "BOOLEAN")>]
[<Xunit.InlineData("count", 3, "INTEGER")>]
let ``parseFormatLine returns expected FormatLine when line conforms to regex`` (columnName, width, dataType) =
let line = $"{columnName},{width},{dataType}"
let jsonType = forceParseJsonType line dataType
let jsonType = forceParseJsonType dataType
let expected = Ok <| FormatLine (columnName, width, jsonType)
let regex = Regex("^(?<name>.+),(?<width>\d+),(?<type>.+)$")

Expand Down

0 comments on commit 33cc622

Please sign in to comment.