terraform/internal/experiments/set.go

47 lines
1.0 KiB
Go
Raw Permalink Normal View History

experiments: a mechanism for opt-in experimental language features Traditionally we've preferred to release new language features in major releases only, because we can then use the beta cycle to gather feedback on the feature and learn about any usability challenges or other situations we didn't consider during our design in time to make those changes before inclusion in a stable release. This "experiments" feature is intended to decouple the feedback cycle for new features from the major release rhythm, and thus allow us to release new features in minor releases by first releasing them as experimental for a minor release or two, adjust for any feedback gathered during that period, and then finally remove the experiment gate and enable the feature for everyone. The intended model here is that anything behind an experiment gate is subject to breaking changes even in patch releases, and so any module using these experimental features will be broken by a future Terraform upgrade. The behavior implemented here is: - Recognize a new "experiments" setting in the "terraform" block which allows module authors to explicitly opt in to experimental features. terraform { experiments = [resource_for_each] } - Generate a warning whenever loading a module that has experiments enabled, to avoid accidentally depending on experimental features and thus risking unexpected breakage on next Terraform upgrade. - We check the enabled experiments against the configuration at module load time, which means that experiments are scoped to a particular module. Enabling an experiment in one module does not automatically enable it in any other module. This experiments mechanism is itself an experiment, and so I'd like to use the resource for_each feature to trial it. Because any configuration using experiments is subject to breaking changes, we are free to adjust this experiments feature in future releases as we see fit, but once for_each is shipped without an experiment gate we'll be blocked from making significant changes to it until the next major release at least.
2019-07-10 21:37:11 +02:00
package experiments
// Set is a collection of experiments where every experiment is either a member
// or not.
type Set map[Experiment]struct{}
// NewSet constructs a new Set with the given experiments as its initial members.
func NewSet(exps ...Experiment) Set {
ret := make(Set)
for _, exp := range exps {
ret.Add(exp)
}
return ret
}
// SetUnion constructs a new Set containing the members of all of the given
// sets.
func SetUnion(sets ...Set) Set {
ret := make(Set)
for _, set := range sets {
for exp := range set {
ret.Add(exp)
}
}
return ret
}
// Add inserts the given experiment into the set.
//
// If the given experiment is already present then this is a no-op.
func (s Set) Add(exp Experiment) {
s[exp] = struct{}{}
}
// Remove takes the given experiment out of the set.
//
// If the given experiment not already present then this is a no-op.
func (s Set) Remove(exp Experiment) {
delete(s, exp)
}
// Has tests whether the given experiment is in the receiving set.
func (s Set) Has(exp Experiment) bool {
_, ok := s[exp]
return ok
}