-
Notifications
You must be signed in to change notification settings - Fork 120
migrate to github.com/pelletier/go-toml/v2 #1018
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
Open
thaJeztah
wants to merge
2
commits into
zmap:master
Choose a base branch
from
thaJeztah:toml_v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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 |
|---|---|---|
|
|
@@ -21,9 +21,70 @@ import ( | |
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/pelletier/go-toml" | ||
| "github.com/pelletier/go-toml/v2" | ||
| ) | ||
|
|
||
| func TestNestedConfigs(t *testing.T) { | ||
| c, err := NewConfigFromString(` | ||
| [Test] | ||
| A = "Test" | ||
| [Test.Sub1] | ||
| A = "Test.Sub1" | ||
| [Test.Sub1.Sub2] | ||
| A = "Test.Sub1.Sub2" | ||
| [Test2.Sub3.Sub4] | ||
| A = "Test2.Sub3.Sub4" | ||
| [Test3.ConfigA.ConfigB] | ||
| A = "Test3.ConfigA.ConfigB.A" | ||
| `) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| type Test struct { | ||
| A string | ||
| } | ||
| type Test2 struct { | ||
| ConfigA struct{ ConfigB Test } | ||
| } | ||
| t.Run("nested configs", func(t *testing.T) { | ||
| for _, ns := range []string{"Test", "Test.Sub1", "Test.Sub1.Sub2", "Test2.Sub3.Sub4"} { | ||
| test := Test{} | ||
|
Comment on lines
+27
to
+51
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed a commit with a tests that passes before and after this PR 😅 - perhaps you could have a peek to see if this test matches the intended behavior 🤗 |
||
| err = c.Configure(&test, ns) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if test.A != ns { | ||
| t.Fatalf("wanted %q got %q", ns, test.A) | ||
| } | ||
| } | ||
| }) | ||
| t.Run("nested config from top", func(t *testing.T) { | ||
| got := Test2{} | ||
| err = c.Configure(&got, "Test3") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| want := Test2{} | ||
| want.ConfigA.ConfigB.A = "Test3.ConfigA.ConfigB.A" | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("wanted %+v got %+v", want, got) | ||
| } | ||
| }) | ||
| t.Run("nested config", func(t *testing.T) { | ||
| got := Test{} | ||
| err = c.Configure(&got, "Test3.ConfigA.ConfigB") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| want := "Test3.ConfigA.ConfigB.A" | ||
| if got.A != want { | ||
| t.Fatalf("wanted %q got %q", want, got) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestInt(t *testing.T) { | ||
| type Test struct { | ||
| A int | ||
|
|
@@ -383,7 +444,7 @@ func TestSmokeExamplePrinting(t *testing.T) { | |
| go func() { | ||
| defer wg.Done() | ||
| defer w.Close() | ||
| err = toml.NewEncoder(w).Indentation("").CompactComments(true).Encode(mapping) | ||
| err = toml.NewEncoder(w).SetIndentSymbol("").SetIndentTables(false).Encode(mapping) | ||
| }() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
|
|
@@ -930,8 +991,7 @@ func TestPrintConfiguration(t *testing.T) { | |
| // I'm not a huge fan of this sort of test since it will have to be updated | ||
| // on the slightest change, but it's better than not have a test for printing | ||
| // out the configuration file. | ||
| want := ` | ||
| [AppleRootStorePolicyConfig] | ||
| want := `[AppleRootStorePolicyConfig] | ||
|
|
||
| [CABFBaselineRequirementsConfig] | ||
| CrossSignedCa = false | ||
|
|
@@ -1015,10 +1075,9 @@ func TestNewGlobalWithPrivateMembersDontGetPrinted(t *testing.T) { | |
| // I'm not a huge fan of this sort of test since it will have to be updated | ||
| // on the slightest change, but it's better than not have a test for printing | ||
| // out the configuration file. | ||
| want := ` | ||
| [this_is_a_test] | ||
| want := `[this_is_a_test] | ||
| A = 1 | ||
| B = "2" | ||
| B = '2' | ||
| ` | ||
| if got != want { | ||
| t.Fatalf("wanted '%s' but got '%s'", want, got) | ||
|
|
@@ -1095,12 +1154,8 @@ func TestStripGlobalsFromStructWithPrivates(t *testing.T) { | |
|
|
||
| func TestNewEmptyConfig(t *testing.T) { | ||
| c := NewEmptyConfig() | ||
| got, err := c.tree.Marshal() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != nil { | ||
| t.Fatalf("wanted nil byte slice, got %s", string(got)) | ||
| if len(c.tree) != 0 { | ||
| t.Fatalf("wanted empty config, got %#v", c.tree) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1172,12 +1227,8 @@ func TestEmptyConfigFromEmptyPath(t *testing.T) { | |
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got, err := c.tree.Marshal() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != nil { | ||
| t.Fatalf("wanted nil byte slice, got %s", string(got)) | ||
| if len(c.tree) != 0 { | ||
| t.Fatalf("wanted empty config, got %#v", c.tree) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I can see why it would think to do nonsense like this due to the word
namespace.So
namespaceis what this TOML library calls an object name.Since it's using a map as intermediary, what it really should be looking for are object keys that match the give namespace.
{ "A": {}, "B": { "C": { "some_config": true } } }Should return...
{ "some_config": true }There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks! I recall now that I was not sure what the expectation was; the examples I could find were all a flat config with no nesting.
What's the expectation if there's nesting? Say;
Would
got := getNamespace(doc, "C")traverse the tree and find the first match (because that looks ambiguous); I gave it a quick try, and it looks like currently it would allow namespace to benamespace.sub1.sub2to resolve a config.Let me push a test for what I had in mind.