|
6 | 6 | "fmt" |
7 | 7 | "os" |
8 | 8 | "reflect" |
| 9 | + "strings" |
9 | 10 | "sync" |
10 | 11 | "testing" |
11 | 12 | "time" |
@@ -139,7 +140,86 @@ func ExampleEnv_tagged_field_names() { |
139 | 140 |
|
140 | 141 | fmt.Printf("%v", output) |
141 | 142 |
|
142 | | - // Output : Hello World |
| 143 | + // Output: Hello World |
| 144 | +} |
| 145 | + |
| 146 | +func ExampleEnv_hidden_tagged_field_names() { |
| 147 | + type Internal struct { |
| 148 | + Visible string |
| 149 | + Hidden string `expr:"-"` |
| 150 | + } |
| 151 | + type environment struct { |
| 152 | + Visible string |
| 153 | + Hidden string `expr:"-"` |
| 154 | + HiddenInternal Internal `expr:"-"` |
| 155 | + VisibleInternal Internal |
| 156 | + } |
| 157 | + |
| 158 | + env := environment{ |
| 159 | + Hidden: "First level secret", |
| 160 | + HiddenInternal: Internal{ |
| 161 | + Visible: "Second level secret", |
| 162 | + Hidden: "Also hidden", |
| 163 | + }, |
| 164 | + VisibleInternal: Internal{ |
| 165 | + Visible: "Not a secret", |
| 166 | + Hidden: "Hidden too", |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + hiddenValues := []string{ |
| 171 | + `Hidden`, |
| 172 | + `HiddenInternal`, |
| 173 | + `HiddenInternal.Visible`, |
| 174 | + `HiddenInternal.Hidden`, |
| 175 | + `VisibleInternal["Hidden"]`, |
| 176 | + } |
| 177 | + for _, expression := range hiddenValues { |
| 178 | + output, err := expr.Eval(expression, env) |
| 179 | + if err == nil || !strings.Contains(err.Error(), "cannot fetch") { |
| 180 | + fmt.Printf("unexpected output: %v; err: %v\n", output, err) |
| 181 | + return |
| 182 | + } |
| 183 | + fmt.Printf("%q is hidden as expected\n", expression) |
| 184 | + } |
| 185 | + |
| 186 | + visibleValues := []string{ |
| 187 | + `Visible`, |
| 188 | + `VisibleInternal`, |
| 189 | + `VisibleInternal["Visible"]`, |
| 190 | + } |
| 191 | + for _, expression := range visibleValues { |
| 192 | + _, err := expr.Eval(expression, env) |
| 193 | + if err != nil { |
| 194 | + fmt.Printf("unexpected error: %v\n", err) |
| 195 | + return |
| 196 | + } |
| 197 | + fmt.Printf("%q is visible as expected\n", expression) |
| 198 | + } |
| 199 | + |
| 200 | + testWithIn := []string{ |
| 201 | + `not ("Hidden" in $env)`, |
| 202 | + `"Visible" in $env`, |
| 203 | + `not ("Hidden" in VisibleInternal)`, |
| 204 | + `"Visible" in VisibleInternal`, |
| 205 | + } |
| 206 | + for _, expression := range testWithIn { |
| 207 | + val, err := expr.Eval(expression, env) |
| 208 | + shouldBeTrue, ok := val.(bool) |
| 209 | + if err != nil || !ok || !shouldBeTrue { |
| 210 | + fmt.Printf("unexpected result; value: %v; error: %v\n", val, err) |
| 211 | + return |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // Output: "Hidden" is hidden as expected |
| 216 | + // "HiddenInternal" is hidden as expected |
| 217 | + // "HiddenInternal.Visible" is hidden as expected |
| 218 | + // "HiddenInternal.Hidden" is hidden as expected |
| 219 | + // "VisibleInternal[\"Hidden\"]" is hidden as expected |
| 220 | + // "Visible" is visible as expected |
| 221 | + // "VisibleInternal" is visible as expected |
| 222 | + // "VisibleInternal[\"Visible\"]" is visible as expected |
143 | 223 | } |
144 | 224 |
|
145 | 225 | func ExampleAsKind() { |
@@ -529,7 +609,7 @@ func ExamplePatch() { |
529 | 609 | } |
530 | 610 | fmt.Printf("%v", output) |
531 | 611 |
|
532 | | - // Output : Hello, you, world! |
| 612 | + // Output: Hello, you, world! |
533 | 613 | } |
534 | 614 |
|
535 | 615 | func ExampleWithContext() { |
@@ -2765,3 +2845,20 @@ func TestMemoryBudget(t *testing.T) { |
2765 | 2845 | }) |
2766 | 2846 | } |
2767 | 2847 | } |
| 2848 | + |
| 2849 | +func TestIssue807(t *testing.T) { |
| 2850 | + type MyStruct struct { |
| 2851 | + nonExported string |
| 2852 | + } |
| 2853 | + out, err := expr.Eval(` "nonExported" in $env `, MyStruct{}) |
| 2854 | + if err != nil { |
| 2855 | + t.Fatalf("unexpected error: %v", err) |
| 2856 | + } |
| 2857 | + b, ok := out.(bool) |
| 2858 | + if !ok { |
| 2859 | + t.Fatalf("expected boolean type, got %T: %v", b, b) |
| 2860 | + } |
| 2861 | + if b { |
| 2862 | + t.Fatalf("expected 'in' operator to return false for unexported field") |
| 2863 | + } |
| 2864 | +} |
0 commit comments