Chore: Replace util.DynMap with structs (#36332)

* Chore: Replace util.DynMap

* Chore: added comments
This commit is contained in:
Hugo Häggmark
2021-07-01 10:23:33 +02:00
committed by GitHub
parent 4434eeaf59
commit 44c48ecebb
5 changed files with 38 additions and 10 deletions

View File

@@ -9,7 +9,6 @@ import (
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/util"
)
func (l *LibraryElementService) registerAPIEndpoints() {
@@ -31,7 +30,7 @@ func (l *LibraryElementService) createHandler(c *models.ReqContext, cmd CreateLi
return toLibraryElementError(err, "Failed to create library element")
}
return response.JSON(200, util.DynMap{"result": element})
return response.JSON(200, LibraryElementResponse{Result: element})
}
// deleteHandler handles DELETE /api/library-elements/:uid.
@@ -51,7 +50,7 @@ func (l *LibraryElementService) getHandler(c *models.ReqContext) response.Respon
return toLibraryElementError(err, "Failed to get library element")
}
return response.JSON(200, util.DynMap{"result": element})
return response.JSON(200, LibraryElementResponse{Result: element})
}
// getAllHandler handles GET /api/library-elements/.
@@ -71,7 +70,7 @@ func (l *LibraryElementService) getAllHandler(c *models.ReqContext) response.Res
return toLibraryElementError(err, "Failed to get library elements")
}
return response.JSON(200, util.DynMap{"result": elementsResult})
return response.JSON(200, LibraryElementSearchResponse{Result: elementsResult})
}
// patchHandler handles PATCH /api/library-elements/:uid
@@ -81,7 +80,7 @@ func (l *LibraryElementService) patchHandler(c *models.ReqContext, cmd patchLibr
return toLibraryElementError(err, "Failed to update library element")
}
return response.JSON(200, util.DynMap{"result": element})
return response.JSON(200, LibraryElementResponse{Result: element})
}
// getConnectionsHandler handles GET /api/library-panels/:uid/connections/.
@@ -91,7 +90,7 @@ func (l *LibraryElementService) getConnectionsHandler(c *models.ReqContext) resp
return toLibraryElementError(err, "Failed to get connections")
}
return response.JSON(200, util.DynMap{"result": connections})
return response.JSON(200, LibraryElementConnectionsResponse{Result: connections})
}
// getByNameHandler handles GET /api/library-elements/name/:name/.
@@ -101,7 +100,7 @@ func (l *LibraryElementService) getByNameHandler(c *models.ReqContext) response.
return toLibraryElementError(err, "Failed to get library element")
}
return response.JSON(200, util.DynMap{"result": elements})
return response.JSON(200, LibraryElementArrayResponse{Result: elements})
}
func toLibraryElementError(err error, message string) response.Response {

View File

@@ -181,3 +181,23 @@ type searchLibraryElementsQuery struct {
excludeUID string
folderFilter string
}
// LibraryElementResponse is a response struct for LibraryElementDTO.
type LibraryElementResponse struct {
Result LibraryElementDTO `json:"result"`
}
// LibraryElementSearchResponse is a response struct for LibraryElementSearchResult.
type LibraryElementSearchResponse struct {
Result LibraryElementSearchResult `json:"result"`
}
// LibraryElementArrayResponse is a response struct for an array of LibraryElementDTO.
type LibraryElementArrayResponse struct {
Result []LibraryElementDTO `json:"result"`
}
// LibraryElementConnectionsResponse is a response struct for an array of LibraryElementConnectionDTO.
type LibraryElementConnectionsResponse struct {
Result []LibraryElementConnectionDTO `json:"result"`
}