fsprojects / FSharp.AWS.DynamoDB

F# wrapper API for AWS DynamoDB
MIT License
58 stars 18 forks source link

Support ADDing numbers #56

Open NickDarvey opened 2 years ago

NickDarvey commented 2 years ago

An ADD in an update expression lets you increment a number attribute which doesn't exist yet, treating it as zero. Trying to use SET to increment a non-existent attribute will throw:

The provided expression refers to an attribute that does not exist in the item

The current update operations allow ADDing to a set attribute only (let ADD (path : Set<'T>) (values : seq<'T>)), this PR proposes adding support to ADD to a number attribute.


It's in draft right now because I'm looking for guidance on how you want this contribution to be implemented, should you want this contribution. The current changes shown in this PR are just the silliest simplest thing to verify the tests would pass.

Specifically, I'm guessing we don't want various ADD_* functions, so I'm thinking of how we could support ADDing numbers without introducing those. Perhaps:

TODO

NickDarvey commented 2 years ago

Specifically, I'm guessing we don't want various ADD_* functions, so I'm thinking of how we could support ADDing numbers without introducing those. Perhaps: Change module UpdateOperators to be a type UpdateOperators with static operators so ADD can be overloaded with the various number types

...which might look something like this...

/// Table Update operation placeholder type
type UpdateOp =
    /// Combines two update operations into one
    static member (&&&) (left : UpdateOp, right : UpdateOp) : UpdateOp =
        invalidOp "Update combiner reserved for quoted update expressions."

    static member ADD  (path : Set<'T>, values : seq<'T>) : UpdateOp =
        invalidOp "ADD operation reserved for quoted update expressions."

    static member ADD  (path : int64, value : int64) : UpdateOp =
        invalidOp "ADD operation reserved for quoted update expressions."

/// Update expression special operators
[<AutoOpen>]
module UpdateOperators =

    /// Assigns a record attribute path to given value
    let SET (path : 'T) (value : 'T) : UpdateOp =
        invalidOp "SET operation reserved for quoted update expressions."

    /// Removes a record attribute path from entry
    let REMOVE (path : 'T) : UpdateOp =
        invalidOp "REMOVE operation reserved for quoted update expressions."

    /// Adds given set of values to set attribute path
    let inline ADD (path : Set<'T>) (values : seq<'T>) : UpdateOp =
        UpdateOp.ADD (path, values)

    /// Deletes given set of values to set attribute path
    let DELETE (path : Set<'T>) (values : seq<'T>) : UpdateOp =
        invalidOp "DELETE operation reserved for quoted update expressions."

However, I'm not sure where to go after updating this block

https://github.com/fsprojects/FSharp.AWS.DynamoDB/blob/b817b6db1186379119aec5d5beeea962cf600a1c/src/FSharp.AWS.DynamoDB/Expression/UpdateExpr.fs#L218-L221

to

| SpecificCall2 <@ UpdateOp.ADD @> (None, _, _, [AttributeGet attr; value]) ->
 //                ~~~~~~~~~~~~~
  let op = getOperand attr.Pickler value
  attrs.Add attr
  updateOps.Add (Add (attr.Id, op))

because I run in to:

A member or object constructor 'ADD' taking 1 arguments is not accessible from this code location. All accessible versions of method 'ADD' take 2 arguments.F# Compiler503

and I'm not sure how to tackle that error.