Get HashMap key that has an unknown suffix

Last modified by Fawad Ali on 2021/03/18 11:28

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

Fawad Ali

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

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}}
Tags:
     

Get Connected