question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rethink the missing/default field attributes

See original GitHub issue

Those attributes come from marshmallow, which is all about serialize/deserialize a document:

  • missing is used when the field is not present in the dict to deserialize
  • default is used when the field is not present in the document to serialize

However in umongo the focus shifts toward the document itself, so those terms seems a bit clunky

  • the missing object is already used inside umongo’s DataProxy to represent field not present in MongoDB. Hence the missing attribute should mean what value to return if the value is missing
  • the default attribute sounds like the value to set by default to the field when creating a new document

In a nutshell the meaning of missing and default are reversed between mashmallow and umongo…

What I’m thinking to do:

  • Remove the missing attribute (in fact just hide it from abstract.BaseField constructor and documentation)
  • Only use the default attribute for both missing and default (in marshmallow’s logic)
  • Add methods to get from the umongo document an equivalent pure mashmallow Schema (see #34 )

The idea is to hide mashmallow logic to expose a more consistent API from the umongo user’s point of view. Then provide a way to get back a pure Marshmallow Schema when needed to do all the custom.

example:

@instance.register
class Person(Document):
    name = fields.StrField(default='John Doe')

p = Person()
# Default is set
assert p.name == 'John Doe'
# Default value will be written in database
assert p._data.get('name') == 'John Doe'
# If we want more cunning behavior (e.g. only save in database non-default value)
# we should use a `MethodField` to define custom method for serialization/deseriazation

del p.name
assert p._data.get('name') == missing
# If not present on database, we switch back to default value as well
assert p.name == 'John Doe'

# Now it's customization time !
PersonSchema = p.get_marshmallow_schema()
class MyCustomSchema(PersonSchema):
    ...

# It could be also useful to provide method to get a specific field
class MyCustomSchema2(marshmallow.Schema)
    name = p.get_marshmallow_field('name')

@lafrech What do you think ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, May 10, 2017

I’ve been following the discussions about Marshmallow 3 and from what I gathered, the choices made are likely to make our lives easier.

Among the breaking changes, strict would be True by default or even removed and we’d need to catch ValidationError, and load would return the deserialized data rather than the (data, errors) tuple. This will break stuff, but ultimately, the code should be easier to read.

1reaction
touilleMancommented, May 5, 2017

I think we should keep thing explicit to avoid misleading use of missing/default. For example we only use default un umongo, raise a runtime error if missing is used (to explain how umongo and marshmallow have their behavior changing on this point) and use a marshmallow_missing/marshmallow_default fields to pass a custom attribute value when using as_marshmallow_field. Of course if marshamallow_* is not provided, we fall back on default value (serializing it on the fly before marshmallow use it)

I’ve created a PR which implement this behavior #107, can you have a look and play a bit with it (the subject is pretty complicated, better have two pair of eyes on it !)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReQL command: filter - RethinkDB
By default, filter will silently skip documents with missing fields: if the predicate tries to access a field that doesn't exist (for instance, ......
Read more >
Rethink Winter 2022 Release Notes - v3.21.8 (Sprint 8) – Buildout ...
Rethink Standard User Profile Now Includes "View All" Permission on Properties and Spaces/Units ... The Rethink Standard User profile, by default, will now ......
Read more >
Staff Profile Set Up - Rethink BH Self-Help - Confluence
From Staff Members, Select your staff of choice, and click on General Information from the menu on the left. Click Edit to complete...
Read more >
A Practical Introduction to RethinkDB - Pluralsight
This is the default behavior, but if the data is replicated to other nodes within the cluster, we can instruct the query that...
Read more >
You're overusing useMemo: Rethinking Hooks memoization
As long as page and type are the same — i.e., no prop changes — resolvedValue will ... Memoizing default state for any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found