Skip to content

Unaware if Null on set Timestamp #2417

Closed

Description

@Nosfert

Hi
Im trying to use Timestamp when set'ing the Entity that im saving.
But if the code send in null then i only get an exception back.
I can manually say that the Key should be null but then i need to know beforehand that the value im sending in is null....
I think this needs to be fixed.
Timestamp timestampThatCanBeNull = getTimestampOrNull(); Entity.newBuilder(keyFactory.newKey()).set("TimestampThatCanSometimesBeNull", consumableFormatId)
I think this is true for not only Timestamp but the other types, instead of return a exception it should use NullValue

Activity

  1. Nosfert commented on Sep 8, 2017

    @Nosfert
    Author

    i solved it by creating a convinience method to take builder property key/value and then do a lookup.
    But feels weird that it's not supported in the framework.

  2. added
    api: datastoreIssues related to the Datastore API.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Sep 11, 2017
  3. andreamlin commented on Sep 26, 2017

    @andreamlin
    Contributor

    Is this for the com.google.cloud.datastore.Entity class?
    If so, that neither the Entity class nor its Builder has a .set() method.

    Can you provide a valid code snippet to clarify the issue?

  4. Nosfert commented on Sep 28, 2017

    @Nosfert
    Author

    Sorry for not providing enough info.

    It's the BaseEntity and it's builder that I'm talking about.

    My solution to the issue is as follows.
    private void setTimestampOrNull(BaseEntity.Builder builder, String key, Timestamp timestamp) { if (timestamp != null) { builder.set(key, timestamp); } else { builder.setNull(key); } }
    Because a value can be Null in datastore it feels normal to have the option to pass in null while setting the value and not having to do this if null type of check.

  5. andreamlin commented on Sep 29, 2017

    @andreamlin
    Contributor

    Thanks for the clarification! Your setTimestampOrNull() example looks like a simple, clean workaround :)

    The javadoc for the set(String name, Timestamp value) function, and others like it, say that it "sets a property of type TimestampValue, which "must be associated with non-null content" (see com.google.cloud.datastore.Value javadoc). If a null timestamp was passed in, there would be no way for the set() function to fulfill its contract, because TimestampValue cannot be null, and NullValue is not the same type as TimestampValue.

    I am biased towards requiring that the property values in set() be non-null, and explicitly calling the setNull() function for null values, so that the client isn't surprised when a get() returns a null value, because the client must have explicitly called the setNull() previously for that to happen.

    @vam-google, any thoughts?

  6. Nosfert commented on Oct 4, 2017

    @Nosfert
    Author

    Would it be possible to something like
    builder.orElseSetNull(key).set(key, timestamp);
    It will catch the nullpoint exception and then set the key to be null.

    So that the developer can easily solve this without having to create their own solution to this problem?

  7. yihanzhen commented on Jan 29, 2018

    @yihanzhen
    Contributor

    Move this issue to feature request backlog and close it here. This place can continue to be used for discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions