-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat(depinject): support ignored fields in input structs #22409
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,11 @@ import ( | |
// | ||
// Fields of the struct may support the following tags: | ||
// | ||
// optional if set to true, the dependency is optional and will | ||
// be set to its default value if not found, rather than causing | ||
// an error | ||
// optional if set to true, the dependency is optional and will | ||
// be set to its default value if not found, rather than causing | ||
// an error | ||
// | ||
// ignored if set to any value the dependency will be ignored | ||
Comment on lines
+15
to
+19
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. 💡 Codebase verification Test coverage exists but could be more comprehensive The
🔗 Analysis chainVerify test coverage for ignored fields. Let's verify that there are adequate test cases for the new ignored field functionality. Also applies to: 118-122, 180-184 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for test cases related to ignored fields
echo "Searching for test cases covering ignored fields..."
rg -l "ignored.*tag" --type go "depinject/*_test.go"
Length of output: 227 Script: #!/bin/bash
# Let's first find the test files in the repository
fd "_test.go$" --type f
# Then search for any mentions of "ignored" in test files
rg "ignored" --type go -C 3 ".*_test\.go$"
# Also search for struct tag related tests
rg "struct.*tag" --type go -C 3 ".*_test\.go$"
# Let's also check the implementation of ignored field handling
ast-grep --pattern 'if $_.Tag.Get("ignored") $$$'
Length of output: 27881 Script: #!/bin/bash
# Let's search for the implementation of ignored tag handling in struct_args.go
rg -C 5 "ignored.*tag" depinject/struct_args.go
# Let's also check for any tests in the depinject directory
rg -C 5 "ignored" "depinject/*_test.go"
# Let's look for the struct field processing implementation
ast-grep --pattern 'func $_(field reflect.StructField, $$$) {
$$$
}'
Length of output: 222 Script: #!/bin/bash
# Let's check the actual implementation in struct_args.go
cat depinject/struct_args.go
# Let's find test files in depinject directory with correct path
fd "_test.go$" depinject/
# Let's also check for any struct tag processing in the codebase
ast-grep --pattern 'type $_ struct {
$$$
`$$$"ignored"$$$`
$$$
}'
Length of output: 5560 Script: #!/bin/bash
# Let's check the test files for any test cases involving ignored tag
rg -C 5 "ignored" depinject/container_test.go depinject/binding_test.go
# Also check for any struct definitions with tags in tests
ast-grep --pattern 'type $_ struct {
$_ $_ `$_`
}' depinject/container_test.go depinject/binding_test.go
Length of output: 728 |
||
type In struct{} | ||
|
||
func (In) isIn() {} | ||
|
@@ -113,6 +115,11 @@ func structArgsInTypes(typ reflect.Type) ([]providerInput, error) { | |
continue | ||
} | ||
|
||
_, found := f.Tag.Lookup("ignored") | ||
if found { | ||
continue | ||
} | ||
|
||
var optional bool | ||
optTag, found := f.Tag.Lookup("optional") | ||
if found { | ||
|
@@ -170,6 +177,11 @@ func buildIn(typ reflect.Type, values []reflect.Value) (reflect.Value, int, erro | |
if f.Type.AssignableTo(isInType) { | ||
continue | ||
} | ||
_, found := f.Tag.Lookup("ignored") | ||
if found { | ||
continue | ||
} | ||
|
||
Comment on lines
+180
to
+184
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. 🛠️ Refactor suggestion Consider extracting the ignored tag check into a helper function. The ignored tag check is duplicated between +func isIgnoredField(f reflect.StructField) bool {
+ _, found := f.Tag.Lookup("ignored")
+ return found
+}
func buildIn(typ reflect.Type, values []reflect.Value) (reflect.Value, int, error) {
// ...
- _, found := f.Tag.Lookup("ignored")
- if found {
+ if isIgnoredField(f) {
continue
}
// ...
}
func structArgsInTypes(typ reflect.Type) ([]providerInput, error) {
// ...
- _, found := f.Tag.Lookup("ignored")
- if found {
+ if isIgnoredField(f) {
continue
}
// ...
}
|
||
if !res.Elem().Field(i).CanSet() { | ||
return reflect.Value{}, 0, fmt.Errorf("depinject.In struct %s on package %s can't have unexported field", res.Elem().String(), f.PkgPath) | ||
} | ||
|
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.
🛠️ Refactor suggestion
Enhance test coverage with additional assertions
While the test verifies successful injection, it should also validate:
Consider enhancing the test with these assertions:
📝 Committable suggestion