Hi, I have a question relating to requesting a sub...
# plugins
c
Hi, I have a question relating to requesting a subset of targets in a plugin. I have created a new field (
class MyRef(SpecialCasedDependencies):
) used by some custom targets types. I have also extended some existing target types using
register_plugin_field
.
In my rule, I want to request all of the targets that have this field. I could request
AllTargets
and then iterate and filter, but that seems like the wrong approach. I suspect that this is what Field Sets are for - but I may be wrong, and I'm not entirely sure how to use them. I can define a new Field Set with the required fields, but then how do I request all the targets (or at least field set instances)? Is there any documentation on this? I've seen some examples of usage, such as extending existing goals like
package
, but that seems orthogonal to what I'm trying to do. Can anyone point me in the right direction?
c
Well, when using field sets, you also start out with
AllTargets
and then ask for all/any field sets for those targets, so to me iterating over all targets doesn’t seem worse than what is already being done in pants.
there’s no short-cut to get all your targets of a specific kind without looking at all targets that I’m aware of.
n.b. that
AllTargets
are dynamic (i.e. follows what specs you provide on the command line, so it is only really all targets in your project if you run pants with the
::
spec)
c
Ok, thanks! Good to know about the
AllTargets
👍 1
h
defining a FieldSet with the required fields is generally recommended over testing for the target type, FWIW
c
In this case I'm testing for the presence of a single field - is there any advantage to using a Field Set over a simple
tgt.has_field()
call?
c
Then you do the equivalent of what a field set does, as you’re not testing
isinstance(tgt, SomeTarget)
which is what Benjy referred to with
testing for the target type
h
Yeah,
tgt.has_field()
seems fine to me if the "field set" is just a single field