From 44c48ecebbe016821e7da796701b47ad1381442c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Thu, 1 Jul 2021 10:23:33 +0200 Subject: [PATCH] Chore: Replace util.DynMap with structs (#36332) * Chore: Replace util.DynMap * Chore: added comments --- pkg/api/annotations.go | 2 +- pkg/services/annotations/annotations.go | 11 ++++++++++- pkg/services/libraryelements/api.go | 13 ++++++------- pkg/services/libraryelements/models.go | 20 ++++++++++++++++++++ pkg/services/sqlstore/annotation.go | 2 +- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index 84009e5a9de..cb3154e19dc 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -302,5 +302,5 @@ func GetAnnotationTags(c *models.ReqContext) response.Response { return response.Error(500, "Failed to find annotation tags", err) } - return response.JSON(200, util.DynMap{"result": result}) + return response.JSON(200, annotations.GetAnnotationTagsResponse{Result: result}) } diff --git a/pkg/services/annotations/annotations.go b/pkg/services/annotations/annotations.go index e008ba2009f..cc7f4192409 100644 --- a/pkg/services/annotations/annotations.go +++ b/pkg/services/annotations/annotations.go @@ -41,6 +41,7 @@ type ItemQuery struct { Limit int64 `json:"limit"` } +// TagsQuery is the query for a tags search. type TagsQuery struct { OrgID int64 `json:"orgId"` Tag string `json:"tag"` @@ -48,21 +49,29 @@ type TagsQuery struct { Limit int64 `json:"limit"` } -type Tags struct { +// Tag is the DB result of a tags search. +type Tag struct { Key string Value string Count int64 } +// TagsDTO is the frontend DTO for Tag. type TagsDTO struct { Tag string `json:"tag"` Count int64 `json:"count"` } +// FindTagsResult is the result of a tags search. type FindTagsResult struct { Tags []*TagsDTO `json:"tags"` } +// GetAnnotationTagsResponse is a response struct for FindTagsResult. +type GetAnnotationTagsResponse struct { + Result FindTagsResult `json:"result"` +} + type DeleteParams struct { OrgId int64 Id int64 diff --git a/pkg/services/libraryelements/api.go b/pkg/services/libraryelements/api.go index 0e4ed4f0b04..983ad3d6694 100644 --- a/pkg/services/libraryelements/api.go +++ b/pkg/services/libraryelements/api.go @@ -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 { diff --git a/pkg/services/libraryelements/models.go b/pkg/services/libraryelements/models.go index fb0670c3992..6cecb54991a 100644 --- a/pkg/services/libraryelements/models.go +++ b/pkg/services/libraryelements/models.go @@ -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"` +} diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index 52eb65468c0..fb49ccd068c 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -298,7 +298,7 @@ func (r *SQLAnnotationRepo) FindTags(query *annotations.TagsQuery) (annotations. sql.WriteString(` ORDER BY ` + tagKey + `,` + tagValue) sql.WriteString(` ` + dialect.Limit(query.Limit)) - var items []*annotations.Tags + var items []*annotations.Tag if err := x.SQL(sql.String(), params...).Find(&items); err != nil { return annotations.FindTagsResult{Tags: []*annotations.TagsDTO{}}, err }