user160820
user160820

Reputation: 15220

Creating Nested Structs, what will be a better way?

I am trying to create nested Structs something like

<cffunction name="setDataAllWithFilter" output="false" access="public">

    <cfargument name="stCollection" required="true" type="Struct" />

    <cfif NOT StructKeyExists( Session, this.LOCAL ) >
        <cfset Session[this.LOCAL] = StructNew() />
    </cfif>

    <cfif NOT StructKeyExists( Session[this.LOCAL], "Data" ) >
        <cfset Session[this.LOCAL]["Data"] = StructNew() />
    </cfif>

    <cfif NOT StructKeyExists( Session[this.LOCAL]["Data"], "Filtered" ) >
        <cfset Session[this.LOCAL]["Data"]["Filtered"]  = StructNew() />
    </cfif>

    <cfreturn SetAll( Arguments.stCollection, Session[this.LOCAL]["Data"]["Filtered"] ) />

</cffunction>

is it OK like this? or there is a better way to do so?

Thanks

Upvotes: 0

Views: 2018

Answers (3)

baynezy
baynezy

Reputation: 7066

What you are doing is fine. You can use cfparam to make your life easier though

<cffunction name="setDataAllWithFilter" output="false" access="public">

    <cfargument name="stCollection" required="true" type="Struct" />

    <cfparam name="session[this.local]" default="#structNew()#">
    <cfparam name="session[this.local].Data" default="#structNew()#">
    <cfparam name="session[this.local].Data.Filtered" default="#structNew()#">

    <cfreturn SetAll( Arguments.stCollection, Session[this.LOCAL]["Data"]["Filtered"] ) />

</cffunction>

Upvotes: 0

barnyr
barnyr

Reputation: 5678

You could look to use StructAppend() to set up your session structure:

<cfscript>
initStruct = {Data={Filtered={}}};
StructAppend(session[this.local],initStruct,false);
</cfscript>

Not I've not had time yet to test this here, so ymmv

Upvotes: 1

Tom Hubbard
Tom Hubbard

Reputation: 16139

The SetVariable function will create nested structures in order to satisfy the requirement.

 <cfset SetVariable("test1.test2.test3",4)>

Will create a variable test1["test2"]["Test3"] which equals 4.

Also look into StructGet which will allow you to create empty structs based on a path (string).

Upvotes: 1

Related Questions