@@ -7,28 +7,32 @@ namespace CommandLine.Helpers;
7
7
8
8
public static class SupabasePatchHelper
9
9
{
10
+ // Applies a "SET" operation to the table, setting the value of a specific property.
10
11
public static IPostgrestTable < T > ApplySet < T > (
11
- IPostgrestTable < T > table ,
12
- string jsonPropertyName ,
13
- object value
14
- ) where T : BaseModel , new ( )
12
+ IPostgrestTable < T > table , // The table to apply the operation to
13
+ string jsonPropertyName , // The name of the JSON property to update
14
+ object value // The new value to set for the property
15
+ ) where T : BaseModel , new ( ) // Ensures T is a subclass of BaseModel with a parameterless constructor
15
16
{
16
- // Find the property that matches the JsonProperty name
17
+ // Find the property on the model that matches the JSON property name
17
18
var property = typeof ( T )
18
- . GetProperties ( )
19
+ . GetProperties ( ) // Get all properties of the model type
19
20
. FirstOrDefault ( p =>
21
+ // Check if the property has a JsonPropertyAttribute
20
22
p . GetCustomAttributes ( typeof ( JsonPropertyAttribute ) , true )
21
23
. FirstOrDefault ( ) is JsonPropertyAttribute attr &&
22
- attr . PropertyName == jsonPropertyName ) ;
24
+ attr . PropertyName == jsonPropertyName ) ; // Check if the JSON property name matches
23
25
24
26
if ( property == null )
25
27
throw new ArgumentException ( $ "'{ jsonPropertyName } ' is not a valid property on type '{ typeof ( T ) . Name } '") ;
26
28
27
- var parameter = Expression . Parameter ( typeof ( T ) , "x" ) ;
28
- var propertyAccess = Expression . Property ( parameter , property . Name ) ;
29
- var converted = Expression . Convert ( propertyAccess , typeof ( object ) ) ;
30
- var lambda = Expression . Lambda < Func < T , object > > ( converted , parameter ) ;
29
+ // Create an expression to access the specified property on the model
30
+ var parameter = Expression . Parameter ( typeof ( T ) , "x" ) ; // Define a parameter for the expression
31
+ var propertyAccess = Expression . Property ( parameter , property . Name ) ; // Access the property
32
+ var converted = Expression . Convert ( propertyAccess , typeof ( object ) ) ; // Convert the value to object type
33
+ var lambda = Expression . Lambda < Func < T , object > > ( converted , parameter ) ; // Create a lambda expression for the property
31
34
35
+ // Apply the "SET" operation to the table using the lambda expression
32
36
return table . Set ( lambda , value ) ;
33
37
}
34
- }
38
+ }
0 commit comments