Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
 * removed usages of `|>`
 * Removed a bunch of Uncheck.defaultOf<_>
 * Removed most curry and uncurry statements
  • Loading branch information
ShaneGH committed Jan 14, 2025
1 parent b807a7c commit ac10cdd
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 113 deletions.
15 changes: 7 additions & 8 deletions TestDynamo/Client/DtoMappers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module IsSet =
tplDouble
>> mapFst Expr.not
>> mapSnd (flip Expr.assign (Expr.constant true))
>> uncurry Expr.ifThen)
>> fun struct (``if``, ``then``) -> Expr.ifThen ``if`` ``then``)

module VOption =

Expand Down Expand Up @@ -480,14 +480,14 @@ module ToAttributeValue =
struct ("M", typedefof<_ voption>.MakeGenericType([|typedefof<Map<_, _>>.MakeGenericType([|typeof<string>; eFrom.Type|])|]))
struct ("BOOL", typeof<bool voption>)
]
|> Seq.map (mapFst (fun name ->
|> Collection.mapFst (fun name ->
[ eFrom.Type.GetProperty(name, BindingFlags.Instance ||| BindingFlags.Public)
eFrom.Type.GetProperty(name, BindingFlags.Instance ||| BindingFlags.NonPublic) ]
|> Seq.filter ((<>) null)
|> Seq.filter _.CanRead
|> Collection.tryHead
|> Maybe.expectSomeErr "Expected property on type %A" struct (name, eFrom.Type)))
|> Seq.map (uncurry (propAccessor invokePropMap))
|> Maybe.expectSomeErr "Expected property on type %A" struct (name, eFrom.Type))
|> Seq.map (fun struct (prop, typeExpectation) -> propAccessor invokePropMap prop typeExpectation)
|> flip Seq.append [isNullAccessor eFrom.Type]
|> createInstance builderT
|> Expr.constant
Expand Down Expand Up @@ -967,8 +967,8 @@ module ComplexObjectMapper =
let private validatePropNames (t: Type) =
let props =
Array.append
<| t.GetProperties(BindingFlags.Instance ||| BindingFlags.Public)
<| t.GetProperties(BindingFlags.Instance ||| BindingFlags.NonPublic)
(t.GetProperties(BindingFlags.Instance ||| BindingFlags.Public))
(t.GetProperties(BindingFlags.Instance ||| BindingFlags.NonPublic))
|> Seq.map _.Name
|> Collection.groupBy _.ToLower()
|> Collection.mapSnd List.ofSeq
Expand Down Expand Up @@ -1087,8 +1087,7 @@ module ComplexObjectMapper =
|> Either.map1Of2 (fun fromProp -> Expr.prop fromProp.Name eFrom |> tpl fromProp.Name)
|> Either.map2Of2 (tpl "")
|> Either.reduce
|> uncurry tryInvokePropMap
<| toParam.ParameterType
|> fun struct (propName, expr) -> tryInvokePropMap propName expr toParam.ParameterType
?|> (
fromProp
|> Either.ignore2
Expand Down
5 changes: 3 additions & 2 deletions TestDynamo/Client/Fetch.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ let private comparisonOperator =
let private buildKeyCondition' struct (struct (attrAlias, valName), struct (attrName, value)) =
let attrValues =
attributeValueList value
|> Seq.mapi (curry <| function
|> Seq.mapi (function
| struct (0, x) -> struct (valName, x)
| i, x -> struct ($"{valName}_{i}", x))
| i, x -> struct ($"{valName}_{i}", x)
|> curry)
|> List.ofSeq

let exprString =
Expand Down
2 changes: 1 addition & 1 deletion TestDynamo/Client/Item.fs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module Update =
struct (
Map.add x.name x.actualAttributeName names,
x.value
?|> (uncurry (flip3To1 Map.add values))
?|> fun struct (name, value) -> Map.add name value values
?|? values)) struct (Map.empty, Map.empty)
|> tpl x.expression
|> ValueSome
Expand Down
20 changes: 9 additions & 11 deletions TestDynamo/Client/MultiClientOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ module MultiClientOperations =
let private execute operation database acc (struct (batchGetKey: Item.Batch.BatchItemKey, _) & inputs) =

chooseDatabase batchGetKey.databaseId database
|> execute' operation
<| acc
<| inputs
|> fun db -> execute' operation db acc inputs

let executeBatch operation initialState (database: Either<ApiDb, struct (GlobalDatabase * DatabaseId)>) (batchRequests: struct (Item.Batch.BatchItemKey * _) seq) =
batchRequests
Expand Down Expand Up @@ -130,10 +128,10 @@ module MultiClientOperations =
| k, v -> v.keys |> Seq.map (fun keys -> struct (k, { v with keys = [|keys|] })))

ResponseAggregator.executeBatch
<| (execute logger)
<| Settings.BatchItems.BatchGetItemMaxSizeBytes
<| database
<| requests
(execute logger)
Settings.BatchItems.BatchGetItemMaxSizeBytes
database
requests
|> fun x ->
{ notProcessed =
x.notProcessed
Expand Down Expand Up @@ -190,10 +188,10 @@ module MultiClientOperations =
|> MapUtils.toSeq

ResponseAggregator.executeBatch
<| (execute logger)
<| Settings.BatchItems.BatchPutItemMaxSizeBytes
<| database
<| requests
(execute logger)
Settings.BatchItems.BatchPutItemMaxSizeBytes
database
requests
|> _.notProcessed
|> Map.map (fun _ -> List.collect id)
|> fun x -> { notProcessed = x }: BatchWriteResponse
Expand Down
55 changes: 27 additions & 28 deletions TestDynamo/Client/Table.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ module Local =
KeySchema = toKeySchema x |> ValueSome
Projection = buildProjection (Index.projections x) [Index.keyConfig x; Table.keyConfig t] |> ValueSome

IndexSizeBytes = Unchecked.defaultof<_>
OnDemandThroughput = Unchecked.defaultof<_>
ProvisionedThroughput = Unchecked.defaultof<_> }: GlobalSecondaryIndexDescription
IndexSizeBytes = ValueNone
OnDemandThroughput = ValueNone
ProvisionedThroughput = ValueNone }: GlobalSecondaryIndexDescription

let private buildLsi awsAccountId databaseId t (x: Index) =

Expand All @@ -70,12 +70,12 @@ module Local =
KeySchema = toKeySchema x |> ValueSome
Projection = buildProjection (Index.projections x) [Index.keyConfig x; Table.keyConfig t] |> ValueSome

IndexSizeBytes = Unchecked.defaultof<_> }: LocalSecondaryIndexDescription
IndexSizeBytes = ValueNone }: LocalSecondaryIndexDescription

let private buildReplicaGsi (x: Index) =
{ IndexName = Index.getName x
OnDemandThroughputOverride = Unchecked.defaultof<_>
ProvisionedThroughputOverride = Unchecked.defaultof<_> }: ReplicaGlobalSecondaryIndexDescription
OnDemandThroughputOverride = ValueNone
ProvisionedThroughputOverride = ValueNone }: ReplicaGlobalSecondaryIndexDescription

let private buildGsis awsAccountId databaseId t = Seq.filter Index.isGsi >> Seq.map (buildGsi awsAccountId databaseId t)

Expand Down Expand Up @@ -108,14 +108,14 @@ module Local =
{ GlobalSecondaryIndexes = buildReplicaGsis replicaIndexes |> Array.ofSeq |> ValueSome
RegionName = !!<databaseId.regionId

KMSMasterKeyId = Unchecked.defaultof<_>
OnDemandThroughputOverride = Unchecked.defaultof<_>
ProvisionedThroughputOverride = Unchecked.defaultof<_>
ReplicaStatusPercentProgress = Unchecked.defaultof<_>
ReplicaInaccessibleDateTime = Unchecked.defaultof<_>
ReplicaStatus = Unchecked.defaultof<_>
ReplicaStatusDescription = Unchecked.defaultof<_>
ReplicaTableClassSummary = Unchecked.defaultof<_> }: ReplicaDescription
KMSMasterKeyId = ValueNone
OnDemandThroughputOverride = ValueNone
ProvisionedThroughputOverride = ValueNone
ReplicaStatusPercentProgress = ValueNone
ReplicaInaccessibleDateTime = ValueNone
ReplicaStatus = ValueNone
ReplicaStatusDescription = ValueNone
ReplicaTableClassSummary = ValueNone }: ReplicaDescription

let private toAttributeDefinitions attr =
attr
Expand Down Expand Up @@ -149,12 +149,11 @@ module Local =
|> List.sortBy (
fstT
>> fun x -> if x = databaseId then "" else x.regionId)
|> Seq.map (
mapSnd (
Table.indexes
>> _.Values
>> Seq.filter (Index.getName >> hasIndex)))
|> Seq.map (uncurry buildReplica)
|> Collection.mapSnd (
Table.indexes
>> _.Values
>> Seq.filter (Index.getName >> hasIndex))
|> Seq.map (fun struct (dbId, indexes) -> buildReplica dbId indexes)
|> Array.ofSeq

let tableDescription awsAccountId databaseId (cluster: Api.FSharp.GlobalDatabase voption) (table: TableDetails) status =
Expand All @@ -179,14 +178,14 @@ module Local =
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GlobalTables.html
if replicas.Length > 0 then "2019.11.21" |> ValueSome else ValueNone

ArchivalSummary = Unchecked.defaultof<_>
BillingModeSummary = Unchecked.defaultof<_>
OnDemandThroughput = Unchecked.defaultof<_>
ProvisionedThroughput = Unchecked.defaultof<_>
RestoreSummary = Unchecked.defaultof<_>
SSEDescription = Unchecked.defaultof<_>
TableClassSummary = Unchecked.defaultof<_>
TableSizeBytes = Unchecked.defaultof<_> }: TableDescription
ArchivalSummary = ValueNone
BillingModeSummary = ValueNone
OnDemandThroughput = ValueNone
ProvisionedThroughput = ValueNone
RestoreSummary = ValueNone
SSEDescription = ValueNone
TableClassSummary = ValueNone
TableSizeBytes = ValueNone }: TableDescription

let private fromKeySchema (xs: KeySchemaElement list) =
let pk = xs |> Seq.filter (_.KeyType ??|> (_.Value >> (=) KeyType.HASH.Value) ??|? false) |> Collection.tryHead
Expand Down
2 changes: 1 addition & 1 deletion TestDynamo/Model/ChangeResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type DeleteAttemptData =
with
static member asDeleteRequest attemptedDelete =
attemptedDelete.sortKey
?|> (flip3To1 Map.add Map.empty |> uncurry)
?|> fun struct (name, value) -> Map.add name value Map.empty
|> ValueOption.defaultValue Map.empty
|> Map.add (fstT attemptedDelete.partitionKey) (sndT attemptedDelete.partitionKey)
|> tpl attemptedDelete.deleteId
Expand Down
8 changes: 4 additions & 4 deletions TestDynamo/Model/ClientError.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ let private amazonDynamoDBException =
|> Collection.tryHead
|> Maybe.expectSomeErr "Could not find type Amazon.DynamoDBv2.AmazonDynamoDBException%s" ""

let private exn1 =
let private buildException =
let ctr = amazonDynamoDBException.GetConstructor([|typeof<string>|])

Expr.lambda1 typeof<string> (Seq.singleton >> Expr.newObj ctr >> Expr.convert typeof<Exception>)
|> Expr.compile
|> Converters.fromFunc
:?> string -> Exception

let private exn2 =
let private buildException2 =
let ctr = amazonDynamoDBException.GetConstructor([|typeof<string>; typeof<Exception>|])

let inputT = typeof<struct (string * Exception)>
Expand All @@ -44,8 +44,8 @@ let unit' = box ()
let testDynamoException' msg inner data =
let exn =
match inner with
| ValueNone -> exn1 msg
| ValueSome e -> exn2 struct (msg, e)
| ValueNone -> buildException msg
| ValueSome e -> buildException2 struct (msg, e)

exn.Data.Add(clientErrFlag', unit')

Expand Down
5 changes: 3 additions & 2 deletions TestDynamo/Model/Compiler.AstOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ module Value =
let private accessorErr3 = NonEmptyList.singleton "Invalid expression, expected form: x.y or x[123]" |> Error
let accessorExpression: CodeGenSettings -> (AstNode -> CompilerOutput) -> ExpressionPartCompiler =

let rec expandAccessorPath acc = Collection.foldBack (folder |> curry) (Ok acc)
and folder = function
let rec expandAccessorPath acc = Collection.foldBack folder (Ok acc)
and folder s x =
match struct (s, x) with
| struct (Error _ & e, _) -> e
| Ok acc, AstNode.Accessor x -> x::acc |> Ok
| Ok acc, AstNode.Synthetic (AccessorPath x) -> x@acc |> Ok
Expand Down
12 changes: 6 additions & 6 deletions TestDynamo/Model/Compiler.QueryExpressionCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ module QueryExpressionCompiler =
let substringAttr = getSubstring query.expressionParams.expressionAttrValues

Partition.subset
<| ValueSome substringAttr
<| ValueNone
<| true
<| true
<| partition
(ValueSome substringAttr)
ValueNone
true
true
partition
|> Seq.takeWhile (fun x ->
let str = itemValue x
startsWith struct (str, substringAttr))
Expand Down Expand Up @@ -248,7 +248,7 @@ module QueryExpressionCompiler =
>> ValueOption.defaultValue Seq.empty

queryKeys.partitionKey
|> ValueOption.defaultWith (fun _ -> ClientError.clientError "Query does not contain partition key condition")
?|>? fun _ -> ClientError.clientError "Query does not contain partition key condition"
<| inputs
<| index
|> function
Expand Down
2 changes: 1 addition & 1 deletion TestDynamo/Model/Database.fs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ module Database =
>> Maybe.defaultWith (fun name -> ClientError.clientError $"Invalid index {name}") req.indexName
>> Logger.logFn1 "Using index %A" logger)
|> mapSnd Table.attributeNames
|> uncurry (executeFetch req logger))
|> fun struct (index, tableKeys) -> executeFetch req logger index tableKeys)

let private concatStreamErrors err1 err2 =
Io.retn Collection.concat2
Expand Down
2 changes: 1 addition & 1 deletion TestDynamo/Model/ExpressionExecutors.fs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ module Fetch =

Map.empty
|> Map.add (Index.partitionKeyName index) (Index.partitionKey item index)
|> flip (ValueOption.fold (flip <| fun struct (k, v) -> Map.add k v)) sk
|> flip (ValueOption.fold (fun map struct (k, v) -> Map.add k v map)) sk

module private Validation =

Expand Down
3 changes: 2 additions & 1 deletion TestDynamo/Model/Index.fs
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,10 @@ module Index =
[<ExcludeFromCodeCoverage>]
let inline private noResultToValidate struct (_, x) _ = struct ([], x)
let private validateOptionalResultPrecedence loggerIndex result =

mapSnd ValueSome result
|> uncurry Maybe.tpl
?|> (validateResultPrecedence loggerIndex)
?|> validateResultPrecedence loggerIndex
|> ValueOption.defaultWith (noResultToValidate result)

let put =
Expand Down
19 changes: 6 additions & 13 deletions TestDynamo/Model/Item.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ type AttributeSet =
Seq.map (function
| String x -> x
| x -> ClientError.clientError $"Set item {x} is not {AttributeType.String}") xs
static member contains =
function
| struct (value, As struct (_, set)) -> Set.contains value set
|> curry

static member contains value (As struct (_, set)) = Set.contains value set

/// <summary>Returns none if union types do not match</summary>
static member private trySetOperation op = function
Expand Down Expand Up @@ -324,11 +322,6 @@ and
/// </summary>
member this.AttributeType = this |> AttributeValue.getType |> toString

// member this.IsNull =
// match this with
// | Null -> true
// | _ -> false

member this.TryString([<Out>] value: byref<string>) =
match this with
| String x ->
Expand Down Expand Up @@ -688,8 +681,8 @@ module ItemSize =
else
truncated
|> double
// hack, add a tiny number so that (log10 9 = 1, log10 10 = 2)
|> (+) 0.00000001
// hack, add a tiny number (10^-10) so that (log10 9 = 1, log10 10 = 2)
|> (+) 0.0000000001
|> Math.Log10
|> Math.Ceiling
|> int
Expand Down Expand Up @@ -811,8 +804,8 @@ module Item =
match struct (Map.containsKey null item || Map.containsKey "" item, prop) with
| false, _ -> invalidNested
| true, name -> Collection.prepend $"Item has map or property attribute with null or empty name: \"{prop |> List.rev |> Str.join dot}\"" invalidNested
| (depth, prop), HashSet xs -> AttributeSet.asSet xs |> Seq.collect (curry getAttrErrors (depth + 1, "[]"::prop))
| (depth, prop), AttributeList xs -> xs |> AttributeListType.asSeq |> Seq.collect (curry getAttrErrors (depth + 1, "[]"::prop))
| (depth, prop), HashSet xs -> AttributeSet.asSet xs |> Seq.collect (fun attr -> getAttrErrors ((depth + 1, "[]"::prop), attr))
| (depth, prop), AttributeList xs -> AttributeListType.asSeq xs |> Seq.collect (fun attr -> getAttrErrors ((depth + 1, "[]"::prop), attr))
| _, Null -> Seq.empty
| _, String _ -> Seq.empty
| _, Number _ -> Seq.empty
Expand Down
7 changes: 4 additions & 3 deletions TestDynamo/Model/KeyConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ module KeyConfig =

let asAttributeMap pk sk keyConfig =
let pk = partitionKeyName keyConfig |> flip tpl pk
let sk = sortKeyName keyConfig |> flip tpl sk |> uncurry Maybe.tpl
let sk = sortKeyName keyConfig |> flip Maybe.tpl sk

let map =
uncurry Map.add pk Map.empty
|> (
ValueOption.map (uncurry Map.add) sk
|> ValueOption.defaultValue id)
sk
?|> uncurry Map.add
?|? id)

partitionKey map keyConfig |> ignoreTyped<AttributeValue>
sortKey map keyConfig |> ignoreTyped<AttributeValue voption>
Expand Down
Loading

0 comments on commit ac10cdd

Please sign in to comment.