-
Notifications
You must be signed in to change notification settings - Fork 0
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
Date Handlers #11
Date Handlers #11
Conversation
WalkthroughThe pull request introduces support for handling Changes
Assessment against linked issues
Poem
Finishing Touches
🪧 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: 3
🧹 Nitpick comments (7)
pibify-maven-plugin/src/test/java/com/flipkart/pibify/test/data/ClassWithReferences.java (2)
40-41
: Add documentation for the date field.Consider adding a Javadoc comment to describe the purpose and format of the date field.
+ /** + * The date field to store timestamp information. + */ @Pibify(3) private Date date;
45-45
: Consider using a fixed timestamp for testing.Using
System.currentTimeMillis()
makes tests non-deterministic. Consider using a fixed timestamp for consistent test results.- date = new Date(System.currentTimeMillis()); + date = new Date(1704067200000L); // 2025-01-01 00:00:00 UTCpibify-core/src/main/java/com/flipkart/pibify/codegen/stub/DateHandler.java (2)
28-33
: Enhance class documentation.The current documentation is minimal. Consider adding more details about the serialization format and usage.
/** - * This class is used for serde of Date + * Handler for serializing and deserializing java.util.Date objects. + * + * Serialization format: + * - Writes the date as milliseconds since epoch using tag 1 + * - Deserialization reads the milliseconds using tag 8 + * + * Usage: + * This handler is automatically registered in AbstractPibifyHandlerCache + * for handling Date fields annotated with @Pibify + * * Author bageshwar.pn * Date 15/01/25 */
46-65
: Simplify deserialization logic.The current implementation can be simplified using early returns and removing unnecessary variables.
@Override public Date deserialize(IDeserializer deserializer, Class<Date> type, SerializationContext context) throws PibifyCodeExecException { try { - int tag = deserializer.getNextTag(); - Date date = null; - while (tag != 0 && tag != PibifyGenerated.getEndObjectTag()) { - switch (tag) { - case 8: - date = new Date(deserializer.readLong()); - break; - default: - break; - } - tag = deserializer.getNextTag(); + while (true) { + int tag = deserializer.getNextTag(); + if (tag == 0 || tag == PibifyGenerated.getEndObjectTag()) { + return null; + } + if (tag == 8) { + return new Date(deserializer.readLong()); + } } - return date; } catch (IOException e) { throw new PibifyCodeExecException(e); } }Handlers.md (3)
1-11
: LGTM! Consider adding return type documentation.The Handler concept is well explained. Consider adding documentation for the return types and possible exceptions.
public abstract T deserialize(IDeserializer deserializer, Class<T> type, SerializationContext context) - throws PibifyCodeExecException; + throws PibifyCodeExecException; // Throws when deserialization fails + // Returns: Deserialized object of type T public abstract void serialize(T object, ISerializer serializer, SerializationContext context) - throws PibifyCodeExecException; + throws PibifyCodeExecException; // Throws when serialization fails
18-27
: LGTM! Consider adding handler examples.The out-of-the-box handlers section is clear. Consider adding simple usage examples for each handler type.
35-43
: Add error handling in the example.The custom handler example should demonstrate proper error handling.
public class CustomHandlerCache extends AbstractPibifyHandlerCache { public CustomHandlerCache() { super(); - // Type is the class type of the object - super.mapBuilder.put(type, new CustomHandler()); + try { + // Type is the class type of the object + super.mapBuilder.put(type, new CustomHandler()); + } catch (Exception e) { + throw new IllegalStateException("Failed to register custom handler", e); + } } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
Handlers.md
(1 hunks)README.md
(1 hunks)pibify-core/src/main/java/com/flipkart/pibify/codegen/stub/AbstractPibifyHandlerCache.java
(2 hunks)pibify-core/src/main/java/com/flipkart/pibify/codegen/stub/DateHandler.java
(1 hunks)pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/BeanIntrospectorBasedCodeGenSpecCreatorTest.java
(1 hunks)pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/CodeGeneratorImplTest.java
(1 hunks)pibify-maven-plugin/src/test/java/com/flipkart/pibify/test/data/ClassWithReferences.java
(3 hunks)
👮 Files not reviewed due to content moderation or server errors (2)
- pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/CodeGeneratorImplTest.java
- README.md
🧰 Additional context used
🪛 LanguageTool
Handlers.md
[misspelling] ~30-~30: This word is normally spelled as one.
Context: ...tected constructor. #### Adding custom hand-written handlers If you want to add custom han...
(EN_COMPOUNDS_HAND_WRITTEN)
🔇 Additional comments (7)
pibify-maven-plugin/src/test/java/com/flipkart/pibify/test/data/ClassWithReferences.java (3)
24-24
: LGTM!The import statement for
java.util.Date
is correctly placed.
77-79
: LGTM!The equals method correctly includes the date field in the comparison using
Objects.equals()
.
84-84
: LGTM!The hashCode method correctly includes the date field using
Objects.hash()
.pibify-core/src/main/java/com/flipkart/pibify/codegen/stub/AbstractPibifyHandlerCache.java (2)
27-27
: LGTM!The import statement for
java.util.Date
is correctly placed.
79-79
: LGTM!The DateHandler is correctly registered alongside other built-in type handlers.
pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/BeanIntrospectorBasedCodeGenSpecCreatorTest.java (2)
327-327
: LGTM!The field count assertion is correctly updated to include the new date field.
332-341
: LGTM!The assertions comprehensively verify all aspects of the date field:
- Field name and index
- Getter and setter methods
- Data type and reference type
public void setDate(Date date) { | ||
this.date = date; | ||
} |
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.
🛠️ Refactor suggestion
Implement defensive copying in the setter.
Setting the Date
object directly makes the class vulnerable to external modifications. Consider using a defensive copy.
public void setDate(Date date) {
- this.date = date;
+ this.date = date != null ? new Date(date.getTime()) : null;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
public void setDate(Date date) { | |
this.date = date; | |
} | |
public void setDate(Date date) { | |
this.date = date != null ? new Date(date.getTime()) : null; | |
} |
public Date getDate() { | ||
return date; | ||
} |
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.
🛠️ Refactor suggestion
Implement defensive copying in the getter.
Returning the Date
object directly allows callers to modify its state. Consider returning a defensive copy.
public Date getDate() {
- return date;
+ return date != null ? new Date(date.getTime()) : null;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
public Date getDate() { | |
return date; | |
} | |
public Date getDate() { | |
return date != null ? new Date(date.getTime()) : null; | |
} |
@Override | ||
public void serialize(Date object, ISerializer serializer, SerializationContext context) throws PibifyCodeExecException { | ||
if (object != null) { | ||
try { | ||
serializer.writeLong(1, object.getTime()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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.
Fix tag inconsistency and improve error handling.
The code has the following issues:
- Uses different tags for serialization (1) and deserialization (8).
- Wraps IOException in RuntimeException instead of PibifyCodeExecException.
@Override
public void serialize(Date object, ISerializer serializer, SerializationContext context) throws PibifyCodeExecException {
if (object != null) {
try {
- serializer.writeLong(1, object.getTime());
+ serializer.writeLong(8, object.getTime());
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new PibifyCodeExecException(e);
}
}
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
@Override | |
public void serialize(Date object, ISerializer serializer, SerializationContext context) throws PibifyCodeExecException { | |
if (object != null) { | |
try { | |
serializer.writeLong(1, object.getTime()); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
@Override | |
public void serialize(Date object, ISerializer serializer, SerializationContext context) throws PibifyCodeExecException { | |
if (object != null) { | |
try { | |
serializer.writeLong(8, object.getTime()); | |
} catch (IOException e) { | |
throw new PibifyCodeExecException(e); | |
} | |
} | |
} |
Docstrings generation was requested by @bageshwar. * #11 (comment) The following files were modified: * `pibify-core/src/main/java/com/flipkart/pibify/codegen/stub/AbstractPibifyHandlerCache.java` * `pibify-core/src/main/java/com/flipkart/pibify/codegen/stub/DateHandler.java` * `pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/BeanIntrospectorBasedCodeGenSpecCreatorTest.java` * `pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/CodeGeneratorImplTest.java` * `pibify-maven-plugin/src/test/java/com/flipkart/pibify/test/data/ClassWithReferences.java`
Note We have generated docstrings for this pull request, at #12 |
Fixes #8
Summary by CodeRabbit
Documentation
Handler
concept inREADME.md
Handlers.md
New Features
Date
type handling in serialization and deserializationDateHandler
for managingDate
object conversionsTests
Date
field inClassWithReferences