Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for numeric keys in map literal #1152

Merged
merged 3 commits into from
Mar 22, 2024

Conversation

ccutrer
Copy link
Contributor

@ccutrer ccutrer commented Jan 30, 2024

This supercedes #934 (rebased, and then addresses the review comment), and fixes #1090. Note that I verified Python Jinja accepts numbers as dict keys:

>>> from jinja2 import Environment
>>> env = Environment()
>>> expr = env.compile_expression("{0:'test'}[0]")
>>> expr()
'test'

chrisirhc and others added 2 commits January 30, 2024 14:35
This is a hack to support numeric keys declared in map literals.
Most of such usages typically use the keys as if they were strings, so this
hack is likely safe.

A hack is used as opposed to overhauling all maps used since full support would
require overhauling all maps types to `Map<Object, Object>` which seems
overkill.
@apella12
Copy link

apella12 commented Feb 8, 2024

I'd add that merging this PR would help with global warming.

  1. Smart thermostats save energy
  2. Home Assistant (a major smart home DIY application) configurations are used by java based smart home applications to control smart thermostats.
  3. A sample home assistant smart thermostat configuration uses three instances of numeric keys.
{
  "min_temp": 50,
  "max_temp": 85,
  "modes": [
    "off",
    "heat",
    "cool"
  ],
  "fan_modes": [
    "auto",
    "on"
  ],
  "action_topic": "zwave6/Thermostat_-_63/thermostat_operating_state/endpoint_0/state",
  "current_temperature_topic": "zwave6/Thermostat_-_63/sensor_multilevel/endpoint_0/Air_temperature",
  "current_temperature_template": "{{ value_json.value }}",
  "fan_mode_state_topic": "zwave6/Thermostat_-_63/thermostat_fan_mode/endpoint_0/mode",
  "fan_mode_command_topic": "zwave6/Thermostat_-_63/thermostat_fan_mode/endpoint_0/mode/set",
  "mode_state_topic": "zwave6/Thermostat_-_63/thermostat_mode/endpoint_0/mode",
  "mode_command_topic": "zwave6/Thermostat_-_63/thermostat_mode/endpoint_0/mode/set",
  "temperature_state_template": "{{ value_json.value }}",
  "temperature_command_topic": "zwave6/Thermostat_-_63/thermostat_setpoint/endpoint_0/setpoint/1/set",
  "mode_state_template": "{{ {0: \"off\", 1: \"heat\", 2: \"cool\"}[value_json.value] | default('off') }}",
  "temperature_state_topic": "zwave6/Thermostat_-_63/thermostat_setpoint/endpoint_0/setpoint/1",
  "action_template": "{{ {0: \"idle\", 1: \"heating\", 2: \"cooling\", 3: \"fan\"}[value_json.value] | default('idle') }}",
  "fan_mode_state_template": "{{ {0: \"auto\", 1: \"on\"}[value_json.value] | default('auto') }}",
  "temperature_unit": "F",
  "precision": 0.1,
  "device": {
    "identifiers": [
      "zwavejs2mqtt_0xf1c43721_node63"
    ],
    "manufacturer": "Radio Thermostat Company of America (RTC)",
    "model": "Z-Wave Thermostat (CT101)",
    "name": "Thermostat - 63",
    "sw_version": "9.0"
  },
  "availability": [
    {
      "payload_available": "true",
      "payload_not_available": "false",
      "topic": "zwave6/Thermostat_-_63/status",
      "value_template": "{{'true' if value_json.value else 'false'}}"
    },
    {
      "topic": "zwave6/_CLIENTS/ZWAVE_GATEWAY-zwave-js-ui/status",
      "value_template": "{{'online' if value_json.value else 'offline'}}"
    },
    {
      "payload_available": "true",
      "payload_not_available": "false",
      "topic": "zwave6/driver/status"
    }
  ],
  "availability_mode": "all",
  "name": "Thermostat - 63",
  "unique_id": "zwavejs2mqtt_0xf1c43721_Node63_thermostat"
}

@ccutrer
Copy link
Contributor Author

ccutrer commented Mar 21, 2024

Ping @samukce

Copy link
Contributor

@boulter boulter left a comment

Choose a reason for hiding this comment

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

one nit. Otherwise looks safe.

src/main/java/com/hubspot/jinjava/el/ext/AstDict.java Outdated Show resolved Hide resolved
@boulter boulter merged commit 61452e4 into HubSpot:master Mar 22, 2024
2 checks passed
@ccutrer ccutrer deleted the fix-numeric-key branch April 1, 2024 16:13
@ccutrer
Copy link
Contributor Author

ccutrer commented Apr 1, 2024

@boulter: any chance we could get a new release cut with this in it?

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.

Can Dict key be an integer?
4 participants