@@ -75,7 +75,7 @@ func (e *VastResource) doAfterRequest(ctx context.Context, response Renderable)
7575 if ! isDummyResource {
7676 // Pre-normalization: attach @resourceType so resource hooks and user AfterRequestFn
7777 // can rely on it for formatting/logging/branching even if later mutations change shape.
78- if err = SetResourceKey (response , resourceType ); err != nil {
78+ if err = setResourceKey (response , resourceType ); err != nil {
7979 return nil , err
8080 }
8181 }
@@ -103,7 +103,7 @@ func (e *VastResource) doAfterRequest(ctx context.Context, response Renderable)
103103 // Post-normalization: re-attach @resourceType, because mutations can produce new
104104 // Record/RecordSet instances which won't carry the earlier key.
105105 if ! isDummyResource {
106- if err = SetResourceKey (mutated , resourceType ); err != nil {
106+ if err = setResourceKey (mutated , resourceType ); err != nil {
107107 return nil , err
108108 }
109109 }
@@ -126,42 +126,8 @@ func defaultResponseMutations(response Renderable) (Renderable, error) {
126126 }
127127 return nil , fmt .Errorf ("expected map[string]any under 'async_task', got %T" , raw )
128128 }
129- // Normalize pagination envelope when response is a single Record
130- if rs , matched , err := unwrapPaginationEnvelopeFromRecord (typed ); matched {
131- if err != nil {
132- return nil , err
133- }
134- if rs != nil {
135- return rs , nil
136- }
137- }
138129 return response , nil
139130 case RecordSet :
140- // Normalize list responses that wrap data under a pagination envelope
141- // Expected keys: "results", "count", "next", "previous"
142- // Example payload returned as a single Record inside RecordSet: {"results": [...], "count": N, "next": ..., "previous": ...}
143- if len (typed ) == 1 {
144- // Accept both Record and raw map[string]any as the envelope
145- if rec , ok := any (typed [0 ]).(Record ); ok {
146- if rs , matched , err := unwrapPaginationEnvelopeFromRecord (rec ); matched {
147- if err != nil {
148- return nil , err
149- }
150- if rs != nil {
151- return rs , nil
152- }
153- }
154- } else if raw , ok := any (typed [0 ]).(map [string ]any ); ok {
155- if rs , matched , err := unwrapPaginationEnvelopeFromRecord (Record (raw )); matched {
156- if err != nil {
157- return nil , err
158- }
159- if rs != nil {
160- return rs , nil
161- }
162- }
163- }
164- }
165131 return typed , nil
166132 case EmptyRecord :
167133 // No op.
@@ -170,49 +136,6 @@ func defaultResponseMutations(response Renderable) (Renderable, error) {
170136 return nil , fmt .Errorf ("unsupported type %T for result" , response )
171137}
172138
173- // unwrapPaginationEnvelopeFromRecord attempts to detect and unwrap a standard pagination envelope
174- // of the form {"results": [...], "count": N, "next": ..., "previous": ...} into a RecordSet.
175- //
176- // Returns:
177- // - (RecordSet, true, nil) when envelope matched and conversion succeeded
178- // - (nil, true, nil) when envelope matched but results are of unsupported type
179- // - (nil, false, nil) when envelope did not match
180- // - (nil, true, err) when envelope matched but conversion failed
181- func unwrapPaginationEnvelopeFromRecord (rec Record ) (RecordSet , bool , error ) {
182- _ , hasResults := rec ["results" ]
183- _ , hasCount := rec ["count" ]
184- _ , hasNext := rec ["next" ]
185- _ , hasPrev := rec ["previous" ]
186- if ! (hasResults && hasCount && hasNext && hasPrev ) {
187- return nil , false , nil
188- }
189- inner := rec ["results" ]
190- // Prefer []map[string]any, but also handle []any of maps
191- if list , ok := inner .([]map [string ]any ); ok {
192- recordSet , err := ToRecordSet (list )
193- if err != nil {
194- return nil , true , err
195- }
196- return recordSet , true , nil
197- }
198- if anyList , ok := inner .([]any ); ok {
199- converted := make ([]map [string ]any , 0 , len (anyList ))
200- for _ , it := range anyList {
201- if m , ok := it .(map [string ]any ); ok {
202- converted = append (converted , m )
203- } else {
204- return nil , true , nil
205- }
206- }
207- recordSet , err := ToRecordSet (converted )
208- if err != nil {
209- return nil , true , err
210- }
211- return recordSet , true , nil
212- }
213- return nil , true , nil
214- }
215-
216139// ######################################################
217140//
218141// REQUEST/RESPONSE LOGGING
0 commit comments