How to add to a structural set in CFPROPERTY?

I use <cfproperty /> to use implicit getters and setters in ColdFusion (Railo).

However, for more complex values ​​such as structs and arrays, how can I add to them?

 <cfproperty name="settings" type="struct" /> 

How to add an element to a property called settings? If I do the following:

 <cfset setSettings(structAppend(getSettings(), { "hello" = "world" })) /> 

I get the following error:

java.lang.NullPointerException

Am I missing something? I am new to the cfproperty tag and thought it would be a time saver, but I cannot figure it out.

Also, as a bonus, how would I set a default value for these complex data types?

Thanks Mikey

+4
source share
1 answer

The couple is here ...

 <cfset setSettings(structAppend(getSettings(), { "hello" = "world" })) /> 

Settings is a struct , but structAppend() returns a boolean value. Add your structure to this line. Secondly, structs are always passed by reference, that is, if you do getSettings() , you get a struct in which you can make changes. Another call to getSettings() returns the same struct with the updated settings.

All you need is:

 <cfset structAppend(getSettings(), { "hello" = "world" }) /> 

Last thing. You can get a null pointer exception because getSettings() starts to be uninitialized. In your cfc in the constructor area (after your properties) you should set the initial struct settings, for example:

 <cfset setSettings({}) /> 
+5
source

Source: https://habr.com/ru/post/1479982/


All Articles