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

Convert depset into a list before iterating #67

Closed
Closed
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
2 changes: 1 addition & 1 deletion bzl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _bzl_library_impl(ctx):
# a separate program, or from `tools` of a genrule().
DefaultInfo(
files = all_files,
runfiles = ctx.runfiles(files = list(all_files)),
runfiles = ctx.runfiles(transitive_files = all_files),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved this change to #69

),

# We also define our own provider struct, for aggregation and testing.
Expand Down
1 change: 1 addition & 0 deletions lib/new_sets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def _make(elements = None):
A set containing the passed in values.
"""
elements = elements if elements else []
elements = elements.to_list() if type(elements) == "depset" else elements
bttk marked this conversation as resolved.
Show resolved Hide resolved
return struct(_values = {e: None for e in elements})

def _copy(s):
Expand Down
34 changes: 33 additions & 1 deletion lib/sets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ def _precondition_only_sets_or_lists(*args):
fail("Expected arguments to be depset or list, but found type %s: %r" %
(t, a))

def _to_set(a):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @c-parsons
I'd prefer to delete this file, rather than maintain it.

Copy link
Contributor Author

@bttk bttk Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to delete this file, rather than maintain it.

Is there a migration plan for current users? I don't see a deprecation warning in the file.

For example:

  1. add print("DEPRECATED: ... at the top of sets.bzl
  2. Require an action from existing users:
    • move sets.bzl to a deprecated subdirectory
    • rename sets = struct( to sets_deprecated = struct(

I have tried to use an alias() to add a deprecation warning, but it doesn't seem to be possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first step will be done with #70

"""Converts `a` into a set.

Args:
a: A depset or a list.

Returns:
A dict of keys created from elements of `a`.
"""
t = type(a)
if t == "depset":
a = a.to_list()
return {e: None for e in a}

def _to_list(a):
"""Converts `a` into a list.

Args:
a: A depset, list, or an output of `_to_set()`

Returns:
A list of unique elements of `a`.
"""
t = type(a)
if t in ("depset", "list"):
a = _to_set(a)
return a.keys()

def _is_equal(a, b):
"""Returns whether two sets are equal.

Expand All @@ -54,7 +82,7 @@ def _is_equal(a, b):
True if `a` is equal to `b`, False otherwise.
"""
_precondition_only_sets_or_lists(a, b)
return sorted(depset(a)) == sorted(depset(b))
return sorted(_to_list(a)) == sorted(_to_list(b))

def _is_subset(a, b):
"""Returns whether `a` is a subset of `b`.
Expand All @@ -67,6 +95,7 @@ def _is_subset(a, b):
True if `a` is a subset of `b`, False otherwise.
"""
_precondition_only_sets_or_lists(a, b)
a, b = _to_set(a), _to_set(b)
for e in a:
if e not in b:
return False
Expand All @@ -85,6 +114,7 @@ def _disjoint(a, b):
True if `a` and `b` are disjoint, False otherwise.
"""
_precondition_only_sets_or_lists(a, b)
a, b = _to_set(a), _to_set(b)
for e in a:
if e in b:
return False
Expand All @@ -101,6 +131,7 @@ def _intersection(a, b):
A set containing the elements that are in both `a` and `b`.
"""
_precondition_only_sets_or_lists(a, b)
a, b = _to_set(a), _to_set(b)
return depset([e for e in a if e in b])

def _union(*args):
Expand All @@ -127,6 +158,7 @@ def _difference(a, b):
A set containing the elements that are in `a` but not in `b`.
"""
_precondition_only_sets_or_lists(a, b)
a, b = _to_set(a), _to_set(b)
return depset([e for e in a if e not in b])

sets = struct(
Expand Down