cogGet full key from a HashMap which has multiple keys with unknown suffixes such as 'textarea_en', 'textarea__' in Solr response
Developed by

Fawad Ali

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

{{velocity}}

## Macro for getting full key from a short key (useful for keys with multiple unknown suffixes as in solr response)
#macro (getFullKey $valuesMap $shortKey)
 #foreach ($key in $valuesMap.keySet())
   ## Check each key if it starts with the given value
   #if ($key.startsWith($shortKey))
     #set ($fullKey = $key)
     #break
   #end
 #end
  $fullKey
#end

#set ($testValues = {
 'value_en': 'Hello',
 'value__': 'Hello',
 'value_test': 'Hello'
})

## This will get the first full key that starts with 'value'
#set ($fullKey = "#getFullKey($testValues 'value')")

## Now get the value at the full key
$testValues.get($fullKey.trim())

{{/velocity}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags:
Created by Fawad Ali on 2019/08/19 14:00
     

Get Connected