Skip to content

Commit

Permalink
add option with string directly
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulineMauryL committed Aug 12, 2024
1 parent c34897a commit 279cf38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
4 changes: 3 additions & 1 deletion smartnoise_synth_logger/deserialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def deserialise_constraints(constraints_json: str) -> dict:

deserialised = {}
for key, val in json_body[JsonBodyKey.CONSTRAINTS].items():
if isinstance(val[JsonBodyKey.PARAM], list):
if isinstance(val, str):
deserialised[key] = val
elif isinstance(val[JsonBodyKey.PARAM], list):
tranformer_list = []
for t in val[JsonBodyKey.PARAM]:
tranformer_list.append(
Expand Down
37 changes: 21 additions & 16 deletions smartnoise_synth_logger/serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,28 @@ def serialise_constraints(constraints: dict) -> str:
}

for col_name, col_constraints in constraints.items():
operator_name = col_constraints.__class__.__name__
if isinstance(col_constraints, str):
json_body[JsonBodyKey.CONSTRAINTS][col_name] = col_constraints
else:
operator_name = col_constraints.__class__.__name__

if operator_name == Transformers.CHAIN:
transformer_dict = handle_chain_transformer(col_constraints)
elif operator_name == Transformers.ANONIMIZATION:
transformer_dict = {
JsonBodyKey.TYPE: SSYNTH_TRANSFORMER
+ Transformers.ANONIMIZATION,
JsonBodyKey.PARAM: {ANON_PARAM: col_constraints.fake.__name__},
}
else: # default
transformer_dict = {
JsonBodyKey.TYPE: SSYNTH_TRANSFORMER
+ col_constraints.__class__.__name__,
JsonBodyKey.PARAM: get_filtered_params(col_constraints),
}
if operator_name == Transformers.CHAIN:
transformer_dict = handle_chain_transformer(col_constraints)
elif operator_name == Transformers.ANONIMIZATION:
transformer_dict = {
JsonBodyKey.TYPE: SSYNTH_TRANSFORMER
+ Transformers.ANONIMIZATION,
JsonBodyKey.PARAM: {
ANON_PARAM: col_constraints.fake.__name__
},
}
else: # default
transformer_dict = {
JsonBodyKey.TYPE: SSYNTH_TRANSFORMER
+ col_constraints.__class__.__name__,
JsonBodyKey.PARAM: get_filtered_params(col_constraints),
}

json_body[JsonBodyKey.CONSTRAINTS][col_name] = transformer_dict
json_body[JsonBodyKey.CONSTRAINTS][col_name] = transformer_dict

return json.dumps(json_body)
11 changes: 11 additions & 0 deletions tests/test_de_serialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def test_anon_serialize():
assert result_json == expected_json_updated


def test_anon_str_serialize():
example_constraints = {"id": "email"}
result_json = serialise_constraints(example_constraints)

expected_json = """{"module": "smartnoise-synth", "version": "1.0.4", "constraints": {"id": "email"}}""" # noqa
expected_json_updated = expected_json.replace(
"1.0.4", pkg_resources.get_distribution(SSYNTH).version
)
assert result_json == expected_json_updated


def test_chain_serialize():
example_constraints = {
"income": ChainTransformer(
Expand Down

0 comments on commit 279cf38

Please sign in to comment.