-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: budgetadjutments events - handle multiple YAML documents in one …
…file (#294) ## Motivation Currently, only the first document in the input file is processed, meaning any additional documents are ignored. This change ensures that all YAML documents within a file are processed correctly. ## Summary This change updates the processing of YAML input files to send a separate request for each YAML document found, rather than only sending the first document. #Changes Modified the YAML parsing logic to extract all documents from the input file. Each document is now converted to JSON and sent as an individual request. No structural validation or merging of documents is performed. #Justification Instead of merging multiple YAML documents into a single request body, each document is now handled separately. Merging them would require additional logic to determine how different documents should be combined, which would significantly complicate the code just to accommodate a niche use case. Additionally, encountering multiple YAML documents in a single input file is an edge case and highly unlikely to happen in normal usage. Given that this part of the code is only responsible for translating YAML to JSON without validating its structure, keeping the implementation simple and predictable is preferable. #Disadvantages - Loss of Transactionality: Sending separate requests for each YAML document means the process is no longer atomic. If one request fails, previous requests will already be processed, and the system will not roll back to a consistent state. - Partial Execution: The process will stop at the first error encountered, leaving subsequent YAML documents unprocessed. This breaks the "all or nothing" approach expected in sloctl workflows. ## Testing Run update and delete events command with a yaml file containing more then one document. For example: ``` --- --- # commented code --- - eventStart: 2024-12-04T06:37:00Z eventEnd: 2024-12-04T06:59:00Z slos: - project: test-project name: sample-slo-1-578b974d-8e27-43cf-85a3-7751a774f13d update: eventStart: 2024-12-04T06:37:00Z eventEnd: 2024-12-04T06:59:00Z --- grocery_list: - category: Fruits items: - name: Apples quantity: 4 - name: Bananas quantity: 6 - name: Oranges quantity: 3 - category: Vegetables items: - name: Carrots quantity: 2 - name: Broccoli quantity: 1 - name: Spinach quantity: 1 bunch --- --- ``` ## Release Notes The `budgetadjustments events` commands now processes all YAML documents in an input file instead of only the first one, ensuring no data is missed. Each document is sent as a separate request to the API
- Loading branch information
1 parent
1189b2e
commit efe5e93
Showing
5 changed files
with
220 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.10.1 | ||
0.11.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package events | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSplitYAMLDocuments(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected []string | ||
}{ | ||
{ | ||
name: "Basic YAML Split", | ||
input: `--- | ||
name: doc1 | ||
value: 1 | ||
--- | ||
name: doc2 | ||
value: 2 | ||
--- | ||
name: doc3 | ||
value: 3 | ||
`, | ||
expected: []string{ | ||
"name: doc1\nvalue: 1", | ||
"name: doc2\nvalue: 2", | ||
"name: doc3\nvalue: 3", | ||
}, | ||
}, | ||
{ | ||
name: "Basic YAML Split with additional separators", | ||
input: `--- | ||
--- | ||
--- | ||
name: doc1 | ||
value: 1 | ||
--- | ||
--- | ||
name: doc2 | ||
value: 2 | ||
--- | ||
name: doc3 | ||
value: 3 | ||
--- | ||
---`, | ||
expected: []string{ | ||
"name: doc1\nvalue: 1", | ||
"name: doc2\nvalue: 2", | ||
"name: doc3\nvalue: 3", | ||
}, | ||
}, | ||
{ | ||
name: "YAML with Lists", | ||
input: `--- | ||
list: | ||
- item1 | ||
- item2 | ||
--- | ||
list: | ||
- item3 | ||
- item4 | ||
`, | ||
expected: []string{ | ||
"list:\n - item1\n - item2", | ||
"list:\n - item3\n - item4", | ||
}, | ||
}, | ||
{ | ||
name: "YAML with Nested Structures", | ||
input: `--- | ||
parent: | ||
child: value1 | ||
--- | ||
parent: | ||
child: value2 | ||
`, | ||
expected: []string{ | ||
"parent:\n child: value1", | ||
"parent:\n child: value2", | ||
}, | ||
}, | ||
{ | ||
name: "invalid YAML", | ||
input: `--- | ||
foo bar | ||
baz: bob | ||
`, | ||
expected: []string{"foo bar\nbaz: bob"}, | ||
}, | ||
{ | ||
name: "just YAML", | ||
input: "YAML", | ||
expected: []string{"YAML"}, | ||
}, | ||
{ | ||
name: "YAML with doc separators found in content", | ||
input: `--- | ||
parent: | ||
child: "foo---bar" | ||
--- | ||
parent: | ||
child: value2 | ||
--- | ||
----nt: | ||
child: value3 | ||
---- | ||
--- | ||
--- | ||
--- | ||
`, | ||
expected: []string{ | ||
"parent:\n child: \"foo---bar\"", | ||
"parent:\n child: value2", | ||
"----nt:\n child: value3\n----", | ||
"---\n ---", | ||
}, | ||
}, | ||
{ | ||
name: "YAML with correct event format", | ||
input: ` | ||
- eventStart: 2024-12-04T06:37:00Z | ||
eventEnd: 2024-12-04T06:59:00Z | ||
slos: | ||
- project: test-project | ||
name: sample-slo-1-578b974d-8e27-43cf-85a3-7751a774f13d | ||
update: | ||
eventStart: 2024-12-04T06:37:00Z | ||
eventEnd: 2024-12-04T06:59:00Z | ||
`, | ||
expected: []string{ | ||
`- eventStart: 2024-12-04T06:37:00Z | ||
eventEnd: 2024-12-04T06:59:00Z | ||
slos: | ||
- project: test-project | ||
name: sample-slo-1-578b974d-8e27-43cf-85a3-7751a774f13d | ||
update: | ||
eventStart: 2024-12-04T06:37:00Z | ||
eventEnd: 2024-12-04T06:59:00Z`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := splitYAMLDocs([]byte(tt.input)) | ||
|
||
// Trim whitespace from results for better comparison | ||
for i := range result { | ||
result[i] = strings.TrimSpace(result[i]) | ||
} | ||
|
||
if !reflect.DeepEqual(result, tt.expected) { | ||
t.Errorf("Test %s failed. Expected %v, but got %v", tt.name, tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters