Skip to content

Commit 95098b3

Browse files
#1728: add pass_row as configurable parameter for field_update (#1729)
# Description As described in #1728, pass_row parameter is now added as an editable parameter for the field_update step. This allows us to use row data inside custom python function. The default value of the parameter remains False so that any existing uses of this functionality will not be affected. # Tests Ensured the newly added unit test `test_step_field_update_with_function_and_pass_row_true` runs without fail and also that all the previous test cases run and pass too. - fixes #1728 --------- Co-authored-by: Pierre Camilleri <[email protected]>
1 parent 12db3c2 commit 95098b3

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

frictionless/steps/field/__spec__/test_field_update.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,31 @@ def test_step_field_update_referenced_as_foreign_key():
144144
"reference": {"fields": ["pkey"], "resource": "resource1"},
145145
}
146146
]
147+
148+
149+
def test_step_field_update_with_function_and_pass_row_true():
150+
source = TableResource(path="data/transform.csv")
151+
152+
def add_country_to_id(value, row):
153+
return str(value) + " " + row["name"]
154+
155+
pipeline = Pipeline(
156+
steps=[
157+
steps.field_update(
158+
name="id", function=add_country_to_id,
159+
descriptor={"type": "string"}, pass_row=True)
160+
],
161+
)
162+
target = source.transform(pipeline)
163+
assert target.schema.to_descriptor() == {
164+
"fields": [
165+
{"name": "id", "type": "string"},
166+
{"name": "name", "type": "string"},
167+
{"name": "population", "type": "integer"},
168+
]
169+
}
170+
assert target.read_rows() == [
171+
{"id": "1 germany", "name": "germany", "population": 83},
172+
{"id": "2 france", "name": "france", "population": 66},
173+
{"id": "3 spain", "name": "spain", "population": 47},
174+
]

frictionless/steps/field/field_update.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ class field_update(Step):
5050
A descriptor for the field to set the metadata.
5151
"""
5252

53+
pass_row: bool = False
54+
"""
55+
Pass the entire row as a second parameter for the transformation function if set to True.
56+
The first parameter (the value) remains unchanged.
57+
"""
58+
5359
# Transform
5460

5561
def transform_resource(self, resource: Resource):
5662
function = self.function
57-
pass_row = False
63+
pass_row = self.pass_row
5864
table = resource.to_petl() # type: ignore
5965
descriptor = deepcopy(self.descriptor) or {}
6066
new_name = descriptor.get("name")
@@ -106,5 +112,6 @@ def transform_resource(self, resource: Resource):
106112
"value": {},
107113
"formula": {"type": "string"},
108114
"descriptor": {"type": "object"},
115+
"pass_row": {"type": "boolean"},
109116
},
110117
}

0 commit comments

Comments
 (0)