blob: bd0c435cc92f74f97ffa2215249f22b4c5b0a9ba [file] [log] [blame]
#!/usr/bin/env lucicfg
# Copyright 2020 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Library for handling copying objects in LUCI's starlark.
"""
def _is_dict(o):
return type(o) == type({})
def _is_list(o):
return type(o) == type([])
def _simulate_recursion_for_deepcopy_depth_4(x):
"""Helper method for deepcopy()."""
if _is_dict(x) or _is_list(x):
fail("Cannot make a deep copy of containers nested too deeply: %s" % x)
return x
def _simulate_recursion_for_deepcopy_depth_3(x):
"""Helper method for deepcopy()."""
if _is_dict(x):
return {key: _simulate_recursion_for_deepcopy_depth_4(value) for key, value in x.items()}
if _is_list(x):
return [_simulate_recursion_for_deepcopy_depth_4(elem) for elem in x]
return x
def _simulate_recursion_for_deepcopy_depth_2(x):
"""Helper method for deepcopy()."""
if _is_dict(x):
return {key: _simulate_recursion_for_deepcopy_depth_3(value) for key, value in x.items()}
if _is_list(x):
return [_simulate_recursion_for_deepcopy_depth_3(elem) for elem in x]
return x
def _simulate_recursion_for_deepcopy_depth_1(x):
"""Helper method for deepcopy()."""
if _is_dict(x):
return {key: _simulate_recursion_for_deepcopy_depth_2(value) for key, value in x.items()}
if _is_list(x):
return [_simulate_recursion_for_deepcopy_depth_2(elem) for elem in x]
return x
def _deepcopy(x):
"""Returns a copy of the argument, making a deep copy if it is a container.
(Copied from http://google3/borg/test/util.bzl?l=34&rcl=222116979)
Args:
x (object): value to copy. If it is a container with nested
containers as elements, the maximum nesting depth is restricted
to three (e.g., [[[3]]] is okay, but not [[[[4]]]]). If it is a
struct, it is treated as a value type, i.e., only a shallow copy
is made.
Returns:
A copy of the argument.
"""
if _is_dict(x):
return {key: _simulate_recursion_for_deepcopy_depth_1(value) for key, value in x.items()}
if _is_list(x):
return [_simulate_recursion_for_deepcopy_depth_1(elem) for elem in x]
return x
copy = struct(
deepcopy = _deepcopy,
)