-
Notifications
You must be signed in to change notification settings - Fork 228
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 coerce to date #308
Merged
Merged
Fix coerce to date #308
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/EpochToDateConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2016, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.utils.coerce.converters; | ||
|
||
import com.yahoo.elide.core.exceptions.InvalidAttributeException; | ||
|
||
import org.apache.commons.beanutils.Converter; | ||
import org.apache.commons.lang3.ClassUtils; | ||
|
||
import java.sql.Time; | ||
import java.sql.Timestamp; | ||
import java.util.Date; | ||
|
||
/** | ||
* Convert epoch(in string or long) to Date | ||
*/ | ||
public class EpochToDateConverter implements Converter { | ||
|
||
@Override | ||
public <T> T convert(Class<T> cls, Object value) { | ||
try { | ||
if (ClassUtils.isAssignable(value.getClass(), String.class)) { | ||
return stringToDate(cls, (String) value); | ||
} else if (ClassUtils.isAssignable(value.getClass(), Long.class, true)) { | ||
return longToDate(cls, (Long) value); | ||
} else { | ||
throw new UnsupportedOperationException(value.getClass().getSimpleName() + " is not a valid epoch"); | ||
} | ||
} catch (IndexOutOfBoundsException | ReflectiveOperationException | ||
| UnsupportedOperationException | IllegalArgumentException e) { | ||
throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); | ||
} | ||
} | ||
|
||
private static <T> T longToDate(Class<T> cls, Long epoch) throws ReflectiveOperationException { | ||
if (ClassUtils.isAssignable(cls, java.sql.Date.class)) { | ||
return (T) new java.sql.Date(epoch); | ||
} else if (ClassUtils.isAssignable(cls, Timestamp.class)) { | ||
return (T) new Timestamp(epoch); | ||
} else if (ClassUtils.isAssignable(cls, Time.class)) { | ||
return (T) new Time(epoch); | ||
} else if (ClassUtils.isAssignable(cls, Date.class)) { | ||
return (T) new Date(epoch); | ||
} else { | ||
throw new UnsupportedOperationException("Cannot convert to " + cls.getSimpleName()); | ||
} | ||
} | ||
|
||
private static <T> T stringToDate(Class<T> cls, String epoch) throws ReflectiveOperationException { | ||
return longToDate(cls, Long.parseLong(epoch)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@xiaoyao1991 @DeathByTape I've found a bug here. If you try to create entity with date field and set value to 1970-01-01 (timestamp is -14400000 in my time zone) it will be automactically converted into
Integer
object, notLong
inside elide because the value is too small and it seems like the smallest allowed number type is selected on conversion. Then it will miss this if branch and wrong date converter will be chosen.