Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 527
Ignoring fields with struct tag expr:"-" and make "in" return false for unexported fields#806
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
401009f6bd3eb556d0528eecf3711089aa927a4dbfFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,6 +6,7 @@ import ( | ||
| "fmt" | ||
| "os" | ||
| "reflect" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
| @@ -139,7 +140,86 @@ func ExampleEnv_tagged_field_names() { | ||
| fmt.Printf("%v", output) | ||
| // Output : Hello World | ||
| // Output: Hello World | ||
| } | ||
| func ExampleEnv_hidden_tagged_field_names() { | ||
| type Internal struct { | ||
| Visible string | ||
| Hidden string `expr:"-"` | ||
| } | ||
| type environment struct { | ||
| Visible string | ||
| Hidden string `expr:"-"` | ||
| HiddenInternal Internal `expr:"-"` | ||
| VisibleInternal Internal | ||
| } | ||
| env := environment{ | ||
| Hidden: "First level secret", | ||
| HiddenInternal: Internal{ | ||
| Visible: "Second level secret", | ||
| Hidden: "Also hidden", | ||
| }, | ||
| VisibleInternal: Internal{ | ||
| Visible: "Not a secret", | ||
| Hidden: "Hidden too", | ||
| }, | ||
| } | ||
| hiddenValues := []string{ | ||
| `Hidden`, | ||
| `HiddenInternal`, | ||
| `HiddenInternal.Visible`, | ||
| `HiddenInternal.Hidden`, | ||
| `VisibleInternal["Hidden"]`, | ||
| } | ||
| for _, expression := range hiddenValues { | ||
| output, err := expr.Eval(expression, env) | ||
| if err == nil || !strings.Contains(err.Error(), "cannot fetch") { | ||
| fmt.Printf("unexpected output: %v; err: %v\n", output, err) | ||
| return | ||
| } | ||
| fmt.Printf("%q is hidden as expected\n", expression) | ||
| } | ||
| visibleValues := []string{ | ||
| `Visible`, | ||
| `VisibleInternal`, | ||
| `VisibleInternal["Visible"]`, | ||
| } | ||
| for _, expression := range visibleValues { | ||
| _, err := expr.Eval(expression, env) | ||
| if err != nil { | ||
| fmt.Printf("unexpected error: %v\n", err) | ||
| return | ||
| } | ||
| fmt.Printf("%q is visible as expected\n", expression) | ||
| } | ||
| testWithIn := []string{ | ||
| `not ("Hidden" in $env)`, | ||
| `"Visible" in $env`, | ||
| `not ("Hidden" in VisibleInternal)`, | ||
| `"Visible" in VisibleInternal`, | ||
| } | ||
| for _, expression := range testWithIn { | ||
| val, err := expr.Eval(expression, env) | ||
| shouldBeTrue, ok := val.(bool) | ||
| if err != nil || !ok || !shouldBeTrue { | ||
| fmt.Printf("unexpected result; value: %v; error: %v\n", val, err) | ||
| return | ||
| } | ||
| } | ||
| // Output: "Hidden" is hidden as expected | ||
| // "HiddenInternal" is hidden as expected | ||
| // "HiddenInternal.Visible" is hidden as expected | ||
| // "HiddenInternal.Hidden" is hidden as expected | ||
| // "VisibleInternal[\"Hidden\"]" is hidden as expected | ||
| // "Visible" is visible as expected | ||
| // "VisibleInternal" is visible as expected | ||
| // "VisibleInternal[\"Visible\"]" is visible as expected | ||
| } | ||
| func ExampleAsKind() { | ||
| @@ -529,7 +609,7 @@ func ExamplePatch() { | ||
| } | ||
| fmt.Printf("%v", output) | ||
| // Output: Hello, you, world! | ||
| // Output: Hello, you, world! | ||
Comment on lines
-532
to
+612
ContributorAuthor 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. Same as above: this is unrelated but I realized that this example test was not being run because there is a space between | ||
| } | ||
| func ExampleWithContext() { | ||
| @@ -2765,3 +2845,20 @@ func TestMemoryBudget(t *testing.T) { | ||
| }) | ||
| } | ||
| } | ||
| func TestIssue807(t *testing.T) { | ||
| type MyStruct struct { | ||
| nonExported string | ||
| } | ||
| out, err := expr.Eval(` "nonExported" in $env `, MyStruct{}) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| b, ok := out.(bool) | ||
| if !ok { | ||
| t.Fatalf("expected boolean type, got %T: %v", b, b) | ||
| } | ||
| if b { | ||
| t.Fatalf("expected 'in' operator to return false for unexported field") | ||
| } | ||
| } | ||
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.
This is unrelated but I realized that this example test was not being run because there is a space between
Outputand:, so I removed the space and the example test is now running and is successful.