Skip to content

🛠️ fix(generator): fix map should not use default value for un null types - #31

Open
KiddoV wants to merge 1 commit into
Shreemanarjun:mainfrom
KiddoV:29-make-safer-fromMap
Open

🛠️ fix(generator): fix map should not use default value for un null types#31
KiddoV wants to merge 1 commit into
Shreemanarjun:mainfrom
KiddoV:29-make-safer-fromMap

Conversation

@KiddoV

@KiddoV KiddoV commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Related issue #29

I also add check for DateTime and DateTime?instead of just check forcreatedAtandmodifiedAt. All DateTime` values should be treated the same.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue in the data generator where non-nullable types were incorrectly assigned default values during map deserialization, potentially leading to data integrity problems. It also significantly refines the handling of DateTime types, making the parsing logic more generic and robust, and introduces proper support for nullable DateTime and Color fields. The changes improve the reliability of data deserialization and provide more explicit error reporting for invalid date formats.

Highlights

  • Corrected Default Value Assignment: Removed default value assignments for non-nullable types (e.g., int, String, bool, double, Duration, Uri, BigInt, List, Color) when deserializing from a map. This ensures that null values from the map are not silently converted to defaults for non-nullable fields, aligning with sound null safety principles.
  • Enhanced DateTime Handling: Generalized DateTime parsing logic to apply to all DateTime fields, not just 'createdAt' and 'modifiedAt'. This provides consistent and correct deserialization for all non-nullable DateTime properties.
  • Added Nullable DateTime and Color Support: Introduced specific handling for DateTime? and Color? types, allowing them to be correctly deserialized as null if the source map value is null, preventing incorrect default value assignments for nullable fields.
  • Improved Date Parsing Error Handling: Modified the _ParseDate utility function to throw an ArgumentError when an invalid date string is encountered. This provides clearer error feedback instead of returning a default DateTime from the epoch.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • packages/datum_generator/lib/src/datum_generator.dart
    • Removed default value assignments for various non-nullable types (e.g., int, String, bool, double, Duration, Uri, BigInt, Color, List) during map deserialization.
    • Generalized the parsing logic for non-nullable DateTime fields to apply based on type rather than specific field names.
    • Added new deserialization logic for nullable DateTime? and Color? types, allowing them to be null.
    • Updated the _ParseDate utility function to throw an ArgumentError for invalid date string inputs instead of returning a default DateTime.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses an issue where default values were used for non-nullable types during deserialization from a map. Removing these default values is a great improvement for correctness, as it will now fail fast on missing data instead of silently using a default. The generalization of DateTime handling and the improved error handling in the date parsing function are also welcome changes. I have one suggestion to improve the readability and efficiency of the generated code for double and double? types by reducing code repetition.

} else if (type == 'double') {
buffer.writeln(
" $fieldName: (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}'] ?? 0.0) is int ? (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}'] ?? 0.0).toDouble() : (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}'] ?? 0.0) as double,",
" $fieldName: (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']) is int ? (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']).toDouble() : (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']) as double,",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The expression for handling double is quite repetitive. The sub-expression (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']) is evaluated up to three times. This makes the generated code less efficient and harder to read.

You could use an immediately-invoked function expression (IIFE) to store the value in a temporary variable and avoid repetition. This would also apply to the double? case on line 735.

Suggested change
" $fieldName: (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']) is int ? (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']).toDouble() : (map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']) as double,",
" $fieldName: ((){ final v = map['$mapKey'] ?? map['${_camelToSnake(fieldName)}']; return v is int ? v.toDouble() : v as double; })(),"

@KiddoV

KiddoV commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

This might be conflict and need to be resolved with PR #30

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