Skip to content

Allow set plugin can now set entry fields to types other than str#2344

Merged
gazpachoking merged 3 commits intodevelopfrom
native_set
Feb 21, 2019
Merged

Allow set plugin can now set entry fields to types other than str#2344
gazpachoking merged 3 commits intodevelopfrom
native_set

Conversation

@gazpachoking
Copy link
Copy Markdown
Member

@gazpachoking gazpachoking commented Feb 19, 2019

Motivation for changes:

If you want to manipulate a number field it is not currently possible.

Detailed changes:

  • If set plugin templates evaluate to a type other than string, the type is not cast to a string.

Addressed issues:

Config usage if relevant (new plugin or updated schema):

set:
  some_field: {{some_int_field/2}}

To Do:

  • Should a 'native' parameter be added to the Entry.render method rather than constructing the NativeTemplate in set plugin?

@gazpachoking
Copy link
Copy Markdown
Member Author

So, I'm generally a fan of this 'native' rendering for the way we are using jinja templates. I think it allows more user manipulation of entry fields with the only cost being quotes sometimes needed if you explicitly need a string. @paranoidi @liiight @cvium Anything I'm overlooking?

for field in config:
# If this doesn't appear to be a jinja template, just set it right away.
if not isinstance(config[field], basestring) or '{' not in config[field]:
if not isinstance(config[field], str) or '{' not in config[field]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this remove unicode support? Why swtich from basestring -> str, we are not yet python3? :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a redundant import actually. Because of from builtins import *, str is equivalent to basestring.

Source: https://python-future.org/compatible_idioms.html#basestring

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, 'str' is equivalent to 'unicode' on python 2 (because of the from builtins import *.) We don't actually allow byte strings in any of these places.

Copy link
Copy Markdown
Member

@liiight liiight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @gazpachoking. Maybe add native types specific tests?

@gazpachoking
Copy link
Copy Markdown
Member Author

@liiight Yeah, good plan, added.

@gazpachoking gazpachoking merged commit 27928b0 into develop Feb 21, 2019
@gazpachoking gazpachoking deleted the native_set branch March 26, 2019 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants