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

Implement none-behavior strategies #78

Merged
merged 9 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ kwargs

- ``mergelists``: boolean try to merge lists of dict (default: ``True``)

- ``none_behavior``: bit (one of the listed below):

- ``hiyapyco.NONE_BEHAVIOR_DEFAULT``: attempt to merge the value with ``None`` and fail if this is not possible (default method)
- ``hiyapyco.NONE_BEHAVIOR_OVERRIDE``: ``None`` always overrides any other value.

- ``interpolate``: boolean : perform interpolation after the merge
(default: ``False``)

Expand Down
35 changes: 34 additions & 1 deletion hiyapyco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class HiYaPyCoImplementationException(Exception):
METHOD_MERGE = METHODS['METHOD_MERGE']
METHOD_SUBSTITUTE = METHODS['METHOD_SUBSTITUTE']

NONE_BEHAVIORS = {"NONE_BEHAVIOR_DEFAULT": 0x0001, "NONE_BEHAVIOR_OVERRIDE": 0x0002}
NONE_BEHAVIOR_DEFAULT = NONE_BEHAVIORS["NONE_BEHAVIOR_DEFAULT"]
NONE_BEHAVIOR_OVERRIDE = NONE_BEHAVIORS["NONE_BEHAVIOR_OVERRIDE"]


class HiYaPyCo:
"""Main class"""
Expand All @@ -77,6 +81,7 @@ def __init__(self, *args, **kwargs):
hiyapyco.METHOD_SIMPLE | hiyapyco.METHOD_MERGE | hiyapyco.METHOD_SUBSTITUTE
* mergelists: boolean (default: True) try to merge lists
(only makes sense if hiyapyco.METHOD_MERGE or hiyapyco.METHOD_SUBSTITUTE)
* none_behavior: one of hiyapyco.NONE_BEHAVIOR_DEFAULT | hiyapyco.NONE_BEHAVIOR_OVERRIDE
* interpolate: boolean (default: False)
* castinterpolated: boolean (default: False) try to cast values after interpolating
* usedefaultyamlloader: boolean (default: False)
Expand Down Expand Up @@ -122,6 +127,19 @@ def __init__(self, *args, **kwargs):
self.mergelists = kwargs['mergelists']
del kwargs['mergelists']

self.none_behavior = None
if "none_behavior" in kwargs:
logger.debug("parse kwarg method: %s ..." % kwargs["none_behavior"])
if kwargs["none_behavior"] not in NONE_BEHAVIORS.values():
raise HiYaPyCoInvocationException(
"undefined method used, must be one of: %s"
% " ".join(NONE_BEHAVIORS.keys())
)
self.none_behavior = kwargs["none_behavior"]
del kwargs["none_behavior"]
if self.none_behavior is None:
self.none_behavior = NONE_BEHAVIOR_DEFAULT

self.interpolate = False
self.castinterpolated = False
if 'interpolate' in kwargs:
Expand Down Expand Up @@ -333,8 +351,12 @@ def _simplemerge(self, a, b):
a = copy.deepcopy(a)
b = copy.deepcopy(b)
logger.debug('simplemerge %s (%s) and %s (%s)' % (a, type(a), b, type(b),))
# FIXME: make None usage configurable
if b is None:
# override -> None replaces object, no matter what.
if self.none_behavior == NONE_BEHAVIOR_OVERRIDE:
logger.debug('b is None + none_behavior in use => return None')
return None
# default behavior is to attempt merge or fail
logger.debug('pass as b is None')
pass
elif isinstance(b, primitiveTypes):
Expand Down Expand Up @@ -374,6 +396,12 @@ def _substmerge(self, a, b):
logger.debug('substmerge %s and %s' % (a, b,))
# FIXME: make None usage configurable
if b is None:
logger.debug('pass as b is None')
# override -> None replaces object, no matter what.
if self.none_behavior == NONE_BEHAVIOR_OVERRIDE:
logger.debug('b is None + none_behavior in use => return None')
return None
# default behavior is to attempt merge or fail
logger.debug('pass as b is None')
pass

Expand Down Expand Up @@ -429,6 +457,11 @@ def _deepmerge(self, a, b, context = None):
logger.debug('deepmerge %s and %s' % (a, b,))
# FIXME: make None usage configurable
if b is None:
logger.debug('pass as b is None')
# override -> None replaces object, no matter what.
if self.none_behavior == NONE_BEHAVIOR_OVERRIDE:
return None
# default behavior is to attempt merge or fail
logger.debug('pass as b is None')
pass
if a is None or isinstance(b, primitiveTypes):
Expand Down
15 changes: 15 additions & 0 deletions test/base_none_behavior.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 smartindent
singel: null
int: null
array: null
hash: null
deeplist:
- d1: null
- d2:
d2k1: null
d2k2: x2
deepmap:
l1k1:
l2k1: null
l2k2: abc
l1k2: null
61 changes: 60 additions & 1 deletion test/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,65 @@
except KeyError as e:
assert '%s' % e == '\'nosuchelement\''

print('passed test %s' % __file__)

logger.info("test none behavior DEFAULT ...")
for method in [hiyapyco.METHOD_MERGE, hiyapyco.METHOD_SUBSTITUTE]:
try:
conf = hiyapyco.load(
os.path.join(basepath, "base.yaml"),
os.path.join(basepath, "base_none_behavior.yaml"),
method=method,
failonmissingfiles=True,
)
except hiyapyco.HiYaPyCoImplementationException:
pass
else:
raise AssertionError("Default None behavior is expected to fail on this merge")

logger.info("test none behavior OVERRIDE ...")
for method in hiyapyco.METHODS.values():
conf = hiyapyco.load(
os.path.join(basepath, "base.yaml"),
os.path.join(basepath, "base_none_behavior.yaml"),
none_behavior=hiyapyco.NONE_BEHAVIOR_OVERRIDE,
method=hiyapyco.METHOD_MERGE,
failonmissingfiles=True,
)

t = conf["singel"]
logger.info("test single val ... %s" % t)
assert t is None

t = conf["int"]
logger.info("test int val ... %s" % t)
assert t is None

t = conf["array"]
logger.info("test list val ... %s" % t)
assert t is None

t = conf["hash"]
logger.info("test simple dict ... %s" % t)
assert t is None

t = conf["deeplist"]
logger.info("test deeplist ... %s" % t)
assert t == [
{"d1": None},
{"d2": {"d2k2": "x2", "d2k1": None}},
{
"d32": {"a": "A2", "b": "B2"},
"d31": {"a": "A", "c": "C", "b": "B"},
},
]

t = conf["deepmap"]
logger.info("test deepmap ... %s" % t)
assert t == {
"l1k1": {"l2k1": None, "l2k2": "abc"},
"l1k2": None,
}

print("passed test %s" % __file__)

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 smartindent nu
Loading