xref: /aosp_15_r20/external/bazel-skylib/lib/dicts.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1*bcb5dc79SHONG Yifan# Copyright 2017 The Bazel Authors. All rights reserved.
2*bcb5dc79SHONG Yifan#
3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License.
5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at
6*bcb5dc79SHONG Yifan#
7*bcb5dc79SHONG Yifan#    http://www.apache.org/licenses/LICENSE-2.0
8*bcb5dc79SHONG Yifan#
9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software
10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and
13*bcb5dc79SHONG Yifan# limitations under the License.
14*bcb5dc79SHONG Yifan
15*bcb5dc79SHONG Yifan"""Skylib module containing functions that operate on dictionaries."""
16*bcb5dc79SHONG Yifan
17*bcb5dc79SHONG Yifandef _add(*dictionaries, **kwargs):
18*bcb5dc79SHONG Yifan    """Returns a new `dict` that has all the entries of the given dictionaries.
19*bcb5dc79SHONG Yifan
20*bcb5dc79SHONG Yifan    If the same key is present in more than one of the input dictionaries, the
21*bcb5dc79SHONG Yifan    last of them in the argument list overrides any earlier ones.
22*bcb5dc79SHONG Yifan
23*bcb5dc79SHONG Yifan    This function is designed to take zero or one arguments as well as multiple
24*bcb5dc79SHONG Yifan    dictionaries, so that it follows arithmetic identities and callers can avoid
25*bcb5dc79SHONG Yifan    special cases for their inputs: the sum of zero dictionaries is the empty
26*bcb5dc79SHONG Yifan    dictionary, and the sum of a single dictionary is a copy of itself.
27*bcb5dc79SHONG Yifan
28*bcb5dc79SHONG Yifan    Args:
29*bcb5dc79SHONG Yifan      *dictionaries: Zero or more dictionaries to be added.
30*bcb5dc79SHONG Yifan      **kwargs: Additional dictionary passed as keyword args.
31*bcb5dc79SHONG Yifan
32*bcb5dc79SHONG Yifan    Returns:
33*bcb5dc79SHONG Yifan      A new `dict` that has all the entries of the given dictionaries.
34*bcb5dc79SHONG Yifan    """
35*bcb5dc79SHONG Yifan    result = {}
36*bcb5dc79SHONG Yifan    for d in dictionaries:
37*bcb5dc79SHONG Yifan        result.update(d)
38*bcb5dc79SHONG Yifan    result.update(kwargs)
39*bcb5dc79SHONG Yifan    return result
40*bcb5dc79SHONG Yifan
41*bcb5dc79SHONG Yifandef _omit(dictionary, keys):
42*bcb5dc79SHONG Yifan    """Returns a new `dict` that has all the entries of `dictionary` with keys not in `keys`.
43*bcb5dc79SHONG Yifan
44*bcb5dc79SHONG Yifan    Args:
45*bcb5dc79SHONG Yifan      dictionary: A `dict`.
46*bcb5dc79SHONG Yifan      keys: A sequence.
47*bcb5dc79SHONG Yifan
48*bcb5dc79SHONG Yifan    Returns:
49*bcb5dc79SHONG Yifan      A new `dict` that has all the entries of `dictionary` with keys not in `keys`.
50*bcb5dc79SHONG Yifan    """
51*bcb5dc79SHONG Yifan    keys_set = {k: None for k in keys}
52*bcb5dc79SHONG Yifan    return {k: dictionary[k] for k in dictionary if k not in keys_set}
53*bcb5dc79SHONG Yifan
54*bcb5dc79SHONG Yifandef _pick(dictionary, keys):
55*bcb5dc79SHONG Yifan    """Returns a new `dict` that has all the entries of `dictionary` with keys in `keys`.
56*bcb5dc79SHONG Yifan
57*bcb5dc79SHONG Yifan    Args:
58*bcb5dc79SHONG Yifan      dictionary: A `dict`.
59*bcb5dc79SHONG Yifan      keys: A sequence.
60*bcb5dc79SHONG Yifan
61*bcb5dc79SHONG Yifan    Returns:
62*bcb5dc79SHONG Yifan      A new `dict` that has all the entries of `dictionary` with keys in `keys`.
63*bcb5dc79SHONG Yifan    """
64*bcb5dc79SHONG Yifan    return {k: dictionary[k] for k in keys if k in dictionary}
65*bcb5dc79SHONG Yifan
66*bcb5dc79SHONG Yifandicts = struct(
67*bcb5dc79SHONG Yifan    add = _add,
68*bcb5dc79SHONG Yifan    omit = _omit,
69*bcb5dc79SHONG Yifan    pick = _pick,
70*bcb5dc79SHONG Yifan)
71