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

Embedded Structs in Code Generation #1909

Merged
merged 3 commits into from
Feb 7, 2025
Merged

Conversation

oneirocosm
Copy link
Member

This allows code generation to properly embed structs when embedded in the original types. It affects the generation of gotypes.d.ts and metaconsts.go.

Additionally, the AiSettingsType has been split off and embedded into the original SettingsType to make schema generation easier.

Copy link
Contributor

coderabbitai bot commented Feb 6, 2025

Walkthrough

The changes update several function signatures and type definitions. In the Go code generation packages, functions such as GenerateMetaMapConsts and generateTSTypeInternal now include an extra boolean parameter, which alters behavior when processing embedded structs. Calls to these functions have been updated accordingly to pass a default value (false) in non-embedded contexts. In the frontend TypeScript definitions, additional optional properties have been added to telemetry types to capture client and location information. Furthermore, a new type for AI configuration (AiSettingsType) has been introduced and embedded into the existing SettingsType, expanding the configuration structure. These modifications adjust how constants and type declarations are generated and enrich the type definitions with new fields without changing the underlying core logic.

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

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

🧹 Nitpick comments (2)
pkg/gogen/gogen.go (1)

36-39: LGTM! Well-structured handling of embedded types.

The implementation correctly handles embedded structs by:

  1. Conditionally wrapping const declarations
  2. Recursively processing anonymous fields
  3. Preventing duplicate declarations for embedded types

Consider adding a doc comment to explain the embedded parameter's purpose:

+// GenerateMetaMapConsts generates constant declarations for struct field names.
+// The embedded parameter controls whether the type is being processed as an embedded struct,
+// affecting the generation of const declaration blocks.
 func GenerateMetaMapConsts(buf *strings.Builder, constPrefix string, rtype reflect.Type, embedded bool) {

Also applies to: 47-52, 69-71

frontend/types/gotypes.d.ts (1)

809-818: Consider using type composition to avoid property duplication.

The same properties are duplicated between TEventProps and TEventUserProps. Consider extracting these common properties into a shared interface to follow the DRY principle.

+type TelemetryCommonProps = {
+    "client:arch"?: string;
+    "client:version"?: string;
+    "client:initial_version"?: string;
+    "client:buildtime"?: string;
+    "client:osrelease"?: string;
+    "client:isdev"?: boolean;
+    "autoupdate:channel"?: string;
+    "autoupdate:enabled"?: boolean;
+    "loc:countrycode"?: string;
+    "loc:regioncode"?: string;
+};

-type TEventProps = {
+type TEventProps = TelemetryCommonProps & {
-    "client:arch"?: string;
-    "client:version"?: string;
-    "client:initial_version"?: string;
-    "client:buildtime"?: string;
-    "client:osrelease"?: string;
-    "client:isdev"?: boolean;
-    "autoupdate:channel"?: string;
-    "autoupdate:enabled"?: boolean;
-    "loc:countrycode"?: string;
-    "loc:regioncode"?: string;
     // ... rest of the properties
};

-type TEventUserProps = {
+type TEventUserProps = TelemetryCommonProps & {
-    "client:arch"?: string;
-    "client:version"?: string;
-    "client:initial_version"?: string;
-    "client:buildtime"?: string;
-    "client:osrelease"?: string;
-    "client:isdev"?: boolean;
-    "autoupdate:channel"?: string;
-    "autoupdate:enabled"?: boolean;
-    "loc:countrycode"?: string;
-    "loc:regioncode"?: string;
};
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fba8505 and 2137133.

📒 Files selected for processing (6)
  • cmd/generatego/main-generatego.go (2 hunks)
  • frontend/types/gotypes.d.ts (1 hunks)
  • pkg/gogen/gogen.go (2 hunks)
  • pkg/tsgen/tsgen.go (3 hunks)
  • pkg/wconfig/metaconsts.go (0 hunks)
  • pkg/wconfig/settingsconfig.go (2 hunks)
💤 Files with no reviewable changes (1)
  • pkg/wconfig/metaconsts.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Analyze (go)
  • GitHub Check: Build for TestDriver.ai
🔇 Additional comments (5)
cmd/generatego/main-generatego.go (1)

59-59: LGTM! Function calls updated correctly.

The calls to gogen.GenerateMetaMapConsts have been properly updated to include the new embedded parameter, with false as the default value for top-level type generation.

Also applies to: 72-72

pkg/tsgen/tsgen.go (1)

179-179: LGTM! Consistent implementation with gogen.go.

The TypeScript type generation correctly handles embedded types using the same pattern as gogen.go:

  1. Conditionally wrapping type declarations
  2. Recursively processing anonymous fields
  3. Preventing duplicate declarations for embedded types

Also applies to: 186-194, 201-206, 236-238

pkg/wconfig/settingsconfig.go (2)

35-49: LGTM! Well-structured AI settings type.

The new AiSettingsType follows good practices:

  • Consistent field naming convention
  • Proper use of omitempty tags
  • Clear JSON tag prefixes with ai:

51-57: LGTM! Clean embedding of AI settings.

The AiSettingsType is properly embedded in SettingsType, which aligns with the PR's objective of improving struct embedding.

frontend/types/gotypes.d.ts (1)

771-780: LGTM! The new telemetry properties follow consistent naming and typing patterns.

The new properties are well-organized into logical groups (client info, autoupdate settings, location data) and use appropriate types for their purposes.

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

🧹 Nitpick comments (1)
pkg/gogen/gogen.go (1)

37-41: Well-structured implementation for embedded struct handling!

The changes effectively handle embedded structs by:

  • Conditionally wrapping const blocks only for top-level types
  • Recursively processing embedded fields while maintaining proper formatting
  • Using an efficient string builder approach for embedded content

The implementation is clean, efficient, and aligns perfectly with the PR objectives.

Consider adding a comment explaining the embedded struct handling behavior for future maintainers, as this is a significant architectural feature.

Also applies to: 49-54, 71-73

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2137133 and 1acb1ee.

📒 Files selected for processing (1)
  • pkg/gogen/gogen.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
  • GitHub Check: Build for TestDriver.ai
🔇 Additional comments (1)
pkg/gogen/gogen.go (1)

36-36: LGTM! Clear and purposeful signature update.

The addition of the embedded bool parameter is well-aligned with the PR's objective to support embedded struct handling.

@oneirocosm oneirocosm merged commit 134a1f1 into main Feb 7, 2025
8 checks passed
@oneirocosm oneirocosm deleted the sylvie/embedded-struct-gen branch February 7, 2025 21:13
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.

1 participant