Skip to content

Commit d82697a

Browse files
tweak ConstantHashKey methods in RecordTemplate
1 parent 10266af commit d82697a

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/FSharp.DynamoDB/KeySchema.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ with
5151

5252
dict
5353

54+
/// Extract a KeyCondition for records that specify a default hashkey
55+
static member TryExtractHashKeyCondition keyStructure keySchema =
56+
match keyStructure with
57+
| DefaultHashKey(attrName, value, pickler, _) ->
58+
let av = pickler.PickleUntyped value |> Option.get
59+
let cond = ConditionalExpr.mkHashKeyEqualityCondition keySchema av
60+
let cexpr = new ConditionExpression<'TRecord>(cond)
61+
Some cexpr
62+
63+
| _ -> None
64+
5465
/// Extracts key from given record instance
5566
static member ExtractKey(keyStructure : KeyStructure, recordInfo : RecordInfo, record : 'Record) =
5667
let inline getValue (rp : RecordPropertyInfo) = rp.PropertyInfo.GetValue(record)

src/FSharp.DynamoDB/RecordTemplate.fs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ type RecordTemplate<'TRecord> internal () =
1919
let pickler = Pickler.resolve<'TRecord>() :?> RecordPickler<'TRecord>
2020
let keyStructure = KeyStructure.FromRecordInfo pickler.RecordInfo
2121
let keySchema = TableKeySchema.OfKeyStructure keyStructure
22+
let hkeyCond = KeyStructure.TryExtractHashKeyCondition<'TRecord> keyStructure keySchema
2223

2324
/// Key schema used by the current record
2425
member __.KeySchema = keySchema
2526

27+
/// Gets the constant HashKey if specified by the record implementation
28+
member __.ConstantHashKey : obj option =
29+
match keyStructure with
30+
| DefaultHashKey(_, value, _, _) -> Some value
31+
| _ -> None
32+
33+
/// Gets the constant RangeKey if specified by the record implementation
34+
member __.ConstantRangeKey : obj option =
35+
match keyStructure with
36+
| DefaultRangeKey(_, value, _, _) -> Some value
37+
| _ -> None
38+
39+
/// Gets a condition expression that matches the constant HashKey,
40+
/// if so specified.
41+
member __.ConstantHashKeyCondition = hkeyCond
42+
2643
/// Record property info
2744
member internal __.Info = pickler.RecordInfo
2845

@@ -211,17 +228,6 @@ type RecordTemplate<'TRecord> internal () =
211228
/// Constructs a record instance from attribute values
212229
member internal __.OfAttributeValues(ro : RestObject) = pickler.ToRecord ro
213230

214-
/// <summary>
215-
/// Generates a HashKey equality condition used for queries
216-
/// that match given table key. Useful for generating query
217-
/// conditions in records that specify a ConstantHashKey attribute.
218-
/// </summary>
219-
/// <param name="key">Key that specifies the required HashKey.</param>
220-
member __.GetHashKeyCondition(key : TableKey) =
221-
let av = __.ToAttributeValues(key).[keySchema.HashKey.AttributeName]
222-
let cond = mkHashKeyEqualityCondition keySchema av
223-
new ConditionExpression<'TRecord>(cond)
224-
225231
/// Record template factory methods
226232
[<Sealed; AbstractClass>]
227233
type RecordTemplate private () =

tests/FSharp.DynamoDB.Tests/ConditionalExpressionTests.fs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,6 @@ type ``Conditional Expression Tests`` () =
422422

423423
results.Length |> should equal 50
424424

425-
[<Fact>]
426-
let ``Extract Query expression from key`` () =
427-
let item = mkItem()
428-
let key = table.PutItem item
429-
let kc = table.Template.GetHashKeyCondition key
430-
let results = table.Query(kc)
431-
results.Length |> should equal 1
432-
results.[0] |> should equal item
433-
434425
[<Fact>]
435426
let ``Detect incompatible key conditions`` () =
436427
let test outcome q = table.Template.PrecomputeConditionalExpr(q).IsKeyConditionCompatible |> should equal outcome

0 commit comments

Comments
 (0)