@@ -181,40 +181,49 @@ func ReplaceIndexInDashboardObject(index string, content []byte) []byte {
181181 line , err := r .ReadBytes ('\n' )
182182 if err != nil {
183183 if err == io .EOF {
184- return result
184+ return append ( result , replaceInNDJSON ( logger , index , line ) ... )
185185 }
186186 logger .Error ("Error reading bytes from raw dashboard object: %+v" , err )
187187 return content
188188 }
189- objectMap := make (map [string ]interface {}, 0 )
190- err = json .Unmarshal (line , & objectMap )
191- if err != nil {
192- result = append (result , append (line , newline ... )... )
193- continue
194- }
189+ result = append (result , replaceInNDJSON (logger , index , line )... )
190+ }
191+ }
195192
196- attributes , ok := objectMap ["attributes" ].(map [string ]interface {})
197- if ! ok {
198- result = append (result , append (line , newline ... )... )
199- continue
200- }
193+ func replaceInNDJSON (logger * logp.Logger , index string , line []byte ) []byte {
194+ if len (line ) == 0 {
195+ return line
196+ }
201197
202- if kibanaSavedObject , ok := attributes ["kibanaSavedObjectMeta" ].(map [string ]interface {}); ok {
203- attributes ["kibanaSavedObjectMeta" ] = ReplaceIndexInSavedObject (logger , index , kibanaSavedObject )
204- }
198+ objectMap := make (map [string ]interface {}, 0 )
199+ err := json .Unmarshal (line , & objectMap )
200+ if err != nil {
201+ logger .Errorf ("Failed to convert bytes to map[string]interface: %+v" , err )
202+ return line
203+ }
205204
206- if visState , ok := attributes ["visState" ].(string ); ok {
207- attributes ["visState" ] = ReplaceIndexInVisState (logger , index , visState )
208- }
205+ attributes , ok := objectMap ["attributes" ].(map [string ]interface {})
206+ if ! ok {
207+ logger .Errorf ("Object does not have attributes key" )
208+ return line
209+ }
209210
210- b , err := json .Marshal (objectMap )
211- if err != nil {
212- logger .Error ("Error marshaling modified dashboard: %+v" , err )
213- result = append (result , append (line , newline ... )... )
214- }
211+ if kibanaSavedObject , ok := attributes ["kibanaSavedObjectMeta" ].(map [string ]interface {}); ok {
212+ attributes ["kibanaSavedObjectMeta" ] = ReplaceIndexInSavedObject (logger , index , kibanaSavedObject )
213+ }
215214
216- result = append (result , append (b , newline ... )... )
215+ if visState , ok := attributes ["visState" ].(string ); ok {
216+ attributes ["visState" ] = ReplaceIndexInVisState (logger , index , visState )
217217 }
218+
219+ b , err := json .Marshal (objectMap )
220+ if err != nil {
221+ logger .Error ("Error marshaling modified dashboard: %+v" , err )
222+ return line
223+ }
224+
225+ return append (b , newline ... )
226+
218227}
219228
220229// ReplaceStringInDashboard replaces a string field in a dashboard
0 commit comments