Referencing Hash Values from Keys

4 messages Options
Embed this post
Permalink
Shelley, Ryan

Referencing Hash Values from Keys

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)

I’m new to Freemarker, but have used other templating languages in the past.  Currently, I’m attempting to use FMPP to generate configuration files, but having a bit of difficulty referencing hash values from keys.  Here is my TDD and sample template:

 

TDD:

 

{

      pools: [

            {

                  poolName: Foo

                  poolConnections: ServiceA

            },

            {

                  poolName: Bar

                  poolConnections: ServiceB

            }

      ]

}

 

Template:

 

This will output fine:

<#list pools as pool>

      <#assign keys = pool?keys >

      <#list keys as key>

            ${key} = ${pool[key]}

      </#list>

</#list>

 

This will get an error on poolConnections:

<#list pools as pool>

      ${pool.poolName}

      ${pool.poolConnections}

</#list>

 

In the first list iteration, everything is output correctly, but I have no control over the keys.  However, the second iteration fails on “${pool.poolConnections}” with the error:

 

Caused by: freemarker.core.InvalidReferenceException: Expression pool.poolConnections is undefined (notice the error is not on pool.poolName)

 

If I take “${pool.poolConnections}” out, it works fine (I see the value for poolName), but I kind of need to be able to access “poolConnections”!

 

I’ve tried this with different combinations of “${pool[poolConnections]}” and “${pool[‘poolConnections’]}” and “${pool.get(poolConnections)}” but none seem to be correct.  I’ve been back and forth through the documentation and the examples, but none seem to cover accessing a hash value by it’s key directly.

 

Suggestions?  Thanks!

 

-Ryan


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
fmpp-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fmpp-open
Daniel Dekany

Re: Referencing Hash Values from Keys

Reply Threaded More More options
Print post
Permalink
Tuesday, October 27, 2009, 5:45:11 AM, Shelley, Ryan wrote:

> I’m new to Freemarker, but have used other templating languages in
> the past.  Currently, I’m attempting to use FMPP to generate
> configuration files, but having a bit of difficulty referencing hash
> values from keys.  Here is my TDD and sample template:
>  
> TDD:
>  
> {
>       pools: [
>             {
>                   poolName: Foo
>                   poolConnections: ServiceA
>             },
>             {
>                   poolName: Bar
>                   poolConnections: ServiceB
>             }
>       ]
> }
>  
> Template:
>  
> This will output fine:
> <#list pools as pool>
>       <#assign keys = pool?keys >
>       <#list keys as key>
>             ${key} = ${pool[key]}
>       </#list>
> </#list>
>  
> This will get an error on poolConnections:
> <#list pools as pool>
>       ${pool.poolName}
>       ${pool.poolConnections}
> </#list>
>  
> In the first list iteration, everything is output correctly, but I
> have no control over the keys.  However, the second iteration fails
> on “${pool.poolConnections}” with the error:
>  
> Caused by: freemarker.core.InvalidReferenceException: Expression
> pool.poolConnections is undefined (notice the error is not on pool.poolName)
>
> If I take “${pool.poolConnections}” out, it works fine (I see the
> value for poolName), but I kind of need to be able to access “poolConnections”!
>  
> I’ve tried this with different combinations of

Don't... it should work the way you tried to do it:
pool.poolConnections

I copy-pasted your above snippets into an FMPP project and it works
fine here. Even if you use different versions than me, I don't know
about any bugs in FreeMarker or FMPP bugs that would spoil a such
basic operation. Are you absolutely sure you don't have a typo
somewhere, and that you look at the files that your project actually
uses?

> “${pool[poolConnections]}” and “${pool[‘poolConnections’]}” and
> “${pool.get(poolConnections)}” but none seem to be correct.  I’ve
> been back and forth through the documentation and the examples, but
> none seem to cover accessing a hash value by it’s key directly.

FreeMarker documentation does. It's myMap.myKey or
myMap[WhateverExpressionThatEvaluatesToAString] (like myMap["myKey"]
or myMap[aVariable], myMap["foo_" + i], etc.).

> Suggestions?  Thanks!
>  
> -Ryan

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
fmpp-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fmpp-open
Shelley, Ryan

Re: Referencing Hash Values from Keys

Reply Threaded More More options
Print post
Permalink
Well, I think I'm just not familiar with FM yet, and I'm running this as
a Maven FMPP Ant Task, so what it seems was happening was that it was
pulling in a different config file than I thought, and that config file
didn't have a "poolConnections" value in one of the "pools", and since
FM isn't as lenient with missing keys it was throwing an error.  Looks
like I need to add some existence checking prior to accessing the hash
value.  Sorry for the false alarm.  I'm going to keep at it, but I think
it was just user-error.  Thanks for checking!

-Ryan

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Daniel
Dekany
Sent: Tuesday, October 27, 2009 1:48 AM
To: FMPP open discussion
Subject: Re: [FMPP] Referencing Hash Values from Keys

Tuesday, October 27, 2009, 5:45:11 AM, Shelley, Ryan wrote:

> I'm new to Freemarker, but have used other templating languages in
> the past.  Currently, I'm attempting to use FMPP to generate
> configuration files, but having a bit of difficulty referencing hash
> values from keys.  Here is my TDD and sample template:
>  
> TDD:
>  
> {
>       pools: [
>             {
>                   poolName: Foo
>                   poolConnections: ServiceA
>             },
>             {
>                   poolName: Bar
>                   poolConnections: ServiceB
>             }
>       ]
> }
>  
> Template:
>  
> This will output fine:
> <#list pools as pool>
>       <#assign keys = pool?keys >
>       <#list keys as key>
>             ${key} = ${pool[key]}
>       </#list>
> </#list>
>  
> This will get an error on poolConnections:
> <#list pools as pool>
>       ${pool.poolName}
>       ${pool.poolConnections}
> </#list>
>  
> In the first list iteration, everything is output correctly, but I
> have no control over the keys.  However, the second iteration fails
> on "${pool.poolConnections}" with the error:
>  
> Caused by: freemarker.core.InvalidReferenceException: Expression
> pool.poolConnections is undefined (notice the error is not on
pool.poolName)
>
> If I take "${pool.poolConnections}" out, it works fine (I see the
> value for poolName), but I kind of need to be able to access
"poolConnections"!
>  
> I've tried this with different combinations of

Don't... it should work the way you tried to do it:
pool.poolConnections

I copy-pasted your above snippets into an FMPP project and it works
fine here. Even if you use different versions than me, I don't know
about any bugs in FreeMarker or FMPP bugs that would spoil a such
basic operation. Are you absolutely sure you don't have a typo
somewhere, and that you look at the files that your project actually
uses?

> "${pool[poolConnections]}" and "${pool['poolConnections']}" and
> "${pool.get(poolConnections)}" but none seem to be correct.  I've
> been back and forth through the documentation and the examples, but
> none seem to cover accessing a hash value by it's key directly.

FreeMarker documentation does. It's myMap.myKey or
myMap[WhateverExpressionThatEvaluatesToAString] (like myMap["myKey"]
or myMap[aVariable], myMap["foo_" + i], etc.).

> Suggestions?  Thanks!
>  
> -Ryan

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------
------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and
stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
fmpp-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fmpp-open

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
fmpp-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fmpp-open
Daniel Dekany

Re: Referencing Hash Values from Keys

Reply Threaded More More options
Print post
Permalink
Tuesday, October 27, 2009, 6:15:48 PM, Shelley, Ryan wrote:

> Well, I think I'm just not familiar with FM yet, and I'm running this as
> a Maven FMPP Ant Task, so what it seems was happening was that it was
> pulling in a different config file than I thought, and that config file
> didn't have a "poolConnections" value in one of the "pools", and since
> FM isn't as lenient with missing keys it was throwing an error.  Looks
> like I need to add some existence checking prior to accessing the hash
> value.  Sorry for the false alarm.  I'm going to keep at it, but I think
> it was just user-error.  Thanks for checking!

FreeMarker wants you to be explicit about optional stuff. (Like in
this case it certainly helped you to find a mistake... you pick up the
wrong config file or something like that.) If you know a variable is
optional (i.e, *validly* missing or null), please use missing value
handler operators:
http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing

--
Best regards,
 Daniel Dekany

> -Ryan
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Daniel
> Dekany
> Sent: Tuesday, October 27, 2009 1:48 AM
> To: FMPP open discussion
> Subject: Re: [FMPP] Referencing Hash Values from Keys
>
> Tuesday, October 27, 2009, 5:45:11 AM, Shelley, Ryan wrote:
>
>> I'm new to Freemarker, but have used other templating languages in
>> the past.  Currently, I'm attempting to use FMPP to generate
>> configuration files, but having a bit of difficulty referencing hash
>> values from keys.  Here is my TDD and sample template:
>>  
>> TDD:
>>  
>> {
>>       pools: [
>>             {
>>                   poolName: Foo
>>                   poolConnections: ServiceA
>>             },
>>             {
>>                   poolName: Bar
>>                   poolConnections: ServiceB
>>             }
>>       ]
>> }
>>  
>> Template:
>>  
>> This will output fine:
>> <#list pools as pool>
>>       <#assign keys = pool?keys >
>>       <#list keys as key>
>>             ${key} = ${pool[key]}
>>       </#list>
>> </#list>
>>  
>> This will get an error on poolConnections:
>> <#list pools as pool>
>>       ${pool.poolName}
>>       ${pool.poolConnections}
>> </#list>
>>  
>> In the first list iteration, everything is output correctly, but I
>> have no control over the keys.  However, the second iteration fails
>> on "${pool.poolConnections}" with the error:
>>  
>> Caused by: freemarker.core.InvalidReferenceException: Expression
>> pool.poolConnections is undefined (notice the error is not on
> pool.poolName)
>>
>> If I take "${pool.poolConnections}" out, it works fine (I see the
>> value for poolName), but I kind of need to be able to access
> "poolConnections"!
>>  
>> I've tried this with different combinations of
>
> Don't... it should work the way you tried to do it:
> pool.poolConnections
>
> I copy-pasted your above snippets into an FMPP project and it works
> fine here. Even if you use different versions than me, I don't know
> about any bugs in FreeMarker or FMPP bugs that would spoil a such
> basic operation. Are you absolutely sure you don't have a typo
> somewhere, and that you look at the files that your project actually
> uses?
>
>> "${pool[poolConnections]}" and "${pool['poolConnections']}" and
>> "${pool.get(poolConnections)}" but none seem to be correct.  I've
>> been back and forth through the documentation and the examples, but
>> none seem to cover accessing a hash value by it's key directly.
>
> FreeMarker documentation does. It's myMap.myKey or
> myMap[WhateverExpressionThatEvaluatesToAString] (like myMap["myKey"]
> or myMap[aVariable], myMap["foo_" + i], etc.).
>
>> Suggestions?  Thanks!
>>  
>> -Ryan
>



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
fmpp-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fmpp-open