Skip to content
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(schema): add convenience object & enum lookup methods #21549

Merged
merged 3 commits into from
Sep 10, 2024

Conversation

aaronc
Copy link
Member

@aaronc aaronc commented Sep 4, 2024

Description

I ran into a bunch of cases where LookupType or AllTypes needed to be called on TypeSet and then type casting needed to happen at the call site. These utility functions simplify all that. TypeSet has a private method so this is non-breaking and other similar utility functions can be added in the future.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced new methods for improved type lookups: LookupEnumType and LookupObjectType.
    • Enhanced the TypeSet interface with additional methods for iterating over enum and object types.
  • Improvements

    • Streamlined validation logic for enum types, providing clearer error messages.
    • Simplified the generation of random fields by directly collecting enum types, improving performance.
  • Bug Fixes

    • Removed redundant type checks that could lead to confusion in error handling.

Copy link
Contributor

coderabbitai bot commented Sep 4, 2024

Walkthrough

Walkthrough

The changes involve enhancements to schema type handling, specifically focusing on the lookup methods for object and enum types. The CompareModuleSchemas function has been streamlined by replacing LookupType with more specific methods (LookupObjectType and LookupEnumType). Additionally, the Field struct methods have been updated to improve error handling for enum types. New methods have been added to the ModuleSchema and TypeSet interfaces to facilitate better type retrieval and iteration.

Changes

Files Change Summary
schema/diff/diff.go Updated CompareModuleSchemas to replace LookupType with LookupObjectType and LookupEnumType, simplifying type existence checks for object and enum types.
schema/field.go Modified Validate and ValidateValue methods to use LookupEnumType, improving error messages and simplifying control flow for enum type validation.
schema/module_schema.go Introduced LookupEnumType and LookupObjectType methods for specific type retrieval in ModuleSchema, enhancing type safety.
schema/module_schema_test.go Renamed LookupType to LookupObjectType in tests, removed type assertion checks, simplifying the test logic.
schema/testing/field.go Streamlined enum type handling in FieldGen and baseFieldValue functions by directly collecting enum types, improving efficiency and clarity.
schema/type.go Expanded TypeSet interface with new methods for specific type lookups (LookupEnumType, LookupObjectType) and iteration (EnumTypes, ObjectTypes), enhancing type management functionality.

Sequence Diagram(s)

sequenceDiagram
    participant OldSchema
    participant NewSchema
    participant CompareModuleSchemas
    participant Field
    participant ModuleSchema

    OldSchema->>CompareModuleSchemas: Compare Object Types
    CompareModuleSchemas->>NewSchema: LookupObjectType
    NewSchema-->>CompareModuleSchemas: Object Type Found/Not Found
    CompareModuleSchemas->>Field: Validate Enum Types
    Field->>ModuleSchema: LookupEnumType
    ModuleSchema-->>Field: Enum Type Found/Not Found
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@aaronc aaronc marked this pull request as ready for review September 4, 2024 18:40
@aaronc aaronc requested review from testinginprod and a team as code owners September 4, 2024 18:40
Copy link
Contributor

github-actions bot commented Sep 4, 2024

@aaronc your pull request is missing a changelog!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
schema/module_schema_test.go (1)

Line range hint 190-198: Approve renaming LookupType to LookupObjectType.

The function name change clarifies that it specifically looks up object types.

Consider adding a type assertion or error handling for the returned type.

Removing the type assertion simplifies the code but may introduce a risk of failing silently if the returned type is not as expected.

To ensure the returned type is an ObjectType, consider adding a type assertion or error handling:

objectType, ok := moduleSchema.LookupObjectType("object1")
if !ok {
    t.Fatalf("expected to find object type \"object1\"")
}

var _ ObjectType = objectType // Assert that objectType is of type ObjectType

if objectType.Name != "object1" {
    t.Fatalf("expected object type name \"object1\", got %q", objectType.Name)
}

Alternatively, you can use a type switch:

objectType, ok := moduleSchema.LookupObjectType("object1")
if !ok {
    t.Fatalf("expected to find object type \"object1\"")
}

switch typ := objectType.(type) {
case ObjectType:
    if typ.Name != "object1" {
        t.Fatalf("expected object type name \"object1\", got %q", typ.Name)
    }
default:
    t.Fatalf("expected objectType to be of type ObjectType, got %T", typ)
}
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5805792 and 59cc948.

Files selected for processing (6)
  • schema/diff/diff.go (3 hunks)
  • schema/field.go (2 hunks)
  • schema/module_schema.go (1 hunks)
  • schema/module_schema_test.go (1 hunks)
  • schema/testing/field.go (2 hunks)
  • schema/type.go (3 hunks)
Additional context used
Path-based instructions (6)
schema/field.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

schema/type.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

schema/diff/diff.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

schema/module_schema.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

schema/testing/field.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

schema/module_schema_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

Additional comments not posted (16)
schema/field.go (2)

41-43: LGTM!

The code changes improve the specificity of the type lookup and provide a clearer error message for missing enum types. The changes conform to the Uber Golang style guide.


72-72: LGTM!

The code change improves the specificity of the type lookup for enum field validation. The change conforms to the Uber Golang style guide.

schema/type.go (6)

20-21: LGTM!

The change to provide named return values for the LookupType method enhances clarity and conforms to the Uber Golang style guide.


22-24: LGTM!

The addition of the LookupEnumType method to the TypeSet interface improves the type management functionality and conforms to the Uber Golang style guide.


25-26: LGTM!

The addition of the LookupObjectType method to the TypeSet interface improves the type management functionality and conforms to the Uber Golang style guide.


35-38: LGTM!

The addition of the EnumTypes method to the TypeSet interface improves the type management functionality and conforms to the Uber Golang style guide.


39-42: LGTM!

The addition of the ObjectTypes method to the TypeSet interface improves the type management functionality and conforms to the Uber Golang style guide.


61-63: LGTM!

The implementations of the new methods in the emptyTypeSet struct maintain the expected behavior and conform to the Uber Golang style guide.

Also applies to: 65-67, 71-72, 73-74

schema/diff/diff.go (4)

45-46: LGTM!

The code changes are approved. The change simplifies the code by directly using LookupObjectType instead of LookupType with type assertion, which is consistent with the Uber Golang style guide.


58-59: LGTM!

The code changes are approved. The change simplifies the code by directly using LookupObjectType instead of LookupType with type assertion, which is consistent with the Uber Golang style guide.


66-67: LGTM!

The code changes are approved. The change simplifies the code by directly using LookupEnumType instead of LookupType with type assertion, which is consistent with the Uber Golang style guide.


79-80: LGTM!

The code changes are approved. The change simplifies the code by directly using LookupEnumType instead of LookupType with type assertion, which is consistent with the Uber Golang style guide.

schema/module_schema.go (2)

81-91: LGTM!

The LookupEnumType function is a useful addition to the ModuleSchema struct. It provides a convenient way to retrieve enum types by name, improving the usability of the schema.

The function is correctly implemented, following the existing pattern of the LookupType function. It handles the case where the type is not found or is not an enum type by returning an empty EnumType and false.

The code changes are approved.


94-104: LGTM!

The LookupObjectType function is a useful addition to the ModuleSchema struct. It provides a convenient way to retrieve object types by name, improving the usability of the schema.

The function is correctly implemented, following the existing pattern of the LookupType function. It handles the case where the type is not found or is not an object type by returning an empty ObjectType and false.

The code changes are approved.

schema/testing/field.go (2)

23-23: LGTM!

The code changes are approved. The change simplifies the collection of enum types and improves performance by eliminating the need for a filtering function. The change also aligns with the Uber Golang style guide.


113-114: LGTM!

The code changes are approved. The change refines the lookup for enum types by using typeSet.LookupEnumType to directly check for the existence of the enum type. The change improves the clarity and efficiency of the code related to enum type handling. The change also aligns with the Uber Golang style guide.

@aaronc aaronc added this pull request to the merge queue Sep 10, 2024
Merged via the queue into main with commit abc81a6 Sep 10, 2024
73 of 74 checks passed
@aaronc aaronc deleted the aaronc/schema-type-util branch September 10, 2024 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants