Skip to content

Commit 917ad44

Browse files
committed
helper/schema: Fix readSet implementation (DiffFieldReader)
1 parent c738c5a commit 917ad44

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

helper/schema/field_reader_diff.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

helper/schema/set.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func (s *Set) List() []interface{} {
9898
return result
9999
}
100100

101+
func (s *Set) Map() map[string]interface{} {
102+
return s.m
103+
}
104+
101105
// Difference performs a set difference of the two sets, returning
102106
// a new third set that has only the elements unique to this set.
103107
func (s *Set) Difference(other *Set) *Set {

0 commit comments

Comments
 (0)