-
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.
rework to allow for optional and required missing values
- Loading branch information
Showing
6 changed files
with
281 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,52 @@ | ||
namespace LitXml | ||
|
||
open System.Xml | ||
open Microsoft.FSharp.Quotations | ||
open Expression | ||
|
||
[<AutoOpen>] | ||
module DSL = | ||
type DSL = | ||
|
||
let inline elem name = ElementBuilder(name) | ||
static member inline elem name = ElementBuilder(name) | ||
|
||
let inline attr name value = | ||
Attr(fun tb -> | ||
static member inline attr name value : Attr = | ||
ok (fun tb -> | ||
tb.WriteAttributeString(name, string value) | ||
) | ||
|
||
let inline value (s: 'a) : Value = | ||
Value(fun tb -> | ||
static member inline value (s: 'a when 'a :> System.IEquatable<'a>) : Value = | ||
ok (fun tb -> | ||
tb.WriteString(string s) | ||
) | ||
|
||
static member inline value (s : Expr<'a> when 'a :> System.IEquatable<'a>) : Value = | ||
try | ||
let value = eval<'a> s | ||
let f = (fun (sb : XmlWriter) -> | ||
sb.WriteString(string value) | ||
) | ||
ok f | ||
with | ||
| err -> MissingRequired([err.Message]) | ||
|
||
|
||
static member opt (elem : Element) = | ||
match elem with | ||
| Ok (f,messages) -> elem | ||
| MissingOptional (messages) -> Ok((fun tb -> ()),messages) | ||
| MissingRequired (messages) -> Ok((fun tb -> ()),messages) | ||
|
||
static member opt (elem : Expr<Element>) = | ||
try | ||
let elem = eval<Element> elem | ||
match elem with | ||
| Ok (f,messages) -> elem | ||
| MissingOptional (messages) -> Ok((fun tb -> ()),messages) | ||
| MissingRequired (messages) -> Ok((fun tb -> ()),messages) | ||
with | ||
| err -> | ||
let f = (fun (sb : XmlWriter) -> | ||
() | ||
) | ||
Ok(f,[err.Message]) | ||
|
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,93 @@ | ||
namespace LitXml | ||
|
||
open System.Xml | ||
|
||
/// | ||
type Message = string | ||
|
||
/// Xml node with instructions about xml elements, values and attributes to be written by an xml writer. | ||
[<AutoOpen>] | ||
type XmlPart = | ||
/// Node could be computed and contains writer instructions. | ||
| Ok of (XmlWriter -> unit) * Message list | ||
/// Node could not be computed, but was optional. | ||
| MissingOptional of Message list | ||
/// Node could not be computed, but was required. | ||
| MissingRequired of Message list | ||
|
||
static member ok (f : XmlWriter -> unit) : XmlPart = XmlPart.Ok (f,[]) | ||
|
||
/// Write xml node content to a xml writer. | ||
member this.Invoke(xml : XmlWriter) = | ||
|
||
match this with | ||
| Ok (f,errs) -> f xml | ||
| MissingOptional errs -> | ||
printfn "No function to invoke, Missings:" | ||
errs | ||
|> List.iter (printfn "\t %s") | ||
| MissingRequired errs -> | ||
printfn "No function to invoke, Missings:" | ||
errs | ||
|> List.iter (printfn "\t %s") | ||
|
||
/// Get messages | ||
member this.Messages = | ||
|
||
match this with | ||
| Ok (f,errs) -> errs | ||
| MissingOptional errs -> errs | ||
| MissingRequired errs -> errs | ||
|
||
/// Write xml node content to a xml writer and close it. | ||
member this.WriteTo(writer : XmlWriter) = | ||
match this with | ||
| Ok (f,messages) -> | ||
f writer | ||
writer.Flush() | ||
writer.Close() | ||
| _ -> () | ||
|
||
/// Write xml node content to a memory stream using a xml writer. | ||
/// | ||
/// Writer can be tuned by supplying settings | ||
member this.WriteTo(stream : System.IO.MemoryStream, ?Settings : XmlWriterSettings) = | ||
let settings = Option.defaultValue (XmlWriterSettings()) Settings | ||
match this with | ||
| Ok (f,messages) -> | ||
let writer = XmlWriter.Create(stream,settings) | ||
this.WriteTo(writer) | ||
| _ -> () | ||
|
||
/// Write xml node content to a file path using a xml writer. | ||
/// | ||
/// Writer can be tuned by supplying settings | ||
member this.WriteTo(path : string, ?Settings : XmlWriterSettings) = | ||
let settings = Option.defaultValue (XmlWriterSettings()) Settings | ||
match this with | ||
| Ok (f,messages) -> | ||
let writer = XmlWriter.Create(path, settings) | ||
this.WriteTo(writer) | ||
| _ -> () | ||
|
||
/// Write xml node content to a string using a xml writer. | ||
/// | ||
/// Writer can be tuned by supplying settings | ||
member this.WriteToString(?Settings : XmlWriterSettings) = | ||
let settings = Option.defaultValue (XmlWriterSettings()) Settings | ||
match this with | ||
| Ok (f,messages) -> | ||
let tb = System.Text.StringBuilder() | ||
let writer = XmlWriter.Create(tb,settings) | ||
this.WriteTo(writer) | ||
tb.ToString() | ||
| _ -> "" | ||
|
||
/// Xml Element | ||
type Element = XmlPart | ||
|
||
/// Xml Value | ||
type Value = XmlPart | ||
|
||
/// Xml Attribute | ||
type Attr = XmlPart |
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,8 @@ | ||
namespace LitXml | ||
|
||
open Microsoft.FSharp.Quotations | ||
open Microsoft.FSharp.Linq.RuntimeHelpers | ||
|
||
module Expression = | ||
|
||
let eval<'T> q = LeafExpressionConverter.EvaluateQuotation q :?> 'T |
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,34 @@ | ||
namespace LitXml | ||
|
||
open System.Xml | ||
open Microsoft.FSharp.Quotations | ||
open Expression | ||
|
||
[<AutoOpen>] | ||
module Operators = | ||
|
||
/// Required value operator | ||
/// | ||
/// If expression does fail, returns a missing required value | ||
let inline (!!) (s : Expr<'a> when 'a :> System.IEquatable<'a>) : Value = | ||
try | ||
let value = eval<'a> s | ||
let f = (fun (sb : XmlWriter) -> | ||
sb.WriteString(string value) | ||
) | ||
ok f | ||
with | ||
| err -> MissingRequired([err.Message]) | ||
|
||
/// Optional value operator | ||
/// | ||
/// If expression does fail, returns a missing optional value | ||
let inline (!?) (s : Expr<'a> when 'a :> System.IEquatable<'a>) : Value = | ||
try | ||
let value = eval<'a> s | ||
let f = (fun (sb : XmlWriter) -> | ||
sb.WriteString(string value) | ||
) | ||
ok f | ||
with | ||
| err -> MissingOptional([err.Message]) |