Skip to content

refactor(filter): align pkg/common/filter with KHI coding rules - #1320

Open
kyasbal wants to merge 2 commits into
mainfrom
push-yomkrrplkzmp
Open

kyasbal wants to merge 2 commits into
mainfrom
push-yomkrrplkzmp

Conversation

@kyasbal

@kyasbal kyasbal commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

Aligns pkg/common/filter with KHI Go coding and testing conventions without modifying exported APIs or executable production code:

  • Refines GoDoc comments in pkg/common/filter/filter.go to accurately document TypedMapFilter, EnabledFilter, FilterTypedMapCollection, NewEnabledFilter, KeyToFilter, and ShouldInclude (zero executable code changes).
  • Adds pkg/common/filter/filter_test.go covering key retention, the full (includeUndefined, found, value) truth table, nil/empty inputs, zero-match nil returns, order preservation, input immutability, and custom non-boolean TypedMapFilter implementations (0% → 100.0% statement coverage).

Verification

  • make pre-push — PASS (0 mustfix, 0 advisory, 0 skipped)
  • make qa-all-advisory CODEQUALITY_SIMILARITY=true — PASS (0 mustfix, 0 advisory, 0 skipped; 0 similarity advisories)
  • go test -race -count=1 -cover ./pkg/common/filter/... ./pkg/core/task/... ./pkg/core/inspection/metadata/... ./pkg/core/inspection/... — PASS (pkg/common/filter: 100.0% statement coverage)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors comments in pkg/common/filter/filter.go and adds a new unit test file pkg/common/filter/filter_test.go to verify the filtering logic. The review feedback suggests improving the tests by comparing typedmap.TypedKey structs directly instead of using their string representations, and flattening the table-driven test cases in TestEnabledFilter_ShouldInclude and TestFilterTypedMapCollection to make them more idiomatic and maintainable.

Comment thread pkg/common/filter/filter_test.go Outdated
Comment on lines +57 to +59
if got.Key() != wantKey.Key() {
t.Errorf("KeyToFilter().Key() = %q, want %q", got.Key(), wantKey.Key())
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since typedmap.TypedKey is a comparable struct, you can compare the keys directly instead of comparing their string representations via .Key(). This is more robust and idiomatic.

Suggested change
if got.Key() != wantKey.Key() {
t.Errorf("KeyToFilter().Key() = %q, want %q", got.Key(), wantKey.Key())
}
if got != wantKey {
t.Errorf("KeyToFilter() = %v, want %v", got, wantKey)
}
Comment thread pkg/common/filter/filter_test.go Outdated
Comment on lines +70 to +115
testCases := []struct {
name string
inputs []shouldIncludeInput
want []bool
}{
// Absent keys ignore the passed value and return includeUndefined across all four combinations.
{
name: "absent key uses includeUndefined fallback",
inputs: []shouldIncludeInput{
{includeUndefined: false, found: false, value: false},
{includeUndefined: false, found: false, value: true},
{includeUndefined: true, found: false, value: false},
{includeUndefined: true, found: false, value: true},
},
want: []bool{false, false, true, true},
},
// Present keys ignore includeUndefined and return the stored value across all four combinations.
{
name: "present key uses stored value regardless of includeUndefined",
inputs: []shouldIncludeInput{
{includeUndefined: false, found: true, value: false},
{includeUndefined: false, found: true, value: true},
{includeUndefined: true, found: true, value: false},
{includeUndefined: true, found: true, value: true},
},
want: []bool{false, true, false, true},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
got := make([]bool, 0, len(tc.inputs))

// Act.
for _, input := range tc.inputs {
enabledFilter := NewEnabledFilter(key, input.includeUndefined)
got = append(got, enabledFilter.ShouldInclude(input.found, input.value))
}

// Assert.
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("ShouldInclude() mismatch (-want +got):\n%s", diff)
}
})
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The table-driven test groups multiple test inputs and expected outputs into slices within a single test case. In Go, it is more idiomatic and maintainable to flatten the table so that each test case represents a single input combination and expected output. This improves test readability and ensures that any failure points to the exact combination that failed, rather than a mismatch in a slice of results.

	testCases := []struct {
		name             string
		includeUndefined bool
		found            bool
		value            bool
		want             bool
	}{
		{
			name:             "absent key uses includeUndefined fallback (false)",
			includeUndefined: false,
			found:            false,
			value:            false,
			want:             false,
		},
		{
			name:             "absent key uses includeUndefined fallback (false, value true)",
			includeUndefined: false,
			found:            false,
			value:            true,
			want:             false,
		},
		{
			name:             "absent key uses includeUndefined fallback (true)",
			includeUndefined: true,
			found:            false,
			value:            false,
			want:             true,
		},
		{
			name:             "absent key uses includeUndefined fallback (true, value true)",
			includeUndefined: true,
			found:            false,
			value:            true,
			want:             true,
		},
		{
			name:             "present key uses stored value (false)",
			includeUndefined: false,
			found:            true,
			value:            false,
			want:             false,
		},
		{
			name:             "present key uses stored value (true)",
			includeUndefined: false,
			found:            true,
			value:            true,
			want:             true,
		},
		{
			name:             "present key uses stored value regardless of includeUndefined (false)",
			includeUndefined: true,
			found:            true,
			value:            false,
			want:             false,
		},
		{
			name:             "present key uses stored value regardless of includeUndefined (true)",
			includeUndefined: true,
			found:            true,
			value:            true,
			want:             true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Arrange.
			enabledFilter := NewEnabledFilter(key, tc.includeUndefined)

			// Act.
			got := enabledFilter.ShouldInclude(tc.found, tc.value)

			// Assert.
			if got != tc.want {
				t.Errorf("ShouldInclude() = %v, want %v", got, tc.want)
			}
		})
	}
Comment on lines +119 to +209
func TestFilterTypedMapCollection(t *testing.T) {
targetKey := typedmap.NewTypedKey[bool]("enabled")
unrelatedKey := typedmap.NewTypedKey[bool]("unrelated")

testCases := []struct {
name string
itemBatches [][]string
includeUndefined bool
mapsByItem map[string]*typedmap.ReadonlyTypedMap
want [][]string
wantMappedItems []string
}{
// Nil and empty item slices return nil without invoking getMap.
{
name: "nil and empty inputs return nil without invoking getMap",
itemBatches: [][]string{nil, {}},
want: [][]string{nil, nil},
},
// Items that are false, missing targetKey, or only have an unrelated true key return nil.
{
name: "zero matching items returns nil",
itemBatches: [][]string{{"disabled", "missing", "other-key-true"}},
mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
"disabled": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}),
"missing": newReadonlyTypedMap[bool](),
"other-key-true": newReadonlyTypedMap(mapEntry[bool]{key: unrelatedKey, value: true}),
},
want: [][]string{nil},
wantMappedItems: []string{"disabled", "missing", "other-key-true"},
},
// Matching and undefined items when includeUndefined is true are all returned in original non-sorted order.
{
name: "all matching items preserved in input order",
itemBatches: [][]string{{"item-c", "item-a", "item-b"}},
includeUndefined: true,
mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
"item-c": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
"item-a": newReadonlyTypedMap[bool](),
"item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
},
want: [][]string{{"item-c", "item-a", "item-b"}},
wantMappedItems: []string{"item-c", "item-a", "item-b"},
},
// Mixed true, false, and missing items return only true items in original order without mutating input.
{
name: "mixed items include only matches in original order without mutating input",
itemBatches: [][]string{{"item-z", "item-a", "item-m", "item-b"}},
mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
"item-z": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
"item-a": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}),
"item-m": newReadonlyTypedMap[bool](),
"item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
},
want: [][]string{{"item-z", "item-b"}},
wantMappedItems: []string{"item-z", "item-a", "item-m", "item-b"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
enabledFilter := NewEnabledFilter(targetKey, tc.includeUndefined)
originalBatches := make([][]string, len(tc.itemBatches))
for idx, batch := range tc.itemBatches {
originalBatches[idx] = slices.Clone(batch)
}
var gotMappedItems []string
getMap := func(item string) *typedmap.ReadonlyTypedMap {
gotMappedItems = append(gotMappedItems, item)
return tc.mapsByItem[item]
}

// Act.
got := make([][]string, 0, len(tc.itemBatches))
for _, batch := range tc.itemBatches {
got = append(got, FilterTypedMapCollection(batch, getMap, enabledFilter))
}

// Assert.
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("FilterTypedMapCollection() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tc.wantMappedItems, gotMappedItems); diff != "" {
t.Errorf("getMap call sequence mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(originalBatches, tc.itemBatches); diff != "" {
t.Errorf("input slices mutated (-want +got):\n%s", diff)
}
})
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test case groups multiple independent slice batches into a single test case using itemBatches [][]string. Flattening the table-driven test to use a single items []string per test case simplifies the test logic, removes the need for nested loops and complex slice cloning, and aligns with Go's idiomatic testing patterns.

func TestFilterTypedMapCollection(t *testing.T) {
	targetKey := typedmap.NewTypedKey[bool]("enabled")
	unrelatedKey := typedmap.NewTypedKey[bool]("unrelated")

	testCases := []struct {
		name             string
		items            []string
		includeUndefined bool
		mapsByItem       map[string]*typedmap.ReadonlyTypedMap
		want             []string
		wantMappedItems  []string
	}{
		{
			name:  "nil input returns nil without invoking getMap",
			items: nil,
			want:  nil,
		},
		{
			name:  "empty input returns nil without invoking getMap",
			items: []string{},
			want:  nil,
		},
		{
			name:  "zero matching items returns nil",
			items: []string{"disabled", "missing", "other-key-true"},
			mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
				"disabled":       newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}),
				"missing":        newReadonlyTypedMap[bool](),
				"other-key-true": newReadonlyTypedMap(mapEntry[bool]{key: unrelatedKey, value: true}),
			},
			want:            nil,
			wantMappedItems: []string{"disabled", "missing", "other-key-true"},
		},
		{
			name:             "all matching items preserved in input order",
			items:            []string{"item-c", "item-a", "item-b"},
			includeUndefined: true,
			mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
				"item-c": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
				"item-a": newReadonlyTypedMap[bool](),
				"item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
			},
			want:            []string{"item-c", "item-a", "item-b"},
			wantMappedItems: []string{"item-c", "item-a", "item-b"},
		},
		{
			name:  "mixed items include only matches in original order without mutating input",
			items: []string{"item-z", "item-a", "item-m", "item-b"},
			mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
				"item-z": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
				"item-a": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}),
				"item-m": newReadonlyTypedMap[bool](),
				"item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
			},
			want:            []string{"item-z", "item-b"},
			wantMappedItems: []string{"item-z", "item-a", "item-m", "item-b"},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Arrange.
			enabledFilter := NewEnabledFilter(targetKey, tc.includeUndefined)
			originalItems := slices.Clone(tc.items)
			var gotMappedItems []string
			getMap := func(item string) *typedmap.ReadonlyTypedMap {
				gotMappedItems = append(gotMappedItems, item)
				return tc.mapsByItem[item]
			}

			// Act.
			got := FilterTypedMapCollection(tc.items, getMap, enabledFilter)

			// Assert.
			if diff := cmp.Diff(tc.want, got); diff != "" {
				t.Errorf("FilterTypedMapCollection() mismatch (-want +got):\n%s", diff)
			}
			if diff := cmp.Diff(tc.wantMappedItems, gotMappedItems); diff != "" {
				t.Errorf("getMap call sequence mismatch (-want +got):\n%s", diff)
			}
			if diff := cmp.Diff(originalItems, tc.items); diff != "" {
				t.Errorf("input slice mutated (-want +got):\n%s", diff)
			}
		})
	}
}
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Go Quality Report (origin/main) — ✅ PASS

Metric Result
Changed Scope 1 prod file(s), 1 test file(s), 5 unit(s)
Patch Coverage 100.0% (0/0 lines)
Mutation Score 100.0% (0/0 killed, 0 survived)

Commit: af2eec193dc5 · Cloud Build Logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant