HubSpot / jinjava

Jinja template engine for Java
Apache License 2.0
690 stars 168 forks source link

add support for numeric keys in map literal #1152

Closed ccutrer closed 5 months ago

ccutrer commented 7 months ago

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'
apella12 commented 7 months ago

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 commented 5 months ago

Ping @samukce

ccutrer commented 5 months ago

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