-
-
Notifications
You must be signed in to change notification settings - Fork 446
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
Open
diegommm
wants to merge
5
commits into
expr-lang:master
Choose a base branch
from
diegommm:allow-ignoring-struct-member-with-struct-tag
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
5 commits
Select commit
Hold shift + click to select a range
401009f
allow ignoring struct member using a - in expr struct tag
diegommm 6bd3eb5
fix in operator
diegommm 56d0528
simplify logic
diegommm eecf371
add regression test for #807
diegommm 1089aa9
fixes #807: make in operator return false for unexported struct fields
diegommm 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
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
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") | ||
} | ||
} |
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
Oops, something went wrong.
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.
This is unrelated but I realized that this example test was not being run because there is a space between
Output
and:
, so I removed the space and the example test is now running and is successful.