@@ -238,10 +238,60 @@ func (r *DiffFieldReader) readSet(
238238 // Create the set that will be our result
239239 set := schema .ZeroValue ().(* Set )
240240
241+ // Check if we're supposed to remove it
242+ v , ok := r .Diff .Attributes [prefix + "#" ]
243+ if ok && v .New == "0" {
244+ // I'm not entirely sure what's the point of
245+ // returning empty set w/ Exists: true
246+ return FieldReadResult {
247+ Value : set ,
248+ Exists : true ,
249+ }, nil
250+ }
251+
252+ // Compose list of all keys (diff + source)
253+ var keys []string
254+
255+ // Add keys from diff
256+ diffContainsField := false
257+ for k , _ := range r .Diff .Attributes {
258+ if strings .HasPrefix (k , address [0 ]+ "." ) {
259+ diffContainsField = true
260+ }
261+ keys = append (keys , k )
262+ }
263+ // Bail out if diff doesn't contain the given field at all
264+ if ! diffContainsField {
265+ return FieldReadResult {
266+ Value : set ,
267+ Exists : false ,
268+ }, nil
269+ }
270+ // Add keys from source
271+ sourceResult , err := r .Source .ReadField (address )
272+ if err == nil && sourceResult .Exists {
273+ sourceSet := sourceResult .Value .(* Set )
274+ sourceMap := sourceSet .Map ()
275+
276+ for k , _ := range sourceMap {
277+ key := prefix + k
278+ _ , ok := r .Diff .Attributes [key ]
279+ if ! ok {
280+ keys = append (keys , key )
281+ }
282+ }
283+ }
284+
285+ // Keep the order consistent for hashing functions
286+ sort .Strings (keys )
287+
241288 // Go through the map and find all the set items
242- for k , d := range r .Diff .Attributes {
243- if d .NewRemoved {
244- // If the field is removed, we always ignore it
289+ // We are not iterating over the diff directly as some indexes
290+ // may be missing and we expect the whole set to be returned.
291+ for _ , k := range keys {
292+ d , ok := r .Diff .Attributes [k ]
293+ if ok && d .NewRemoved {
294+ // If the field is being removed, we ignore it
245295 continue
246296 }
247297 if ! strings .HasPrefix (k , prefix ) {
0 commit comments