Skip to content

Commit a3c0d43

Browse files
Copilottimflannagan
andcommitted
Add test cases for nested struct fields
Co-authored-by: timflannagan <9899409+timflannagan@users.noreply.github.com>
1 parent 7a38de6 commit a3c0d43

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

pkg/analyzer/analyzer_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ func TestUnexportedCheckDisabled(t *testing.T) {
5353
a := NewAnalyzer(&Config{CheckUnexported: false})
5454
analysistest.Run(t, testdata, a, "unexportedoff")
5555
}
56+
57+
func TestNestedFields(t *testing.T) {
58+
testdata := analysistest.TestData()
59+
a := NewAnalyzer(&Config{})
60+
analysistest.Run(t, testdata, a, "nested")
61+
}
62+
63+
func TestNestedFieldsWithUnexported(t *testing.T) {
64+
testdata := analysistest.TestData()
65+
a := NewAnalyzer(&Config{CheckUnexported: true})
66+
analysistest.Run(t, testdata, a, "nestedunexported")
67+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package nested
2+
3+
import (
4+
"time"
5+
)
6+
7+
// Inner is a nested struct that should have its own Equals method
8+
type Inner struct {
9+
Value1 int
10+
Value2 string
11+
}
12+
13+
// NestedStruct has fields of a custom struct type.
14+
// The linter should check that these nested fields are compared properly.
15+
type NestedStruct struct {
16+
Name string
17+
Config Inner // want "field \"Config\" in type \"NestedStruct\" is not used in Equals"
18+
// +noKrtEquals reason: internal bookkeeping
19+
Cache map[string]string
20+
}
21+
22+
// Missing comparison - should fail because Config is not compared
23+
func (n NestedStruct) Equals(other NestedStruct) bool {
24+
return n.Name == other.Name
25+
}
26+
27+
// ListenerPolicyIR mimics the structure from the issue
28+
type ListenerPolicyIR struct {
29+
ct time.Time
30+
defaultPolicy listenerPolicy
31+
perPortPolicy map[uint32]listenerPolicy
32+
// +noKrtEquals reason: When set to true, suppress source reporting metadata from
33+
// ListenerPolicy specific fields that are irrelevant to the (now deprecated) HTTPListenerPolicy. Remove when HTTPListenerPolicy is removed.
34+
NoOrigin bool
35+
}
36+
37+
type listenerPolicy struct {
38+
proxyProtocol *int
39+
perConnectionBufferLimitBytes *uint32
40+
http *HttpListenerPolicyIr
41+
}
42+
43+
type HttpListenerPolicyIr struct {
44+
Value string
45+
}
46+
47+
// Equals for ListenerPolicyIR that doesn't compare unexported fields
48+
// The linter should NOT flag this with default config (CheckUnexported: false)
49+
func (l ListenerPolicyIR) Equals(other ListenerPolicyIR) bool {
50+
// NoOrigin is marked with +noKrtEquals so it shouldn't be required
51+
return true
52+
}
53+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package nestedunexported
2+
3+
import (
4+
"time"
5+
)
6+
7+
// ListenerPolicyIR mimics the structure from the issue
8+
// When CheckUnexported: true, unexported fields should be flagged
9+
type ListenerPolicyIR struct {
10+
ct time.Time // want "field \"ct\" in type \"ListenerPolicyIR\" is not used in Equals"
11+
defaultPolicy listenerPolicy // want "field \"defaultPolicy\" in type \"ListenerPolicyIR\" is not used in Equals"
12+
perPortPolicy map[uint32]listenerPolicy // want "field \"perPortPolicy\" in type \"ListenerPolicyIR\" is not used in Equals"
13+
// +noKrtEquals reason: When set to true, suppress source reporting metadata from
14+
// ListenerPolicy specific fields that are irrelevant to the (now deprecated) HTTPListenerPolicy. Remove when HTTPListenerPolicy is removed.
15+
NoOrigin bool
16+
}
17+
18+
type listenerPolicy struct {
19+
proxyProtocol *int
20+
perConnectionBufferLimitBytes *uint32
21+
http *HttpListenerPolicyIr
22+
}
23+
24+
type HttpListenerPolicyIr struct {
25+
Value string
26+
}
27+
28+
// Equals for ListenerPolicyIR that doesn't compare unexported fields
29+
// With CheckUnexported: true, this should flag the unexported fields
30+
func (l ListenerPolicyIR) Equals(other ListenerPolicyIR) bool {
31+
// NoOrigin is marked with +noKrtEquals so it shouldn't be required
32+
return true
33+
}

0 commit comments

Comments
 (0)