Skip to content

Commit

Permalink
Merge pull request #29 from pier-digital/f/fix-null-conversion
Browse files Browse the repository at this point in the history
Set empty strings to null in metadata fields
  • Loading branch information
gabrielguarisa authored Jun 4, 2024
2 parents 71ffae6 + 62bd7ed commit 4acc0ef
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[tool.poetry]
name = "retrack"
version = "2.5.0"
version = "2.5.1"
description = "A business rules engine"
authors = ["Gabriel Guarisa <[email protected]>", "Nathalia Trotte <[email protected]>"]
authors = ["Gabriel Guarisa <[email protected]>"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/pier-digital/retrack"
Expand Down
11 changes: 11 additions & 0 deletions retrack/nodes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ def cast_int_to_str(v: typing.Any, info: pydantic.ValidationInfo) -> str:
]


def cast_empty_string_to_none(v: str, info: pydantic.ValidationInfo) -> typing.Any:
if v == "":
return None
return v


OptionalCastedToNoneStringType = typing.Annotated[
typing.Optional[str], pydantic.BeforeValidator(cast_empty_string_to_none)
]


class OutputConnectionItemModel(pydantic.BaseModel):
node: CastedToStringType
input_: str = pydantic.Field(alias="input")
Expand Down
3 changes: 2 additions & 1 deletion retrack/nodes/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
InputConnectionModel,
NodeKind,
OutputConnectionModel,
OptionalCastedToNoneStringType,
)

################################################
Expand All @@ -16,7 +17,7 @@

class InputMetadataModel(pydantic.BaseModel):
name: str
default: typing.Optional[str] = None
default: OptionalCastedToNoneStringType = None


################################################
Expand Down
9 changes: 7 additions & 2 deletions retrack/nodes/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import pandas as pd
import pydantic

from retrack.nodes.base import BaseNode, InputConnectionModel, NodeKind
from retrack.nodes.base import (
BaseNode,
InputConnectionModel,
NodeKind,
OptionalCastedToNoneStringType,
)
from retrack.utils import constants

################################################
Expand All @@ -12,7 +17,7 @@


class OutputMetadataModel(pydantic.BaseModel):
message: typing.Optional[str] = None
message: OptionalCastedToNoneStringType = None


################################################
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest


@pytest.fixture
@pytest.fixture(scope="function")
def valid_input_dict_before_validation() -> dict:
return {
"id": 1,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_nodes/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ def test_input_node(
input_node = Input(**valid_input_dict_before_validation)

assert input_node.model_dump(by_alias=True) == valid_input_dict_after_validation


def test_input_with_empty_string_as_default(valid_input_dict_before_validation):
valid_input_dict_before_validation["data"]["default"] = ""
input_node = Input(**valid_input_dict_before_validation)

assert input_node.data.default is None

0 comments on commit 4acc0ef

Please sign in to comment.