Skip to content

Commit 9e30e85

Browse files
implement support for defaultArg in update expressions
1 parent e1d21d3 commit 9e30e85

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/FSharp.DynamoDB/Expression/UpdateExpr.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ let extractUpdateOps (exprs : IntermediateUpdateExprs) =
284284
| SpecificCall2 <@ List.append @> (None, _, _, [left; right]) ->
285285
UpdateValue.EList_Append (extractOperand pickler left) (extractOperand pickler right)
286286

287+
| SpecificCall2 <@ defaultArg @> (None, _, _, [AttributeGet attr; operand]) ->
288+
match extractOperand attr.Pickler operand with
289+
| Undefined -> invalidExpr()
290+
| op -> If_Not_Exists(attr.Id, op)
291+
287292
| _ -> extractOperand pickler expr |> Operand
288293

289294
let rec extractUpdateOp (parent : QuotedAttribute) (expr : Expr) : UpdateOperation =

src/FSharp.DynamoDB/Picklers/PrimitivePicklers.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ type OptionPickler<'T>(tp : Pickler<'T>) =
187187
override __.DefaultValue = None
188188
override __.Pickle topt = match topt with None -> None | Some t -> tp.Pickle t
189189
override __.UnPickle a = if a.NULL then None else Some(tp.UnPickle a)
190+
override __.PickleCoerced obj =
191+
match obj with
192+
| :? 'T as t -> tp.Pickle t
193+
| :? ('T option) as topt -> __.Pickle topt
194+
| _ -> raise <| new InvalidCastException()
190195

191196
type StringRepresentationPickler<'T>(ep : StringRepresentablePickler<'T>) =
192197
inherit Pickler<'T> ()

tests/FSharp.DynamoDB.Tests/UpdateExpressionTests.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ type ``Update Expression Tests`` () =
230230
let item' = table.UpdateItem(key, <@ fun (r : R) -> { r with List = 1L :: r.List } @>)
231231
item'.List |> should equal [1L ; 2L]
232232

233+
[<Fact>]
234+
let ``Update using defaultArg combinator (Some)`` () =
235+
let item = { mkItem() with Optional = Some (guid()) }
236+
let key = table.PutItem item
237+
let item' = table.UpdateItem(key, <@ fun (r : R) -> { r with String = defaultArg r.Optional "<undefined>" } @>)
238+
item'.String |> should equal item.Optional.Value
239+
240+
[<Fact>]
241+
let ``Update using defaultArg combinator (None)`` () =
242+
let item = { mkItem() with Optional = None }
243+
let key = table.PutItem item
244+
let item' = table.UpdateItem(key, <@ fun (r : R) -> { r with String = defaultArg r.Optional "<undefined>" } @>)
245+
item'.String |> should equal "<undefined>"
246+
233247
[<Fact>]
234248
let ``Update set with add element`` () =
235249
let item = { mkItem() with Set = set [1L;2L] }

0 commit comments

Comments
 (0)