equals not allow to ignore case.

Created on 18 July 2024, 5 months ago

Problem/Motivation

I have an import which uses equals.

  field_destination/value:
    plugin: if_condition
    source: source_value
    condition:
      plugin: equals
      value: 'yes'
    do_get: constants/true
    else_get: constants/false

The problem is that I do not know if source value is always going to be "yes" or it could be "Yes".

Proposed resolution

I have had an idea of using matches

  field_destination/value:
    plugin: if_condition
    source: source_value
    condition:
      plugin: matches
      value: /^yes$/i
    do_get: constants/true
    else_get: constants/false

I do feel this is a bit overkill for a simple case.

My other idea is to add an ignore_case flag to equals

  field_destination/value:
    plugin: if_condition
    source: source_value
    condition:
      plugin: equals
      value: 'yes'
      ignore_case: true
    do_get: constants/true
    else_get: constants/false

or another idea is to create a new condition plugin for bool which will check all common variations of true/false like true/false, Y/N, Yes/No, 1/0 etc.

  field_destination/value:
    plugin: if_condition
    source: source_value
    condition:
      plugin: bool
    do_get: constants/true
    else_get: constants/false

Any help will be appreciated.

πŸ’¬ Support request
Status

Needs review

Version

2.2

Component

Code

Created by

πŸ‡¦πŸ‡ΊAustralia gordon Melbourne

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @gordon
  • Status changed to Needs review 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    Hi @gordon,
    Under the hood, the equals condition is super simple and adding an ignore_case option wouldn't be especially elegant, unfortunately.

    If using Migrate Conditions, the best options are to use matches, as you proposed, or to use in_array like this:

      field_destination/value:
        plugin: if_condition
        source: source_value
        condition:
          plugin: in_array
          value:
            - yes
            - Yes
        do_get: constants/true
        else_get: constants/false
    

    Though I would recommend using the evaluate_condition process plugin since it looks like you are setting a boolean.

      field_destination/value:
        plugin: evaluate_condition
        source: source_value
        condition:
          plugin: in_array
          value:
            - yes
            - Yes
    

    or

      field_destination/value:
        plugin: evaluate_condition
        source: source_value
        condition:
          plugin: matches
          regex: /^yes$/i
    

    which could be simplified further by using parens notation to configure the condition:

      field_destination/value:
        plugin: evaluate_condition
        source: source_value
        condition: matches(/^yes$/i)
    

    If there's some quirk where you need to use if_condition, I'd also like to point out the relatively new do_default_value and else_default_value properties, which might help you avoid defining constants. See ✨ Add do_/else_default_value settings for if_condition Fixed .

    That all said, you could do this with the core static_map plugin as well. Something like

      field_destination/value:
        plugin: static_map
        source: source_value
        map:
          Yes: TRUE
          yes: TRUE
        default_value: FALSE
    
Production build 0.71.5 2024