Skip to content

Commit e3be96c

Browse files
committed
Fix return formatter option binding
1 parent 4038dc2 commit e3be96c

4 files changed

Lines changed: 58 additions & 28 deletions

File tree

models/Query/Formatters/StructFormatter.cfc

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,7 @@ component accessors="true" {
88
}
99

1010
public function toFormatter( struct options = {} ) {
11-
var formatterOptions = arguments.options;
12-
return function( q ) {
13-
if (
14-
!formatterOptions.keyExists( "columnKey" ) || isNull( formatterOptions.columnKey ) || !len(
15-
formatterOptions.columnKey
16-
)
17-
) {
18-
throw(
19-
type = "MissingColumnKey",
20-
message = "A columnKey option is required for the [struct] return formatter."
21-
);
22-
}
23-
24-
return variables.utils.queryToStructOfStructs( q, formatterOptions.columnKey );
25-
};
11+
return new qb.models.Query.Formatters.StructReturnFormatter( variables.utils, arguments.options );
2612
}
2713

2814
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
component accessors="true" {
2+
3+
property name="utils";
4+
property name="options";
5+
6+
public StructReturnFormatter function init( any utils = new qb.models.Query.QueryUtils(), struct options = {} ) {
7+
variables.utils = arguments.utils;
8+
variables.options = arguments.options;
9+
return this;
10+
}
11+
12+
public struct function format( required any q ) {
13+
if (
14+
!variables.options.keyExists( "columnKey" ) || isNull( variables.options.columnKey ) || !len(
15+
variables.options.columnKey
16+
)
17+
) {
18+
throw(
19+
type = "MissingColumnKey",
20+
message = "A columnKey option is required for the [struct] return formatter."
21+
);
22+
}
23+
24+
return variables.utils.queryToStructOfStructs( arguments.q, variables.options.columnKey );
25+
}
26+
27+
}

models/Query/QueryBuilder.cfc

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4337,18 +4337,35 @@ component displayname="QueryBuilder" accessors="true" {
43374337
}
43384338

43394339
if ( isQuery( q ) ) {
4340-
return returnFormat( q );
4340+
return applyReturnFormat( q );
43414341
}
43424342

43434343
if ( isArray( q ) ) {
4344-
return returnFormat( q );
4344+
return applyReturnFormat( q );
43454345
}
43464346

43474347
if ( !q.keyExists( "result" ) || !q.keyExists( "query" ) ) {
4348-
return returnFormat( q );
4348+
return applyReturnFormat( q );
43494349
}
43504350

4351-
return { result: q.result, query: returnFormat( q.query ) };
4351+
return { result: q.result, query: applyReturnFormat( q.query ) };
4352+
}
4353+
4354+
private any function applyReturnFormat( required any q ) {
4355+
var formatter = getReturnFormat();
4356+
4357+
if ( isClosure( formatter ) || isCustomFunction( formatter ) ) {
4358+
return formatter( arguments.q );
4359+
}
4360+
4361+
if ( structKeyExists( formatter, "format" ) ) {
4362+
return formatter.format( arguments.q );
4363+
}
4364+
4365+
throw(
4366+
type = "InvalidFormat",
4367+
message = "The configured return formatter must be a closure or a component with a format method."
4368+
);
43524369
}
43534370

43544371
/**

models/Query/ReturnFormatterRegistry.cfc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ component accessors="true" singleton {
6767
var formatterOptions = duplicate( definition.options );
6868
structAppend( formatterOptions, arguments.options, true );
6969

70-
return resolveFactory( definition.factory, definition.properties )( formatterOptions );
70+
var factory = resolveFactory( definition.factory, definition.properties );
71+
72+
if ( isClosure( factory ) || isCustomFunction( factory ) ) {
73+
return factory( formatterOptions );
74+
}
75+
76+
return factory.toFormatter( formatterOptions );
7177
}
7278

7379
public boolean function hasReturnFormatter( required string name ) {
@@ -140,13 +146,10 @@ component accessors="true" singleton {
140146
);
141147
}
142148

143-
var wireboxFactory = variables.wirebox.getInstance(
149+
return variables.wirebox.getInstance(
144150
name = arguments.factory,
145151
initArguments = { "properties": arguments.properties }
146152
);
147-
return function( options ) {
148-
return wireboxFactory.toFormatter( arguments.options );
149-
};
150153
}
151154

152155
if ( !structKeyExists( arguments.factory, "toFormatter" ) ) {
@@ -156,10 +159,7 @@ component accessors="true" singleton {
156159
);
157160
}
158161

159-
var componentFactory = arguments.factory;
160-
return function( options ) {
161-
return componentFactory.toFormatter( arguments.options );
162-
};
162+
return arguments.factory;
163163
}
164164

165165
}

0 commit comments

Comments
 (0)