Skip to content

Commit 6d0ce08

Browse files
Extract sort function and add unit test
1 parent de88d3a commit 6d0ce08

File tree

2 files changed

+100
-29
lines changed

2 files changed

+100
-29
lines changed

internal/fleet/integration_policy_resource.go

+37-29
Original file line numberDiff line numberDiff line change
@@ -341,17 +341,9 @@ func resourceIntegrationPolicyRead(ctx context.Context, d *schema.ResourceData,
341341
}
342342
}
343343

344-
existingInputs, _ := d.Get("input").([]any)
345-
inputIDToIndex := make(map[string]int, len(existingInputs))
346-
for i, v := range existingInputs {
347-
inputData, _ := v.(map[string]any)
348-
inputID, _ := inputData["input_id"].(string)
349-
inputIDToIndex[inputID] = i
350-
}
351-
352344
newInputs := make([]any, 0, len(pkgPolicy.Inputs))
353345
for inputID, input := range pkgPolicy.Inputs {
354-
inputMap := map[string]any{
346+
inputData := map[string]any{
355347
"input_id": inputID,
356348
"enabled": input.Enabled,
357349
}
@@ -361,36 +353,21 @@ func resourceIntegrationPolicyRead(ctx context.Context, d *schema.ResourceData,
361353
if err != nil {
362354
return diag.FromErr(err)
363355
}
364-
inputMap["streams_json"] = string(data)
356+
inputData["streams_json"] = string(data)
365357
}
366358
if input.Vars != nil {
367359
data, err := json.Marshal(*input.Vars)
368360
if err != nil {
369361
return diag.FromErr(err)
370362
}
371-
inputMap["vars_json"] = string(data)
363+
inputData["vars_json"] = string(data)
372364
}
373365

374-
newInputs = append(newInputs, inputMap)
366+
newInputs = append(newInputs, inputData)
375367
}
376368

377-
sort.Slice(newInputs, func(i, j int) bool {
378-
iInput, _ := newInputs[i].(map[string]any)
379-
iID, _ := iInput["input_id"].(string)
380-
iIdx, ok := inputIDToIndex[iID]
381-
if !ok {
382-
return false
383-
}
384-
385-
jInput, _ := newInputs[j].(map[string]any)
386-
jID, _ := jInput["input_id"].(string)
387-
jIdx, ok := inputIDToIndex[jID]
388-
if !ok {
389-
return true
390-
}
391-
392-
return iIdx < jIdx
393-
})
369+
existingInputs, _ := d.Get("input").([]any)
370+
sortInputs(newInputs, existingInputs)
394371

395372
if err := d.Set("input", newInputs); err != nil {
396373
return diag.FromErr(err)
@@ -414,3 +391,34 @@ func resourceIntegrationPolicyDelete(ctx context.Context, d *schema.ResourceData
414391

415392
return diags
416393
}
394+
395+
// sortInputs will sort the 'incoming' list of input definitions based on
396+
// the order of inputs defined in the 'existing' list. Inputs not present in
397+
// 'existing' will be placed at the end of the list. Inputs are identified by
398+
// their ID ('input_id'). The 'incoming' slice will be sorted in-place.
399+
func sortInputs(incoming []any, existing []any) {
400+
idToIndex := make(map[string]int, len(existing))
401+
for i, v := range existing {
402+
inputData, _ := v.(map[string]any)
403+
inputID, _ := inputData["input_id"].(string)
404+
idToIndex[inputID] = i
405+
}
406+
407+
sort.Slice(incoming, func(i, j int) bool {
408+
iInput, _ := incoming[i].(map[string]any)
409+
iID, _ := iInput["input_id"].(string)
410+
iIdx, ok := idToIndex[iID]
411+
if !ok {
412+
return false
413+
}
414+
415+
jInput, _ := incoming[j].(map[string]any)
416+
jID, _ := jInput["input_id"].(string)
417+
jIdx, ok := idToIndex[jID]
418+
if !ok {
419+
return true
420+
}
421+
422+
return iIdx < jIdx
423+
})
424+
}

internal/fleet/shared_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fleet
2+
3+
import (
4+
"testing"
5+
6+
"github.com./stretchr/testify/require"
7+
)
8+
9+
func Test_SortInputs(t *testing.T) {
10+
t.Run("WithExisting", func(t *testing.T) {
11+
existing := []any{
12+
map[string]any{"input_id": "A", "enabled": true},
13+
map[string]any{"input_id": "B", "enabled": true},
14+
map[string]any{"input_id": "C", "enabled": true},
15+
map[string]any{"input_id": "D", "enabled": true},
16+
map[string]any{"input_id": "E", "enabled": true},
17+
}
18+
19+
incoming := []any{
20+
map[string]any{"input_id": "G", "enabled": true},
21+
map[string]any{"input_id": "F", "enabled": true},
22+
map[string]any{"input_id": "B", "enabled": true},
23+
map[string]any{"input_id": "E", "enabled": true},
24+
map[string]any{"input_id": "C", "enabled": true},
25+
}
26+
27+
want := []any{
28+
map[string]any{"input_id": "B", "enabled": true},
29+
map[string]any{"input_id": "C", "enabled": true},
30+
map[string]any{"input_id": "E", "enabled": true},
31+
map[string]any{"input_id": "G", "enabled": true},
32+
map[string]any{"input_id": "F", "enabled": true},
33+
}
34+
35+
sortInputs(incoming, existing)
36+
37+
require.Equal(t, want, incoming)
38+
})
39+
40+
t.Run("WithEmpty", func(t *testing.T) {
41+
var existing []any
42+
43+
incoming := []any{
44+
map[string]any{"input_id": "G", "enabled": true},
45+
map[string]any{"input_id": "F", "enabled": true},
46+
map[string]any{"input_id": "B", "enabled": true},
47+
map[string]any{"input_id": "E", "enabled": true},
48+
map[string]any{"input_id": "C", "enabled": true},
49+
}
50+
51+
want := []any{
52+
map[string]any{"input_id": "G", "enabled": true},
53+
map[string]any{"input_id": "F", "enabled": true},
54+
map[string]any{"input_id": "B", "enabled": true},
55+
map[string]any{"input_id": "E", "enabled": true},
56+
map[string]any{"input_id": "C", "enabled": true},
57+
}
58+
59+
sortInputs(incoming, existing)
60+
61+
require.Equal(t, want, incoming)
62+
})
63+
}

0 commit comments

Comments
 (0)