Skip to content

Commit

Permalink
[#29] Templating: fix using a number bind as a text hole
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Mar 11, 2019
1 parent dba8240 commit 29e3e25
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/Bolero.Templating.Provider/ConvertExpr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ let WrapExpr (innerType: Parsing.HoleType) (outerType: Parsing.HoleType) (expr:
if innerType = outerType then None else
match innerType, outerType with
| Parsing.Html, Parsing.String ->
Some <@@ Node.Text %%expr @@>
<@@ Node.Text %%expr @@>
| Parsing.AttributeValue, Parsing.String ->
Some <| Expr.Coerce(expr, typeof<obj>)
Expr.Coerce(expr, typeof<obj>)
| Parsing.Event argTy, Parsing.Event _ ->
Some <| Expr.Coerce(expr, EventHandlerOf argTy)
Expr.Coerce(expr, EventHandlerOf argTy)
| Parsing.String, Parsing.DataBinding _ ->
Some <@@ (%%expr: obj * Action<UIChangeEventArgs>).Item1 @@>
<@@ (%%expr: obj * Action<UIChangeEventArgs>).Item1 @@>
| Parsing.Html, Parsing.DataBinding _ ->
Some <@@ Node.Text ((%%expr: obj * Action<UIChangeEventArgs>).Item1.ToString()) @@>
<@@ Node.Text ((%%expr: obj * Action<UIChangeEventArgs>).Item1.ToString()) @@>
| Parsing.AttributeValue, Parsing.DataBinding _ ->
<@@ (%%expr: obj * Action<UIChangeEventArgs>).Item1.ToString() @@>
| a, b -> failwithf "Hole name used multiple times with incompatible types (%A, %A)" a b
|> Some

/// Map an expression's vars from its parent, wrapping the expression in let declarations.
let WrapAndConvert (vars: Map<string, Expr>) (subst: list<Parsing.VarSubstitution>) convert expr =
Expand Down
14 changes: 9 additions & 5 deletions src/Bolero.Templating/ConvertNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ let TypeOf (holeType: Parsing.HoleType) : Type =
| Parsing.AttributeValue -> typeof<obj>

let WrapExpr (innerType: Parsing.HoleType) (outerType: Parsing.HoleType) (expr: obj) : option<obj> =
let inline bindValueOf (e: obj) = fst (e :?> obj * Action<UIChangeEventArgs>)
if innerType = outerType then None else
match innerType, outerType with
| Parsing.Html, Parsing.String ->
Some (box (Node.Text (unbox expr)))
box (Node.Text (unbox expr))
| Parsing.AttributeValue, Parsing.String ->
box (string expr)
| Parsing.Event _, Parsing.Event _ ->
Some expr
| Parsing.String, Parsing.DataBinding _ ->
Some (fst (expr :?> obj * Action<UIChangeEventArgs>))
expr
| Parsing.Html, Parsing.DataBinding _ ->
Some (box (Node.Text (fst (expr :?> obj * Action<UIChangeEventArgs>) :?> string)))
box (Node.Text (string (bindValueOf expr)))
| (Parsing.String | Parsing.AttributeValue), Parsing.DataBinding _ ->
box (string (bindValueOf expr))
| a, b -> failwithf "Hole name used multiple times with incompatible types (%A, %A)" a b
|> Some

let WrapAndConvert (vars: Map<string, obj>) (subst: list<Parsing.VarSubstitution>) convert expr =
let vars = (vars, subst) ||> List.fold (fun vars wrap ->
Expand Down

0 comments on commit 29e3e25

Please sign in to comment.