Skip to content
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

Feedback requested for accessing property values #25

Closed
blowmage opened this issue Nov 12, 2014 · 6 comments
Closed

Feedback requested for accessing property values #25

blowmage opened this issue Nov 12, 2014 · 6 comments
Assignees
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.

Comments

@blowmage
Copy link
Contributor

How should Datastore's Entity behave when accessing properties that have not yet been set?

entity = Gcloud::Datastore::Entity.new
entity["name"] = "John Jacob Jingleheimer Schmidt"
puts entity["name"] #=> John Jacob Jingleheimer Schmidt
puts entity["email"] #=> ???

The current behavior is to return nil. Another option is to raise an error.

IMO the behavior should be permissive. Consider the case of a projection query where entity records stored in Datastore have both name and email, but only name is projected in the query. In ActiveRecord the attributes that are not selected are set to nil.

query = Gcloud::Datastore::Query.new.kind("User").select("name")
entities = Gcloud::Datastore.connection.run query
entities.each do |entity|
  puts entity["email"]
end

Thoughts?

@silvolu
Copy link
Contributor

silvolu commented Nov 12, 2014

Agree with you we should be permissive, unless we do something like registering kinds (as proposed in googleapis/google-cloud-node#157), and then we'd want to enforce a schema.
That proposal is currently blocked on the feature being implemented in the API itself.

@jgeewax
Copy link

jgeewax commented Jan 26, 2015

/cc @GoogleCloudPlatform/cloud-datastore

@pcostell
Copy link
Contributor

The next version of the API will have an explicit null value. So something to consider is that if entity[...] returns nil, there will be no way to distinguish between a set null value and a missing value.

@blowmage
Copy link
Contributor Author

For the past month or so I've been thinking about differentiating between and entity not having a property vs. having a property with a NULL value. Closely related is how to remove a property from an entity vs. setting the property's value to NULL. First, a bit of background. Currently the Entity has a properties method that returns an array of arrays of the properties and values. But this is a read-only list, and modifications aren't possible.

entity["name"] = "Mike"
entity["email"] = "[email protected]"
entity.properties #=> [["name", "Mike"], ["email", "[email protected]"]]
entity.properties.clear #=> []
entity.properties #=> [["name", "Mike"], ["email", "[email protected]"]]

I'd like to change properties to return a hash-like object that can be inspected and modified:

entity["name"] = "Mike"
entity["email"] = "[email protected]"
entity.properties #=> {"name" => "Mike", "email" => "[email protected]"}

# Check for a property that exists
entity.properties.exist? "name" #=> true
entity["name"] #=> "Mike"
entity.properties["name"] #=> "Mike"

# Check for a property that does not exist
entity.properties.exist? "age" #=> false
entity["age"] #=> nil
entity.properties["age"] #=> nil

# Deleting a property
entity.properties.exist? "email" #=> true
entity.properties.delete "email"
entity.properties.exist? "email" #=> false
entity.properties #=> {"name" => "Mike"}

Here is the my proposed API for this new property object:

  • PropertyStore#[] key
  • PropertyStore#[]= key, value
  • PropertyStore#read key
  • PropertyStore#write key, value
  • PropertyStore#exist? key
  • PropertyStore#fetch key, &block
  • PropertyStore#delete key, &block
  • PropertyStore#each

This object would also be responsible for normalizing keys to make sure they were always stored as strings but could be accessed as symbols.

entity[:name] = "Mike"
entity[:email] = "[email protected]"
entity.properties.exist? :name #=> true
entity.properties.exist? "name" #=> true
entity.properties #=> {"name" => "Mike", "email" => "[email protected]"}

In the future I can also see this object being responsible for dirty tracking changes, so we don't have to update every property when saving an entity, only the properties that changed. This would be API-compatible with ActiveModel's dirty tracking. Thoughts?

@jgeewax
Copy link

jgeewax commented Jan 27, 2015

👍 from me.

@jgeewax jgeewax added api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue. labels Feb 2, 2015
@jgeewax jgeewax added this to the Datastore Stable milestone Feb 2, 2015
@blowmage blowmage closed this as completed Feb 9, 2015
@blowmage
Copy link
Contributor Author

blowmage commented Feb 9, 2015

Please see #62.

quartzmo added a commit to quartzmo/google-cloud-ruby that referenced this issue Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants