Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1728: add pass_row as configurable parameter for field_update #1729

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions frictionless/steps/field/__spec__/test_field_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,31 @@ def test_step_field_update_referenced_as_foreign_key():
"reference": {"fields": ["pkey"], "resource": "resource1"},
}
]


def test_step_field_update_with_function_and_pass_row_true():
source = TableResource(path="data/transform.csv")

def add_country_to_id(value, row):
return str(value) + " " + row["name"]

pipeline = Pipeline(
steps=[
steps.field_update(
name="id", function=add_country_to_id,
descriptor={"type": "string"}, pass_row=True)
],
)
target = source.transform(pipeline)
assert target.schema.to_descriptor() == {
"fields": [
{"name": "id", "type": "string"},
{"name": "name", "type": "string"},
{"name": "population", "type": "integer"},
]
}
assert target.read_rows() == [
{"id": "1 germany", "name": "germany", "population": 83},
{"id": "2 france", "name": "france", "population": 66},
{"id": "3 spain", "name": "spain", "population": 47},
]
8 changes: 7 additions & 1 deletion frictionless/steps/field/field_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ class field_update(Step):
A descriptor for the field to set the metadata.
"""

pass_row: bool = False
"""
Pass the entire row as a parameter for the transformation function when True.
ebAbhay marked this conversation as resolved.
Show resolved Hide resolved
"""

# Transform

def transform_resource(self, resource: Resource):
function = self.function
pass_row = False
pass_row = self.pass_row
table = resource.to_petl() # type: ignore
descriptor = deepcopy(self.descriptor) or {}
new_name = descriptor.get("name")
Expand Down Expand Up @@ -106,5 +111,6 @@ def transform_resource(self, resource: Resource):
"value": {},
"formula": {"type": "string"},
"descriptor": {"type": "object"},
"pass_row": {"type": "boolean"},
},
}
Loading