1
1
import json
2
- from typing import Union
2
+ from typing import Union , Optional
3
3
4
4
from aws_lambda_powertools .utilities .data_masking .provider import BaseProvider
5
5
6
6
7
7
class DataMasking :
8
- def __init__ (self , provider = None ):
8
+ def __init__ (self , provider : Optional [ BaseProvider ] = None ):
9
9
self .provider = provider or BaseProvider ()
10
10
11
11
def encrypt (self , data , fields = None , ** provider_options ):
@@ -18,37 +18,66 @@ def mask(self, data, fields=None, **provider_options) -> str:
18
18
return self ._apply_action (data , fields , self .provider .mask , ** provider_options )
19
19
20
20
def _apply_action (self , data , fields , action , ** provider_options ):
21
+ """
22
+ Helper method to determine whether to apply a given action to the entire input data
23
+ or to specific fields if the 'fields' argument is specified.
24
+
25
+ Parameters
26
+ ----------
27
+ data : any
28
+ The input data to process.
29
+ fields : Optional[List[any]] = None
30
+ A list of fields to apply the action to. If 'None', the action is applied to the entire 'data'.
31
+ action : Callable
32
+ The action to apply to the data. It should be a callable that performs an operation on the data
33
+ and returns the modified value.
34
+
35
+ Returns
36
+ -------
37
+ any
38
+ The modified data after applying the action.
39
+ """
40
+
21
41
if fields is not None :
22
42
return self ._apply_action_to_fields (data , fields , action , ** provider_options )
23
43
else :
24
44
return action (data , ** provider_options )
25
45
26
46
def _apply_action_to_fields (self , data : Union [dict , str ], fields , action , ** provider_options ) -> Union [dict , str ]:
27
47
"""
28
- Apply the specified action to the specified fields in the input data.
29
-
30
- This method is takes the input data, which can be either a dictionary or a JSON string representation
48
+ This method takes the input data, which can be either a dictionary or a JSON string representation
31
49
of a dictionary, and applies a mask, an encryption, or a decryption to the specified fields.
32
50
33
- Parameters:
34
- data (Union[dict, str]): The input data to process. It can be either a dictionary or a JSON string
51
+ Parameters
52
+ ----------
53
+ data : Union[dict, str])
54
+ The input data to process. It can be either a dictionary or a JSON string
35
55
representation of a dictionary.
36
- fields (list): A list of fields to apply the action to. Each field can be specified as a string or
56
+ fields : List
57
+ A list of fields to apply the action to. Each field can be specified as a string or
37
58
a list of strings representing nested keys in the dictionary.
38
- action (callable): The action to apply to the fields. It should be a callable that takes the current
59
+ action : Callable
60
+ The action to apply to the fields. It should be a callable that takes the current
39
61
value of the field as the first argument and any additional arguments that might be required
40
62
for the action. It performs an operation on the current value using the provided arguments and
41
63
returns the modified value.
42
- **provider_options: Additional keyword arguments to pass to the 'action' function.
64
+ **provider_options:
65
+ Additional keyword arguments to pass to the 'action' function.
43
66
44
- Returns:
45
- str: A JSON string representation of the modified dictionary after applying the action to the
67
+ Returns
68
+ -------
69
+ str
70
+ A JSON string representation of the modified dictionary after applying the action to the
46
71
specified fields.
47
72
48
- Raises:
49
- ValueError: If 'fields' parameter is None.
50
- TypeError: If the 'data' parameter is not a dictionary or a JSON string representation of a dictionary.
51
- KeyError: If specified 'fields' do not exist in input data
73
+ Raises
74
+ -------
75
+ ValueError
76
+ If 'fields' parameter is None.
77
+ TypeError
78
+ If the 'data' parameter is not a dictionary or a JSON string representation of a dictionary.
79
+ KeyError
80
+ If specified 'fields' do not exist in input data
52
81
"""
53
82
54
83
if fields is None :
0 commit comments