github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/resource/graph/resource_set.go (about) 1 // Copyright 2016-2021, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package graph 16 17 import ( 18 "github.com/pulumi/pulumi/sdk/v3/go/common/resource" 19 "sort" 20 ) 21 22 // ResourceSet represents a set of Resources. 23 type ResourceSet map[*resource.State]bool 24 25 // Intersect returns a new set that is the intersection of the two given resource sets. 26 func (s ResourceSet) Intersect(other ResourceSet) ResourceSet { 27 newSet := make(ResourceSet) 28 for key := range s { 29 if other[key] { 30 newSet[key] = true 31 } 32 } 33 34 return newSet 35 } 36 37 // Returns the contents of the set as an array of resources. To ensure 38 // determinism, they are sorted by urn. 39 func (s ResourceSet) ToArray() []*resource.State { 40 arr := make([]*resource.State, len(s)) 41 i := 0 42 for r := range s { 43 arr[i] = r 44 i++ 45 } 46 sort.Slice(arr, func(i, j int) bool { 47 return arr[i].URN < arr[j].URN 48 }) 49 return arr 50 } 51 52 // Produces a set from an array. 53 func NewResourceSetFromArray(arr []*resource.State) ResourceSet { 54 s := ResourceSet{} 55 for _, r := range arr { 56 s[r] = true 57 } 58 return s 59 } 60 61 // Produces a shallow copy of `s`. 62 func CopyResourceSet(s ResourceSet) ResourceSet { 63 result := ResourceSet{} 64 for k, v := range s { 65 result[k] = v 66 } 67 return result 68 } 69 70 // Computes s - other. Input sets are unchanged. If `other[k] = false`, then `k` 71 // will not be removed from `s`. 72 func (s ResourceSet) SetMinus(other ResourceSet) ResourceSet { 73 result := CopyResourceSet(s) 74 for k, v := range other { 75 if v { 76 delete(result, k) 77 } 78 } 79 return result 80 } 81 82 // Produces a new set with elements from both sets. The original sets are unchanged. 83 func (s ResourceSet) Union(other ResourceSet) ResourceSet { 84 result := CopyResourceSet(s) 85 return result.UnionWith(other) 86 } 87 88 // Alters `s` to include elements of `other`. 89 func (s ResourceSet) UnionWith(other ResourceSet) ResourceSet { 90 for k, v := range other { 91 s[k] = v || s[k] 92 } 93 return s 94 }