blob: 35b9050099d4ac77c63c2f4bd44d054ec002daaf [file] [log] [blame]
# Copyright 2021 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Utility functions for dealing with class members which are sets and lists.
"""
from typing import Any
__all__ = ["difference_field", "intersect_field"]
def intersect_field(item_a: Any, item_b: Any, field: str, result: Any) -> None:
"""Set the named field in `result` if the value for that field is the same
in items A and B.
"""
value_a = getattr(item_a, field)
value_b = getattr(item_b, field)
if value_a == value_b:
setattr(result, field, value_a)
def difference_field(item_a: Any, item_b: Any, field: str, result: Any) -> None:
"""Set the named field in `result` to the value from item_a if the value for that field is different
in items A and B.
"""
value_a = getattr(item_a, field)
value_b = getattr(item_b, field)
if value_a != value_b:
setattr(result, field, value_a)