这是一个专注于每个字段的自定义验证和错误处理的解决方案。对于仅包含数字数据的数据文件,这可能是矫枉过正!
首先,对于这类事情,我喜欢使用Microsoft.VisualBasic.dll 中的解析器,因为它在不使用 NuGet 的情况下已经可用。
对于每一行,我们可以返回字段数组,以及行号(用于报错)
#r "Microsoft.VisualBasic.dll"
// for each row, return the line number and the fields
let parserReadAllFields fieldWidths textReader =
let parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader=textReader)
parser.SetFieldWidths fieldWidths
parser.TextFieldType <- Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
seq {while not parser.EndOfData do
yield parser.LineNumber,parser.ReadFields() }
接下来,我们需要一个小的错误处理库(更多信息请参阅http://fsharpforfunandprofit.com/rop/)
type Result<'a> =
| Success of 'a
| Failure of string list
module Result =
let succeedR x =
Success x
let failR err =
Failure [err]
let mapR f xR =
match xR with
| Success a -> Success (f a)
| Failure errs -> Failure errs
let applyR fR xR =
match fR,xR with
| Success f,Success x -> Success (f x)
| Failure errs,Success _ -> Failure errs
| Success _,Failure errs -> Failure errs
| Failure errs1, Failure errs2 -> Failure (errs1 @ errs2)
然后定义您的域模型。在这种情况下,它是文件中每个字段都有一个字段的记录类型。
type MyRecord =
{id:int; name:string; description:string}
然后您可以定义特定于域的解析代码。对于每个字段,我都创建了一个验证函数(validateId、validateName 等)。
不需要验证的字段可以通过原始数据 (validateDescription)。
在fieldsToRecord 中,各种字段使用应用样式(<!> 和<*>)进行组合。
有关这方面的更多信息,请参阅http://fsharpforfunandprofit.com/posts/elevated-world-3/#validation。
最后,readRecords 将每个输入行映射到记录 Result 并仅选择成功的行。失败的将写入handleResult 的日志。
module MyFileParser =
open Result
let createRecord id name description =
{id=id; name=name; description=description}
let validateId (lineNo:int64) (fields:string[]) =
let rawId = fields.[0]
match System.Int32.TryParse(rawId) with
| true, id -> succeedR id
| false, _ -> failR (sprintf "[%i] Can't parse id '%s'" lineNo rawId)
let validateName (lineNo:int64) (fields:string[]) =
let rawName = fields.[1]
if System.String.IsNullOrWhiteSpace rawName then
failR (sprintf "[%i] Name cannot be blank" lineNo )
else
succeedR rawName
let validateDescription (lineNo:int64) (fields:string[]) =
let rawDescription = fields.[2]
succeedR rawDescription // no validation
let fieldsToRecord (lineNo,fields) =
let (<!>) = mapR
let (<*>) = applyR
let validatedId = validateId lineNo fields
let validatedName = validateName lineNo fields
let validatedDescription = validateDescription lineNo fields
createRecord <!> validatedId <*> validatedName <*> validatedDescription
/// print any errors and only return good results
let handleResult result =
match result with
| Success record -> Some record
| Failure errs -> printfn "ERRORS %A" errs; None
/// return a sequence of records
let readRecords parserOutput =
parserOutput
|> Seq.map fieldsToRecord
|> Seq.choose handleResult
下面是一个实际解析的例子:
// Set up some sample text
let text = """01name1description1
02name2description2
xxname3badid-------
yy badidandname
"""
// create a low-level parser
let textReader = new System.IO.StringReader(text)
let fieldWidths = [| 2; 5; 11 |]
let parserOutput = parserReadAllFields fieldWidths textReader
// convert to records in my domain
let records =
parserOutput
|> MyFileParser.readRecords
|> Seq.iter (printfn "RECORD %A") // print each record
输出将如下所示:
RECORD {id = 1;
name = "name1";
description = "description";}
RECORD {id = 2;
name = "name2";
description = "description";}
ERRORS ["[3] Can't parse id 'xx'"]
ERRORS ["[4] Can't parse id 'yy'"; "[4] Name cannot be blank"]
这绝不是解析文件的最有效方式(我认为 NuGet 上有一些 CSV 解析库可以在解析时进行验证),但它确实展示了如何完全控制验证和错误处理如果你需要的话。