-
Notifications
You must be signed in to change notification settings - Fork 24
keep base layer updated with new config #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,124 @@ | ||
package deps | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
type ExampleConfig struct { | ||
Subsystems struct { | ||
EnableWindowPost bool | ||
WindowPostMaxTasks int | ||
} | ||
Fees struct { | ||
DefaultMaxFee string | ||
} | ||
} | ||
|
||
// An original TOML configuration that has both recognized and unknown fields. | ||
const originalTOML = ` | ||
[Subsystems] | ||
EnableWindowPost = true | ||
WindowPostMaxTasks = 5 | ||
|
||
[Fees] | ||
DefaultMaxFee = "0.07 FIL" | ||
|
||
[UnknownSection] | ||
SomeUnknownKey = "whatever" | ||
AnotherField = 123 | ||
|
||
[AnotherUnknownSection.Nested] | ||
NestedValue = "I am nested" | ||
` | ||
|
||
func TestExtractAndMergeUnknownFields(t *testing.T) { | ||
//---------------------------------------------------------------------- | ||
// Step 1: Decode original TOML into recognized struct & collect MetaData | ||
//---------------------------------------------------------------------- | ||
var recognized ExampleConfig | ||
meta, err := toml.Decode(originalTOML, &recognized) | ||
if err != nil { | ||
t.Fatalf("failed to decode recognized fields: %v", err) | ||
} | ||
|
||
keys := removeUnknownEntries(meta.Keys(), meta.Undecoded()) | ||
|
||
//---------------------------------------------------------------------- | ||
// Step 2: Extract the unknown fields using extractUnknownFields | ||
//---------------------------------------------------------------------- | ||
unknownFields := extractUnknownFields(keys, originalTOML) | ||
if len(unknownFields) == 0 { | ||
t.Errorf("expected unknown fields, got none") | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
// Step 3: Update recognized fields in the struct | ||
//---------------------------------------------------------------------- | ||
recognized.Subsystems.EnableWindowPost = false // flip the boolean | ||
recognized.Subsystems.WindowPostMaxTasks = 10 // change from 5 to 10 | ||
recognized.Fees.DefaultMaxFee = "0.08 FIL" // update the fee | ||
|
||
//---------------------------------------------------------------------- | ||
// Step 4: Re-encode recognized fields back to TOML | ||
//---------------------------------------------------------------------- | ||
var buf bytes.Buffer | ||
if err := toml.NewEncoder(&buf).Encode(recognized); err != nil { | ||
t.Fatalf("failed to marshal updated recognized config: %v", err) | ||
} | ||
updatedConfig := buf.String() | ||
|
||
//---------------------------------------------------------------------- | ||
// Step 5: Merge unknown fields back | ||
//---------------------------------------------------------------------- | ||
finalConfig, err := mergeUnknownFields(updatedConfig, unknownFields) | ||
if err != nil { | ||
t.Fatalf("failed to merge unknown fields: %v", err) | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
// Assertions: Check recognized fields have changed & unknown remain | ||
//---------------------------------------------------------------------- | ||
// 5a. Parse final config into a map to check contents | ||
var finalMap map[string]interface{} | ||
if err := toml.Unmarshal([]byte(finalConfig), &finalMap); err != nil { | ||
t.Fatalf("failed to parse final config: %v\nFinal Config:\n%s", err, finalConfig) | ||
} | ||
|
||
// 5b. Check recognized fields updated | ||
subsystems, ok := finalMap["Subsystems"].(map[string]interface{}) | ||
if !ok { | ||
t.Fatalf("expected 'Subsystems' in final config") | ||
} | ||
|
||
if enable, _ := subsystems["EnableWindowPost"].(bool); enable { | ||
t.Errorf("expected Subsystems.EnableWindowPost = false, got true") | ||
} | ||
if tasks, _ := subsystems["WindowPostMaxTasks"].(int64); tasks != 10 { | ||
t.Errorf("expected Subsystems.WindowPostMaxTasks = 10, got %d", tasks) | ||
} | ||
|
||
fees, ok := finalMap["Fees"].(map[string]interface{}) | ||
if !ok { | ||
t.Fatalf("expected 'Fees' in final config") | ||
} | ||
if defaultFee, _ := fees["DefaultMaxFee"].(string); defaultFee != "0.08 FIL" { | ||
t.Errorf("expected Fees.DefaultMaxFee = '0.08 FIL', got '%s'", defaultFee) | ||
} | ||
|
||
// 5c. Check unknown fields remain | ||
if _, exists := finalMap["UnknownSection"]; !exists { | ||
t.Errorf("expected UnknownSection to remain in final config, but not found") | ||
} | ||
if anotherUnknown, exists := finalMap["AnotherUnknownSection"]; !exists { | ||
t.Errorf("expected AnotherUnknownSection to remain in final config, but not found") | ||
} else { | ||
// Inside nested | ||
nested := anotherUnknown.(map[string]interface{})["Nested"].(map[string]interface{}) | ||
if val, ok := nested["NestedValue"].(string); !ok || val != "I am nested" { | ||
t.Errorf("expected AnotherUnknownSection.Nested.NestedValue = 'I am nested', got '%v'", val) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.