You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful degradation
Status: Missing type validation: IgnoreFieldsOf accepts any structType without validation and may panic at runtime if a non-struct (or wrong shape) value is passed into cmpopts.IgnoreFields.
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent vulnerabilities
Status: Unvalidated external input: IgnoreFieldsOf takes an unvalidated caller-provided structType which can trigger panics/unsafe behavior in reflection-based comparison options if misused, rather than failing fast with a clear validation error.
func IgnoreFields(f ...string) func(interface{}) cmp.Option {
return func(i interface{}) cmp.Option {
t := reflect.TypeOf(i)
- if t.Kind() == reflect.Ptr { // dereference pointers- i = reflect.New(t.Elem()).Elem().Interface()+ if t == nil {+ return cmp.Options{}
}
- // get the type of element of a slice/array- if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {- i = reflect.New(t.Elem()).Elem().Interface()++ switch t.Kind() {+ case reflect.Slice, reflect.Array:+ t = t.Elem()
}
++ if t.Kind() == reflect.Ptr {+ t = t.Elem()+ }++ i = reflect.New(t).Elem().Interface()
return cmpopts.IgnoreFields(i, f...)
}
}
[To ensure code accuracy, apply this suggestion manually]
Suggestion importance[1-10]: 8
__
Why: This suggestion correctly identifies a bug in the existing IgnoreFields function that causes a panic when used with a slice of pointers. The proposed fix is accurate and makes the function more robust by correctly handling pointer types within slices.
Medium
Prevent panic on nil struct type
Add a nil check to the IgnoreFieldsOf function to prevent a panic when a nil pointer is passed as the structType, returning a no-op option instead.
// IgnoreFieldsOf ignores specific fields on a given struct type.
// Use this when ignoring fields in embedded structs where IgnoreFields
// cannot infer the correct type.
//
// trial.EqualOpt(trial.IgnoreFieldsOf(Metadata{}, "CreatedAt", "UpdatedAt"))
func IgnoreFieldsOf(structType interface{}, fields ...string) func(interface{}) cmp.Option {
return func(_ interface{}) cmp.Option {
+ if v := reflect.ValueOf(structType); v.Kind() == reflect.Ptr && v.IsNil() {+ return cmp.Options{}+ }
return cmpopts.IgnoreFields(structType, fields...)
}
}
Apply / Chat
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies a potential panic in the new IgnoreFieldsOf function when a nil pointer to a struct is passed. Adding a nil check improves the function's robustness and prevents unexpected test failures.
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
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.
PR Type
Enhancement, Documentation
Description
Add new
IgnoreFieldsOffunction for embedded struct field comparisonExpand CI/CD testing to Go 1.20-1.24 and update GitHub Actions versions
Create comprehensive documentation with API reference, comparers guide, examples, and helpers
Improve code formatting and documentation in README with cleaner structure
Comment out unused const block and fix minor typos throughout codebase
Diagram Walkthrough
File Walkthrough
1 files
Add IgnoreFieldsOf and comment unused constants2 files
Fix struct literal syntax and typo in test namesSimplify struct initialization syntax in test cases1 files
Expand Go versions and update GitHub Actions versions7 files
Restructure documentation with new doc files and improve formattingCreate comprehensive API reference documentationCreate detailed comparers guide with examples and use casesCreate practical examples and patterns for trial usageCreate helpers documentation with all utility functionsAdd comment explaining go-cmp version compatibilityRemove obsolete test case documentation file