Skip to content

Commit bcbff9a

Browse files
authored
👻 Use JSON serializer everywhere (#680)
Extends the use of the JSON serializer to the rest of the models. Signed-off-by: Sam Lucidi <[email protected]>
1 parent 74c5419 commit bcbff9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+576
-608
lines changed

api/analysis.go

+32-49
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,9 @@ func (h AnalysisHandler) RuleReports(ctx *gin.Context) {
10601060
Name: m.Name,
10611061
}
10621062
resources = append(resources, r)
1063-
if m.Labels != nil {
1064-
_ = json.Unmarshal(m.Labels, &r.Labels)
1065-
}
1066-
if m.Links != nil {
1067-
_ = json.Unmarshal(m.Links, &r.Links)
1063+
r.Labels = m.Labels
1064+
for _, l := range m.Links {
1065+
r.Links = append(r.Links, Link(l))
10681066
}
10691067
r.Effort += m.Effort
10701068
}
@@ -1197,11 +1195,9 @@ func (h AnalysisHandler) AppIssueReports(ctx *gin.Context) {
11971195
ID: m.ID,
11981196
}
11991197
resources = append(resources, r)
1200-
if m.Labels != nil {
1201-
_ = json.Unmarshal(m.Labels, &r.Labels)
1202-
}
1203-
if m.Links != nil {
1204-
_ = json.Unmarshal(m.Links, &r.Links)
1198+
r.Labels = m.Labels
1199+
for _, l := range m.Links {
1200+
r.Links = append(r.Links, Link(l))
12051201
}
12061202
r.Effort += m.Effort
12071203
}
@@ -1722,13 +1718,9 @@ func (h AnalysisHandler) DepReports(ctx *gin.Context) {
17221718
Name: m.Name,
17231719
Applications: m.Applications,
17241720
}
1725-
if m.Labels != nil {
1726-
var aggregated []string
1727-
_ = json.Unmarshal(m.Labels, &aggregated)
1728-
for _, s := range aggregated {
1729-
if s != "" {
1730-
r.Labels = append(r.Labels, s)
1731-
}
1721+
for _, s := range m.Labels {
1722+
if s != "" {
1723+
r.Labels = append(r.Labels, s)
17321724
}
17331725
}
17341726
resources = append(resources, r)
@@ -2082,7 +2074,7 @@ func (h *AnalysisHandler) archive(ctx *gin.Context, q *gorm.DB) (err error) {
20822074
db = db.Where("n.IssueID = i.ID")
20832075
db = db.Where("i.AnalysisID", m.ID)
20842076
db = db.Group("i.ID")
2085-
summary := []ArchivedIssue{}
2077+
summary := []model.ArchivedIssue{}
20862078
err = db.Scan(&summary).Error
20872079
if err != nil {
20882080
return
@@ -2091,8 +2083,8 @@ func (h *AnalysisHandler) archive(ctx *gin.Context, q *gorm.DB) (err error) {
20912083
db = db.Model(m)
20922084
db = db.Omit(clause.Associations)
20932085
m.Archived = true
2094-
m.Summary, _ = json.Marshal(summary)
2095-
err = db.Updates(h.fields(&m)).Error
2086+
m.Summary = summary
2087+
err = db.Save(&m).Error
20962088
if err != nil {
20972089
return
20982090
}
@@ -2155,7 +2147,7 @@ type Issue struct {
21552147
Effort int `json:"effort,omitempty" yaml:",omitempty"`
21562148
Incidents []Incident `json:"incidents,omitempty" yaml:",omitempty"`
21572149
Links []Link `json:"links,omitempty" yaml:",omitempty"`
2158-
Facts FactMap `json:"facts,omitempty" yaml:",omitempty"`
2150+
Facts Map `json:"facts,omitempty" yaml:",omitempty"`
21592151
Labels []string `json:"labels"`
21602152
}
21612153

@@ -2176,15 +2168,11 @@ func (r *Issue) With(m *model.Issue) {
21762168
r.Incidents,
21772169
n)
21782170
}
2179-
if m.Links != nil {
2180-
_ = json.Unmarshal(m.Links, &r.Links)
2181-
}
2182-
if m.Facts != nil {
2183-
_ = json.Unmarshal(m.Facts, &r.Facts)
2184-
}
2185-
if m.Labels != nil {
2186-
_ = json.Unmarshal(m.Labels, &r.Labels)
2171+
for _, l := range m.Links {
2172+
r.Links = append(r.Links, Link(l))
21872173
}
2174+
r.Facts = m.Facts
2175+
r.Labels = m.Labels
21882176
r.Effort = m.Effort
21892177
}
21902178

@@ -2203,9 +2191,11 @@ func (r *Issue) Model() (m *model.Issue) {
22032191
m.Incidents,
22042192
*n)
22052193
}
2206-
m.Links, _ = json.Marshal(r.Links)
2207-
m.Facts, _ = json.Marshal(r.Facts)
2208-
m.Labels, _ = json.Marshal(r.Labels)
2194+
for _, l := range r.Links {
2195+
m.Links = append(m.Links, model.Link(l))
2196+
}
2197+
m.Facts = r.Facts
2198+
m.Labels = r.Labels
22092199
m.Effort = r.Effort
22102200
return
22112201
}
@@ -2231,9 +2221,7 @@ func (r *TechDependency) With(m *model.TechDependency) {
22312221
r.Version = m.Version
22322222
r.Indirect = m.Indirect
22332223
r.SHA = m.SHA
2234-
if m.Labels != nil {
2235-
_ = json.Unmarshal(m.Labels, &r.Labels)
2236-
}
2224+
r.Labels = m.Labels
22372225
}
22382226

22392227
// Model builds a model.
@@ -2244,20 +2232,20 @@ func (r *TechDependency) Model() (m *model.TechDependency) {
22442232
m.Version = r.Version
22452233
m.Provider = r.Provider
22462234
m.Indirect = r.Indirect
2247-
m.Labels, _ = json.Marshal(r.Labels)
2235+
m.Labels = r.Labels
22482236
m.SHA = r.SHA
22492237
return
22502238
}
22512239

22522240
// Incident REST resource.
22532241
type Incident struct {
22542242
Resource `yaml:",inline"`
2255-
Issue uint `json:"issue"`
2256-
File string `json:"file"`
2257-
Line int `json:"line"`
2258-
Message string `json:"message"`
2259-
CodeSnip string `json:"codeSnip" yaml:"codeSnip"`
2260-
Facts FactMap `json:"facts"`
2243+
Issue uint `json:"issue"`
2244+
File string `json:"file"`
2245+
Line int `json:"line"`
2246+
Message string `json:"message"`
2247+
CodeSnip string `json:"codeSnip" yaml:"codeSnip"`
2248+
Facts Map `json:"facts"`
22612249
}
22622250

22632251
// With updates the resource with the model.
@@ -2268,9 +2256,7 @@ func (r *Incident) With(m *model.Incident) {
22682256
r.Line = m.Line
22692257
r.Message = m.Message
22702258
r.CodeSnip = m.CodeSnip
2271-
if m.Facts != nil {
2272-
_ = json.Unmarshal(m.Facts, &r.Facts)
2273-
}
2259+
r.Facts = m.Facts
22742260
}
22752261

22762262
// Model builds a model.
@@ -2280,7 +2266,7 @@ func (r *Incident) Model() (m *model.Incident) {
22802266
m.Line = r.Line
22812267
m.Message = r.Message
22822268
m.CodeSnip = r.CodeSnip
2283-
m.Facts, _ = json.Marshal(r.Facts)
2269+
m.Facts = r.Facts
22842270
return
22852271
}
22862272

@@ -2371,9 +2357,6 @@ type DepAppReport struct {
23712357
} `json:"dependency"`
23722358
}
23732359

2374-
// FactMap map.
2375-
type FactMap map[string]any
2376-
23772360
// IssueWriter used to create a file containing issues.
23782361
type IssueWriter struct {
23792362
encoder

api/application.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (h ApplicationHandler) Update(ctx *gin.Context) {
362362
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
363363
db = h.DB(ctx).Model(m)
364364
db = db.Omit(clause.Associations, "BucketID")
365-
result = db.Updates(h.fields(m))
365+
result = db.Save(m)
366366
if result.Error != nil {
367367
_ = ctx.Error(result.Error)
368368
return
@@ -722,12 +722,10 @@ func (h ApplicationHandler) FactList(ctx *gin.Context, key FactKey) {
722722
return
723723
}
724724

725-
facts := FactMap{}
725+
facts := Map{}
726726
for i := range list {
727727
fact := &list[i]
728-
var v any
729-
_ = json.Unmarshal(fact.Value, &v)
730-
facts[fact.Key] = v
728+
facts[fact.Key] = fact.Value
731729
}
732730
h.Respond(ctx, http.StatusOK, facts)
733731
}
@@ -772,9 +770,7 @@ func (h ApplicationHandler) FactGet(ctx *gin.Context) {
772770
return
773771
}
774772

775-
var v any
776-
_ = json.Unmarshal(list[0].Value, &v)
777-
h.Respond(ctx, http.StatusOK, v)
773+
h.Respond(ctx, http.StatusOK, list[0].Value)
778774
}
779775

780776
// FactCreate godoc
@@ -846,12 +842,11 @@ func (h ApplicationHandler) FactPut(ctx *gin.Context) {
846842
return
847843
}
848844

849-
value, _ := json.Marshal(f.Value)
850845
m := &model.Fact{
851846
Key: key.Name(),
852847
Source: key.Source(),
853848
ApplicationID: id,
854-
Value: value,
849+
Value: f.Value,
855850
}
856851
db := h.DB(ctx)
857852
result = db.Save(m)
@@ -906,7 +901,7 @@ func (h ApplicationHandler) FactDelete(ctx *gin.Context) {
906901
// @param factmap body api.FactMap true "Fact map"
907902
func (h ApplicationHandler) FactReplace(ctx *gin.Context, key FactKey) {
908903
id := h.pk(ctx)
909-
facts := FactMap{}
904+
facts := Map{}
910905
err := h.Bind(ctx, &facts)
911906
if err != nil {
912907
_ = ctx.Error(err)
@@ -1145,7 +1140,10 @@ func (r *Application) With(m *model.Application, tags []model.ApplicationTag) {
11451140
r.Bucket = r.refPtr(m.BucketID, m.Bucket)
11461141
r.Comments = m.Comments
11471142
r.Binary = m.Binary
1148-
_ = json.Unmarshal(m.Repository, &r.Repository)
1143+
if m.Repository != (model.Repository{}) {
1144+
repo := Repository(m.Repository)
1145+
r.Repository = &repo
1146+
}
11491147
if m.Review != nil {
11501148
ref := &Ref{}
11511149
ref.With(m.Review.ID, "")
@@ -1246,7 +1244,7 @@ func (r *Application) Model() (m *model.Application) {
12461244
}
12471245
m.ID = r.ID
12481246
if r.Repository != nil {
1249-
m.Repository, _ = json.Marshal(r.Repository)
1247+
m.Repository = model.Repository(*r.Repository)
12501248
}
12511249
if r.BusinessService != nil {
12521250
m.BusinessServiceID = &r.BusinessService.ID
@@ -1307,14 +1305,14 @@ type Fact struct {
13071305
func (r *Fact) With(m *model.Fact) {
13081306
r.Key = m.Key
13091307
r.Source = m.Source
1310-
_ = json.Unmarshal(m.Value, &r.Value)
1308+
r.Value = m.Value
13111309
}
13121310

13131311
func (r *Fact) Model() (m *model.Fact) {
13141312
m = &model.Fact{}
13151313
m.Key = r.Key
13161314
m.Source = r.Source
1317-
m.Value, _ = json.Marshal(r.Value)
1315+
m.Value = r.Value
13181316
return
13191317
}
13201318

api/archetype.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (h ArchetypeHandler) Update(ctx *gin.Context) {
232232
m.UpdateUser = h.CurrentUser(ctx)
233233
db := h.DB(ctx).Model(m)
234234
db = db.Omit(clause.Associations)
235-
result := db.Updates(h.fields(m))
235+
result := db.Save(m)
236236
if result.Error != nil {
237237
_ = ctx.Error(result.Error)
238238
return

api/assessment.go

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"encoding/json"
54
"net/http"
65

76
"github.com/gin-gonic/gin"
@@ -126,7 +125,7 @@ func (h AssessmentHandler) Update(ctx *gin.Context) {
126125
m.UpdateUser = h.CurrentUser(ctx)
127126
db := h.DB(ctx).Model(m)
128127
db = db.Omit(clause.Associations, "Thresholds", "RiskMessages")
129-
result := db.Updates(h.fields(m))
128+
result := db.Save(m)
130129
if result.Error != nil {
131130
_ = ctx.Error(result.Error)
132131
return
@@ -148,21 +147,25 @@ func (h AssessmentHandler) Update(ctx *gin.Context) {
148147
// Assessment REST resource.
149148
type Assessment struct {
150149
Resource `yaml:",inline"`
151-
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
152-
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
153-
Questionnaire Ref `json:"questionnaire" binding:"required"`
154-
Sections []assessment.Section `json:"sections" binding:"dive"`
155-
Stakeholders []Ref `json:"stakeholders"`
156-
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
150+
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
151+
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
152+
Questionnaire Ref `json:"questionnaire" binding:"required"`
153+
Sections []Section `json:"sections" binding:"dive"`
154+
Stakeholders []Ref `json:"stakeholders"`
155+
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
157156
// read only
158-
Risk string `json:"risk"`
159-
Confidence int `json:"confidence"`
160-
Status string `json:"status"`
161-
Thresholds assessment.Thresholds `json:"thresholds"`
162-
RiskMessages assessment.RiskMessages `json:"riskMessages" yaml:"riskMessages"`
163-
Required bool `json:"required"`
157+
Risk string `json:"risk"`
158+
Confidence int `json:"confidence"`
159+
Status string `json:"status"`
160+
Thresholds Thresholds `json:"thresholds"`
161+
RiskMessages RiskMessages `json:"riskMessages" yaml:"riskMessages"`
162+
Required bool `json:"required"`
164163
}
165164

165+
type Section model.Section
166+
type Thresholds model.Thresholds
167+
type RiskMessages model.RiskMessages
168+
166169
// With updates the resource with the model.
167170
func (r *Assessment) With(m *model.Assessment) {
168171
r.Resource.With(&m.Model)
@@ -186,18 +189,21 @@ func (r *Assessment) With(m *model.Assessment) {
186189
r.Required = a.Questionnaire.Required
187190
r.Risk = a.Risk()
188191
r.Confidence = a.Confidence()
189-
r.RiskMessages = a.RiskMessages
190-
r.Thresholds = a.Thresholds
191-
r.Sections = a.Sections
192+
r.RiskMessages = RiskMessages(a.RiskMessages)
193+
r.Thresholds = Thresholds(a.Thresholds)
194+
r.Sections = []Section{}
195+
for _, s := range a.Sections {
196+
r.Sections = append(r.Sections, Section(s))
197+
}
192198
r.Status = a.Status()
193199
}
194200

195201
// Model builds a model.
196202
func (r *Assessment) Model() (m *model.Assessment) {
197203
m = &model.Assessment{}
198204
m.ID = r.ID
199-
if r.Sections != nil {
200-
m.Sections, _ = json.Marshal(r.Sections)
205+
for _, s := range r.Sections {
206+
m.Sections = append(m.Sections, model.Section(s))
201207
}
202208
m.QuestionnaireID = r.Questionnaire.ID
203209
if r.Archetype != nil {

api/base.go

-6
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ func (h *BaseHandler) preLoad(db *gorm.DB, fields ...string) (tx *gorm.DB) {
8484
return
8585
}
8686

87-
// fields builds a map of fields.
88-
func (h *BaseHandler) fields(m any) (mp map[string]any) {
89-
mp = reflect.Fields(m)
90-
return
91-
}
92-
9387
// pk returns the PK (ID) parameter.
9488
func (h *BaseHandler) pk(ctx *gin.Context) (id uint) {
9589
s := ctx.Param(ID)

api/businessservice.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (h BusinessServiceHandler) Update(ctx *gin.Context) {
153153
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
154154
db := h.DB(ctx).Model(m)
155155
db = db.Omit(clause.Associations)
156-
result := db.Updates(h.fields(m))
156+
result := db.Save(m)
157157
if result.Error != nil {
158158
_ = ctx.Error(result.Error)
159159
return

api/group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (h StakeholderGroupHandler) Update(ctx *gin.Context) {
163163
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
164164
db := h.DB(ctx).Model(m)
165165
db = db.Omit(clause.Associations)
166-
result := db.Updates(h.fields(m))
166+
result := db.Save(m)
167167
if result.Error != nil {
168168
_ = ctx.Error(result.Error)
169169
return

0 commit comments

Comments
 (0)