Skip to content

Commit

Permalink
implement support for defaultArg in update expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Mar 3, 2016
1 parent e1d21d3 commit 9e30e85
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/FSharp.DynamoDB/Expression/UpdateExpr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ let extractUpdateOps (exprs : IntermediateUpdateExprs) =
| SpecificCall2 <@ List.append @> (None, _, _, [left; right]) ->
UpdateValue.EList_Append (extractOperand pickler left) (extractOperand pickler right)

| SpecificCall2 <@ defaultArg @> (None, _, _, [AttributeGet attr; operand]) ->
match extractOperand attr.Pickler operand with
| Undefined -> invalidExpr()
| op -> If_Not_Exists(attr.Id, op)

| _ -> extractOperand pickler expr |> Operand

let rec extractUpdateOp (parent : QuotedAttribute) (expr : Expr) : UpdateOperation =
Expand Down
5 changes: 5 additions & 0 deletions src/FSharp.DynamoDB/Picklers/PrimitivePicklers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ type OptionPickler<'T>(tp : Pickler<'T>) =
override __.DefaultValue = None
override __.Pickle topt = match topt with None -> None | Some t -> tp.Pickle t
override __.UnPickle a = if a.NULL then None else Some(tp.UnPickle a)
override __.PickleCoerced obj =
match obj with
| :? 'T as t -> tp.Pickle t
| :? ('T option) as topt -> __.Pickle topt
| _ -> raise <| new InvalidCastException()

type StringRepresentationPickler<'T>(ep : StringRepresentablePickler<'T>) =
inherit Pickler<'T> ()
Expand Down
14 changes: 14 additions & 0 deletions tests/FSharp.DynamoDB.Tests/UpdateExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ type ``Update Expression Tests`` () =
let item' = table.UpdateItem(key, <@ fun (r : R) -> { r with List = 1L :: r.List } @>)
item'.List |> should equal [1L ; 2L]

[<Fact>]
let ``Update using defaultArg combinator (Some)`` () =
let item = { mkItem() with Optional = Some (guid()) }
let key = table.PutItem item
let item' = table.UpdateItem(key, <@ fun (r : R) -> { r with String = defaultArg r.Optional "<undefined>" } @>)
item'.String |> should equal item.Optional.Value

[<Fact>]
let ``Update using defaultArg combinator (None)`` () =
let item = { mkItem() with Optional = None }
let key = table.PutItem item
let item' = table.UpdateItem(key, <@ fun (r : R) -> { r with String = defaultArg r.Optional "<undefined>" } @>)
item'.String |> should equal "<undefined>"

[<Fact>]
let ``Update set with add element`` () =
let item = { mkItem() with Set = set [1L;2L] }
Expand Down

0 comments on commit 9e30e85

Please sign in to comment.