Conversation
|
🎉 This PR is included in version 3.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
There was a problem hiding this comment.
Pull request overview
This PR adds support for default values in TypeSpec model properties, enabling developers to specify default values for string, number, boolean, and enum types in their entity definitions.
Key Changes:
- Added
extractDefaultValuefunction to convert TypeSpec Value types to primitive JavaScript values - Modified
emitModelPropertyandemitAttributeto include default values in the generated ElectroDB entity schemas - Added comprehensive test coverage for default values across different data types (strings, numbers, booleans, enums) and nested models
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/main.tsp | Defines new Task entity model and Priority enum with various default values to test the feature, including optional and required properties with defaults, nested models with defaults, and different data types |
| test/entities.test.js | Adds comprehensive test suite for Task entity covering default values for all supported types (string, number, boolean, enum) and nested model properties. Also includes minor formatting improvements to existing tests |
| src/emitter.ts | Implements core functionality: adds extractDefaultValue helper function to extract default values from TypeSpec Value types, and updates both emitModelProperty and emitAttribute functions to include default values in generated attributes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Add default value if present | ||
| if (prop.defaultValue) { | ||
| const defaultValue = extractDefaultValue(prop.defaultValue); | ||
| if (defaultValue !== undefined) { | ||
| // @ts-expect-error - default is a valid ElectroDB attribute property | ||
| attr.default = defaultValue; | ||
| } | ||
| } |
There was a problem hiding this comment.
The logic for adding default values is duplicated between emitModelProperty (lines 257-264) and emitAttribute (lines 312-319). Consider refactoring to avoid duplication. Since emitAttribute calls emitType which may eventually call emitModelProperty for nested models, you could rely on emitModelProperty to handle default values for nested properties and only handle them once in emitAttribute for top-level properties.
| // Add default value if present | |
| if (prop.defaultValue) { | |
| const defaultValue = extractDefaultValue(prop.defaultValue); | |
| if (defaultValue !== undefined) { | |
| // @ts-expect-error - default is a valid ElectroDB attribute property | |
| attr.default = defaultValue; | |
| } | |
| } |
Example Usage
In TypeSpec:
Generates ElectroDB schema: