close
Skip to content

If a field is exported, generate json even in the absence of tags#3678

Merged
leaanthony merged 3 commits into
wailsapp:masterfrom
pbnjay:json-revamp
Aug 13, 2024
Merged

If a field is exported, generate json even in the absence of tags#3678
leaanthony merged 3 commits into
wailsapp:masterfrom
pbnjay:json-revamp

Conversation

@pbnjay

@pbnjay pbnjay commented Aug 13, 2024

Copy link
Copy Markdown
Contributor

Description

Fixes #945 - generate typescript models for structs that do not contain json tags, using similar logic as Go's JSON interfaces (e.g. in the absence of json tags, if the field is exported then include it as-is).

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

I'm not sure the "proper" way to test, but I vendored wails in my app with these changes and looked at the resulting models.ts output.

Test Configuration

For brevity I will skip wails doctor output here but I can include if necessary.

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • New Features

    • Improved handling of exported fields in JSON serialization, ensuring those without a JSON tag are recognized appropriately.
    • Enhanced TypeScript definition generation to reflect accurate JSON field names and optionality for fields that are pointers.
  • Bug Fixes

    • Corrected logic for checking JSON field names, leading to more accurate serialization and TypeScript definitions.
  • Documentation

    • Updated changelog to document the fixes related to binding struct fields without associated JSON tags, improving data integrity and usability.
  • Tests

    • Added new test cases to validate the behavior of model generation for entities without field tags, expanding test coverage.

@coderabbitai

coderabbitai Bot commented Aug 13, 2024

Copy link
Copy Markdown
Contributor

Walkthrough

The recent changes improve the handling of exported fields in JSON serialization and TypeScript generation from Go structs. Key modifications include adding checks for the presence of "json" tags in exported fields, ensuring that fields without tags are correctly recognized and named. This addresses previous issues related to model generation for output types, enhancing the functionality and usability of the binding mechanism.

Changes

Files Change Summary
v2/internal/binding/binding.go Updated hasExportedJSONFields to check for "json" tag presence, recognizing exported fields without tags.
v2/internal/typescriptify/typescriptify.go Modified getJSONFieldName to use Tag.Lookup for "json", improving naming for exported fields.
v2/internal/binding/binding_test/binding_notags_test.go Added tests for the NoFieldTags struct to validate binding behavior without field tags.
v2/internal/binding/binding_test/binding_test.go Included NoFieldTagsTest to enhance test coverage for model generation scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant GoStruct
    participant JSONSerializer
    participant TypeScriptGenerator
    GoStruct->>JSONSerializer: Check for exported fields
    JSONSerializer->>GoStruct: Identify exported fields without "json" tag
    GoStruct->>TypeScriptGenerator: Generate model from Go struct
    TypeScriptGenerator->>TypeScriptModel: Create TypeScript definition with field names
Loading

Assessment against linked issues

Objective Addressed Explanation
Models should be generated (e.g., for output types) (#945)
Methods on Models should also be available by JavaScript (#945) Unclear if the changes allow method access from JS.

🐰 "In fields so bright and true,
With JSON tags and names anew,
We hop along, our models clear,
In TypeScript land, we now appear!
A joyous leap, a code-filled cheer!" 🐇✨


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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2b35861 and aedfcd0.

Files selected for processing (2)
  • v2/internal/binding/binding.go (1 hunks)
  • v2/internal/typescriptify/typescriptify.go (1 hunks)
Additional comments not posted (2)
v2/internal/binding/binding.go (1)

353-355: Ensure exported fields without JSON tags are correctly handled.

The logic now considers exported fields without JSON tags as having exported JSON fields. This change aligns with the PR objectives to generate TypeScript models for such fields.

v2/internal/typescriptify/typescriptify.go (1)

556-562: Handle exported fields without JSON tags and mark optional fields.

The method now names exported fields without JSON tags based on their field name and appends a question mark for optional fields. This change improves TypeScript definition generation.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aedfcd0 and fd35682.

Files selected for processing (2)
  • v2/internal/binding/binding_test/binding_notags_test.go (1 hunks)
  • v2/internal/binding/binding_test/binding_test.go (1 hunks)
Additional comments not posted (3)
v2/internal/binding/binding_test/binding_notags_test.go (2)

3-8: Struct NoFieldTags is well-defined.

The struct NoFieldTags is a straightforward representation of a Go struct without JSON tags, which aligns with the purpose of the test.


14-59: Test case NoFieldTagsTest is comprehensive.

The test case NoFieldTagsTest effectively verifies the TypeScript model generation for structs without JSON tags. The expected TypeScript output is clearly defined, and the test covers various field types, including pointers and nested structs.

v2/internal/binding/binding_test/binding_test.go (1)

53-53: Integration of NoFieldTagsTest enhances test coverage.

The addition of NoFieldTagsTest to the TestBindings_GenerateModels function improves the test coverage by validating the model generation for structs without JSON tags. This aligns with the PR objectives and ensures robust functionality.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fd35682 and 260e85d.

Files selected for processing (1)
  • website/src/pages/changelog.mdx (1 hunks)
Additional comments not posted (1)
website/src/pages/changelog.mdx (1)

22-22: Changelog entry is clear and informative.

The entry provides a concise description of the fix and references the associated pull request, ensuring traceability.

@leaanthony leaanthony left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good!! 👍

@leaanthony
leaanthony merged commit fe1f23b into wailsapp:master Aug 13, 2024
therealsamf pushed a commit to therealsamf/wails that referenced this pull request Aug 29, 2024
…ilsapp#3678)

* if no JSON tag, check if field is exported

* add no-tags binding test case

* update changelog for wailsapp#3678
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.

Models are not generated in Models.ts for output types

2 participants