-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
fix: ZModel generator support JSON Type #1965
Conversation
📝 WalkthroughWalkthroughThe pull request introduces modifications to the schema and code generation functionalities. In the schema file, a new Changes
Sequence DiagramsequenceDiagram
participant CodeGenerator
participant AST
participant TypeDef
participant TypeDefField
CodeGenerator->>AST: Parse type definition
AST->>TypeDef: Extract type information
TypeDef->>TypeDefField: Process individual fields
CodeGenerator->>TypeDef: Generate type definition string
CodeGenerator->>TypeDefField: Generate field representation
The sequence diagram illustrates the new type definition generation process, showing how the code generator interacts with the abstract syntax tree (AST) to create type definitions and their corresponding fields. 📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (12)
📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (3)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/schema/tests/schema/all-features.zmodel (1)
181-190
: Consider using Metadata type for better type safety.The
metadata
field in theImage
model is currently usingJson
type, while there's a well-definedMetadata
type available. Consider using theMetadata
type directly for better type safety and schema validation at runtime.model Image { id Int @id @default(autoincrement()) - metadata Json + metadata Metadata }packages/sdk/src/zmodel-code-generator.ts (1)
Line range hint
183-190
: Consider using type predicate for better type narrowing.The type check could be improved using a type predicate function for clearer type discrimination.
+private isDataModelFieldType(type: DataModelFieldType | TypeDefFieldType): type is DataModelFieldType { + return type.$type === 'DataModelFieldType'; +} private fieldType(type: DataModelFieldType | TypeDefFieldType) { const baseType = type.type ? type.type - : type.$type == 'DataModelFieldType' && type.unsupported + : this.isDataModelFieldType(type) && type.unsupported ? 'Unsupported(' + this.generate(type.unsupported.value) + ')' : type.reference?.$refText; return `${baseType}${type.array ? '[]' : ''}${type.optional ? '?' : ''}`; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
packages/sdk/package.json
is excluded by!**/*.json
📒 Files selected for processing (2)
packages/schema/tests/schema/all-features.zmodel
(6 hunks)packages/sdk/src/zmodel-code-generator.ts
(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build-test (20.x)
🔇 Additional comments (8)
packages/schema/tests/schema/all-features.zmodel (6)
39-47
: LGTM! Clean formatting of Space model properties.The properties are well-aligned and maintain consistent spacing.
69-76
: LGTM! Clean formatting of SpaceUser model properties.The properties maintain consistent alignment and spacing.
95-106
: LGTM! Clean formatting of User model properties.The properties are well-organized with consistent spacing.
122-131
: LGTM! Clean formatting of List model properties.The properties maintain consistent alignment and spacing.
154-161
: LGTM! Clean formatting of Todo model properties.The properties are well-aligned with consistent spacing.
176-179
: LGTM! Well-structured view definition.The view captures essential space fields with appropriate constraints.
packages/sdk/src/zmodel-code-generator.ts (2)
42-44
: LGTM! Necessary imports for type definition support.The imports maintain consistent style with other AST types.
339-344
: LGTM! Well-implemented type definition field generation.The implementation follows the established patterns and maintains consistency with other field generators.
There was a problem hiding this 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)
packages/sdk/src/zmodel-code-generator.ts (1)
183-189
: Consider using type guards for better type safety and readability.The current implementation works but could be more maintainable with explicit type guards.
Consider this refactor:
- private fieldType(type: DataModelFieldType | TypeDefFieldType) { + private fieldType(type: DataModelFieldType | TypeDefFieldType) { + function isDataModelFieldType(t: DataModelFieldType | TypeDefFieldType): t is DataModelFieldType { + return t.$type === 'DataModelFieldType'; + } + const baseType = type.type ? type.type - : type.$type == 'DataModelFieldType' && type.unsupported + : isDataModelFieldType(type) && type.unsupported ? 'Unsupported(' + this.generate(type.unsupported.value) + ')' : type.reference?.$refText; return `${baseType}${type.array ? '[]' : ''}${type.optional ? '?' : ''}`; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/sdk/src/zmodel-code-generator.ts
(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build-test (20.x)
🔇 Additional comments (2)
packages/sdk/src/zmodel-code-generator.ts (2)
42-44
: LGTM! Import statements are well-organized.The new type imports are correctly placed and necessary for implementing the type definition functionality.
328-344
: LGTM! Type definition generation methods are well-implemented.The new methods follow consistent patterns with existing code:
- Proper decoration with @gen
- Consistent handling of attributes
- Correct indentation and formatting
Note: There was a past review comment about a typo in the method name
_genearteTypeDef
, but I can see that it has been fixed to_generateTypeDef
in this version.
No description provided.