Skip to content

add compressed=True support to TextProperty #277

@snarfed

Description

@snarfed

hi all! i recently hit an issue migrating an app from google.appengine.ext.ndb with a TextProperty(compressed=True), which isn't supported in google.cloud.ndb. (i'm on 0.2.0, on the Python 3 Standard runtime.)

i saw #160 and the fix in #181, particularly "we can still support compression transparently with bidirectional compatibility," and optimistically tried just removing compressed=True, but no luck. the value comes back as the raw zlib-compressed bytes, eg b'x\x9c\xed\\ks\xdb...'.

i ended up adding support myself with this custom property class:

class CompressedTextProperty(ndb.GenericProperty):
  def _to_base_type(self, value):
    if isinstance(value, str):
      return zlib.compress(value.encode())

  def _from_base_type(self, value):
    if isinstance(value, bytes):
      return zlib.decompress(value).decode()

it'd be nice if google.cloud.ndb supported this natively!

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions