Skip to content

Are Query instances supposed to be immutable? #3

@nick-herrera

Description

@nick-herrera

In the feature/v2 branch, the has_fields method et al. looks like:

def has_fields(self, *fields):
    self.terms.append(['$has_fields', fields])
    return self._clone()

def _clone(self):
    query = type(self)(self.schema)
    query.terms.extend(copy.deepcopy(self.terms))
    return query

has_fields() Reference
_clone() Reference

I'm assuming self isn't being returned directly because Query objects are intended to be immutable.

getAllQuery = montage.Query('movies');
hasFieldsQuery = getAllQuery.hasFields('field');
limitQuery = getAllQuery.limit(1);

Unless I'm missing something, getAllQuery, hasFieldsQuery, and limitQuery would have different references but their terms would overlap because terms.append() happens before the copy.deepcopy().

More specifically, getAllQuery would get the terms from the .hasFields() call and hasFieldsQuery and limitQuery would have identical terms.

  1. Are my observations above correct?
  2. Should Query instances be immutable? (so I can duplicate this behavior in the javascript-montage wrapper).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions