Skip to content

Commit

Permalink
Refactor to matching coding guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
rexcfnghk committed Jun 9, 2024
1 parent 0434c02 commit c53fcb4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions DataParser.Console/DataFileParseResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
open DataParser.Console.DataFiles

type DataFileParseResult =
{ dataFileName : DataFileName
jsonElements : JsonObject seq }
{ DataFileName : DataFileName
JsonElements : JsonObject seq }

module DataFileParseResult =

let iter f x = ignore (f x.dataFileName x.jsonElements)
let iter f x = ignore (f x.DataFileName x.JsonElements)
22 changes: 17 additions & 5 deletions DataParser.Console/DataFiles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ open System.IO
open System.Text
open System.Text.RegularExpressions
open DataParser.Console.Core
open DataParser.Console.Result
open DataParser.Console.FormatFiles

let [<Literal>] trueByteLiteral = 49uy
let [<Literal>] falseByteLiteral = 48uy
let [<Literal>] TrueByte = 49uy
let [<Literal>] FalseByte = 48uy

type DataFileName = DataFileName of fileFormat: FormatName * rawDate : string

type JsonObject = Map<string, obj>

type DataFileFormat =
{ FilePath: string
Name: DataFileName
FormatLines: FormatLine list }

let lookupFormatName availableFormats given =
if Set.contains given availableFormats
then Ok given
else Error <| FileFormatNotFound (availableFormats, given)

let collectFormatNames = Set.ofList

let dataFileNameRegex =
Regex(@"^(.+)_(\d\d\d\d-\d\d-\d\d)$", RegexOptions.Compiled ||| RegexOptions.Singleline ||| RegexOptions.CultureInvariant)
Expand All @@ -30,10 +34,18 @@ let parseDataFileName s =
then Ok <| DataFileName (FormatName regexMatch.Groups[1].Value, regexMatch.Groups[2].Value)
else Error <| DataFileNameFormatError s

let getDataFileFormat (fileFormatLookup: Map<FormatName, FormatLine list>) (filePath, fileName) =
let fileFormatSet = fileFormatLookup.Keys |> Set.ofSeq
result {
let! dataFileName as DataFileName (fileFormat, _) = parseDataFileName fileName
let! formatName = lookupFormatName fileFormatSet fileFormat
return { Name = dataFileName; FormatLines = fileFormatLookup[formatName]; FilePath = filePath }
}

let parseDataFileLine (formatLines : FormatLine list) (dataFileLine : string) : Result<JsonObject, Error> =
let parseDataType dataType (s: byte array) : Result<obj, Error> =
match dataType with
| JBool -> if s = [|trueByteLiteral|] then Ok true elif s = [|falseByteLiteral|] then Ok false else Error (UnparsableValue s)
| JBool -> if s = [|TrueByte|] then Ok true elif s = [|FalseByte|] then Ok false else Error (UnparsableValue s)
| JInt -> match System.Int32.TryParse (s, CultureInfo.InvariantCulture) with true, i -> Ok i | false, _ -> Error (UnparsableValue s)
| JString -> Ok <| let result = Encoding.UTF8.GetString s in result.Trim()

Expand Down
19 changes: 3 additions & 16 deletions DataParser.Console/FileRead.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@ open DataParser.Console.FormatFiles
open DataParser.Console.DataFiles
open System.IO

type DataFileFormat =
{ filePath: string
name: DataFileName
formatLines: FormatLine list }

let readAllSpecFiles folderPath =
Directory.GetFiles(folderPath, "*.csv")
|> Array.map (fun x -> FormatName (Path.GetFileNameWithoutExtension x), parseFormatFile (File.ReadAllText x))
|> Map.ofArray
|> mapSequenceResult

let getDataFileFormat (fileFormatLookup: Map<FormatName, FormatLine list>) (filePath, fileName) =
let fileFormatSet = fileFormatLookup.Keys |> Set.ofSeq
result {
let! dataFileName as DataFileName (fileFormat, _) = parseDataFileName fileName
let! formatName = lookupFormatName fileFormatSet fileFormat
return { name = dataFileName; formatLines = fileFormatLookup[formatName]; filePath = filePath }
}

let parseDataFile dataFile =
File.ReadLines dataFile.filePath
|> seqTraverseResult (parseDataFileLine dataFile.formatLines)
|> Result.map (fun jsonObject -> { dataFileName = dataFile.name; jsonElements = jsonObject })
File.ReadLines dataFile.FilePath
|> seqTraverseResult (parseDataFileLine dataFile.FormatLines)
|> Result.map (fun jsonObject -> { DataFileName = dataFile.Name; JsonElements = jsonObject })

let readDataFiles folderPath (fileFormatLookup: Map<FormatName, FormatLine list>) =
Directory.GetFiles(folderPath, "*.txt")
Expand Down

0 comments on commit c53fcb4

Please sign in to comment.