Skip to content

Conversation

@bkrukowski
Copy link
Member

@bkrukowski bkrukowski commented Oct 1, 2024

Summary by Sourcery

Implement a new feature to iterate over struct fields, enabling setting and getting values through callbacks. Refactor existing code to support this functionality and add extensive tests and examples to validate the new feature.

New Features:

  • Introduce a new feature to iterate over fields of a struct, allowing setting and getting field values using callbacks.

Enhancements:

  • Refactor the Set function to use a new helper function reducedStructValueOf for obtaining the reflect value and kind chain of a struct.

Build:

  • Add a new dependency on github.com/davecgh/go-spew for testing purposes in the go.mod file.

Tests:

  • Add comprehensive tests for the new IterateFields functionality, including various scenarios for setting and getting field values.
  • Add example tests demonstrating the usage of the new IterateFields feature with different struct configurations.

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 1, 2024

Reviewer's Guide by Sourcery

This pull request implements a new feature for reading and setting all fields from a given struct. It introduces a new package called 'fields' with functionality to iterate over struct fields, set and get their values, and handle various edge cases. The changes also include extensive testing and examples to demonstrate the new functionality.

Class diagram for the new 'fields' package

classDiagram
    class fields {
        +Iterate(any, ...Option) error
        +PrefillNilStructs(bool) Option
        +Setter(func(Path, any) (any, bool)) Option
        +Getter(func(Path, any)) Option
        +ConvertTypes(bool) Option
        +ConvertToPointers(bool) Option
        +Recursive(bool) Option
    }
    class config {
        +setter func(Path, any) (any, bool)
        +getter func(Path, any)
        +prefillNilStructs bool
        +convertTypes bool
        +convertToPtr bool
        +recursive bool
    }
    class Path {
        +Names() []string
        +HasSuffix(...string) bool
        +EqualNames(...string) bool
    }
    fields --> config
    fields --> Path
    class intReflect {
        +IterateFields(any, FieldCallback, bool, bool) error
    }
    fields --> intReflect
    class FieldCallback {
        +func(reflect.StructField, any) (any, bool)
    }
    intReflect --> FieldCallback
Loading

File-Level Changes

Change Details Files
Implement new 'fields' package for struct field manipulation
  • Add Iterate function to traverse struct fields
  • Implement Setter and Getter options for field manipulation
  • Add support for recursive field traversal
  • Implement PrefillNilStructs option to automatically initialize nil struct pointers
  • Add ConvertTypes and ConvertToPointers options for type conversion
  • Implement Path type for tracking field hierarchy
fields/iterate.go
fields/path.go
fields/any.go
Add extensive testing for the new 'fields' package
  • Create test scenarios for various struct configurations
  • Test setting and getting field values
  • Test recursive field traversal
  • Test handling of unexported fields
  • Add examples demonstrating package usage
fields/iterate_test.go
fields/examples_test.go
Refactor and enhance internal reflect package
  • Add IterateFields function for low-level field iteration
  • Implement reducedStructValueOf helper function
  • Update existing Get and Set functions to use new field iteration logic
internal/reflect/iterate.go
internal/reflect/common.go
internal/reflect/get_set.go
internal/reflect/get_set_test.go
Update project dependencies
  • Add github.com/davecgh/go-spew dependency for testing
go.mod

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bkrukowski - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@coveralls
Copy link

coveralls commented Oct 1, 2024

Pull Request Test Coverage Report for Build 11134023798

Details

  • 180 of 226 (79.65%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-5.0%) to 92.317%

Changes Missing Coverage Covered Lines Changed/Added Lines %
fields/iterate.go 103 105 98.1%
fields/path.go 9 27 33.33%
internal/reflect/iterate.go 46 72 63.89%
Totals Coverage Status
Change from base Build 11133485394: -5.0%
Covered Lines: 745
Relevant Lines: 807

💛 - Coveralls

@coveralls
Copy link

Pull Request Test Coverage Report for Build 11133578842

Details

  • 176 of 222 (79.28%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-5.0%) to 92.279%

Changes Missing Coverage Covered Lines Changed/Added Lines %
fields/iterate.go 99 101 98.02%
fields/path.go 9 27 33.33%
internal/reflect/iterate.go 46 72 63.89%
Totals Coverage Status
Change from base Build 11133485394: -5.0%
Covered Lines: 741
Relevant Lines: 803

💛 - Coveralls

@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 1, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
4.1% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@bkrukowski
Copy link
Member Author

#11

@bkrukowski bkrukowski closed this Oct 1, 2024
@bkrukowski bkrukowski deleted the feat/fields branch October 1, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants