@@ -85,19 +85,47 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
85
85
logging .FrameworkTrace (ctx , "Called provider defined ListResource" )
86
86
87
87
if stream .Results == nil {
88
- // If the provider returned a nil results stream, we treat it as an empty stream.
88
+ // If the provider returned a nil results stream, we return an empty stream.
89
89
stream .Results = func (func (list.ListResult ) bool ) {}
90
90
}
91
91
92
- fwStream .Results = listResourceEventStreamAdapter ( stream .Results )
92
+ fwStream .Results = processListResults ( req , stream .Results )
93
93
}
94
94
95
- func listResourceEventStreamAdapter (stream iter.Seq [list.ListResult ]) iter.Seq [ListResult ] {
96
- // TODO: is this any more efficient than a for-range?
95
+ func processListResults (req list.ListRequest , stream iter.Seq [list.ListResult ]) iter.Seq [ListResult ] {
97
96
return func (yieldFw func (ListResult ) bool ) {
98
- yield := func (event list.ListResult ) bool {
99
- return yieldFw (ListResult (event ))
97
+ for result := range stream {
98
+ var fwResult ListResult
99
+
100
+ diags := validateListResult (req , result )
101
+ if diags .HasError () {
102
+ fwResult = ListResult {Diagnostics : diags }
103
+ } else {
104
+ fwResult = ListResult (result )
105
+ }
106
+ if ! yieldFw (fwResult ) {
107
+ return
108
+ }
100
109
}
101
- stream (yield )
102
110
}
103
111
}
112
+
113
+ func validateListResult (req list.ListRequest , result list.ListResult ) diag.Diagnostics {
114
+ var diags diag.Diagnostics
115
+
116
+ if result .Identity == nil {
117
+ diags .AddError (
118
+ "Incomplete List Result" ,
119
+ "ListResult.Identity is nil." ,
120
+ )
121
+ }
122
+
123
+ if req .IncludeResource && result .Resource == nil {
124
+ diags .AddWarning (
125
+ "Incomplete List Result" ,
126
+ "ListRequest.IncludeResource is true and ListResult.Resource is nil." ,
127
+ )
128
+ }
129
+
130
+ return diags
131
+ }
0 commit comments