Skip to content

Commit

Permalink
👻 Use JSON serializer everywhere (#680)
Browse files Browse the repository at this point in the history
Extends the use of the JSON serializer to the rest of the models.

Signed-off-by: Sam Lucidi <slucidi@redhat.com>
  • Loading branch information
mansam authored Aug 13, 2024
1 parent 74c5419 commit bcbff9a
Show file tree
Hide file tree
Showing 45 changed files with 576 additions and 608 deletions.
81 changes: 32 additions & 49 deletions api/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,11 +1060,9 @@ func (h AnalysisHandler) RuleReports(ctx *gin.Context) {
Name: m.Name,
}
resources = append(resources, r)
if m.Labels != nil {
_ = json.Unmarshal(m.Labels, &r.Labels)
}
if m.Links != nil {
_ = json.Unmarshal(m.Links, &r.Links)
r.Labels = m.Labels
for _, l := range m.Links {
r.Links = append(r.Links, Link(l))
}
r.Effort += m.Effort
}
Expand Down Expand Up @@ -1197,11 +1195,9 @@ func (h AnalysisHandler) AppIssueReports(ctx *gin.Context) {
ID: m.ID,
}
resources = append(resources, r)
if m.Labels != nil {
_ = json.Unmarshal(m.Labels, &r.Labels)
}
if m.Links != nil {
_ = json.Unmarshal(m.Links, &r.Links)
r.Labels = m.Labels
for _, l := range m.Links {
r.Links = append(r.Links, Link(l))
}
r.Effort += m.Effort
}
Expand Down Expand Up @@ -1722,13 +1718,9 @@ func (h AnalysisHandler) DepReports(ctx *gin.Context) {
Name: m.Name,
Applications: m.Applications,
}
if m.Labels != nil {
var aggregated []string
_ = json.Unmarshal(m.Labels, &aggregated)
for _, s := range aggregated {
if s != "" {
r.Labels = append(r.Labels, s)
}
for _, s := range m.Labels {
if s != "" {
r.Labels = append(r.Labels, s)
}
}
resources = append(resources, r)
Expand Down Expand Up @@ -2082,7 +2074,7 @@ func (h *AnalysisHandler) archive(ctx *gin.Context, q *gorm.DB) (err error) {
db = db.Where("n.IssueID = i.ID")
db = db.Where("i.AnalysisID", m.ID)
db = db.Group("i.ID")
summary := []ArchivedIssue{}
summary := []model.ArchivedIssue{}
err = db.Scan(&summary).Error
if err != nil {
return
Expand All @@ -2091,8 +2083,8 @@ func (h *AnalysisHandler) archive(ctx *gin.Context, q *gorm.DB) (err error) {
db = db.Model(m)
db = db.Omit(clause.Associations)
m.Archived = true
m.Summary, _ = json.Marshal(summary)
err = db.Updates(h.fields(&m)).Error
m.Summary = summary
err = db.Save(&m).Error
if err != nil {
return
}
Expand Down Expand Up @@ -2155,7 +2147,7 @@ type Issue struct {
Effort int `json:"effort,omitempty" yaml:",omitempty"`
Incidents []Incident `json:"incidents,omitempty" yaml:",omitempty"`
Links []Link `json:"links,omitempty" yaml:",omitempty"`
Facts FactMap `json:"facts,omitempty" yaml:",omitempty"`
Facts Map `json:"facts,omitempty" yaml:",omitempty"`
Labels []string `json:"labels"`
}

Expand All @@ -2176,15 +2168,11 @@ func (r *Issue) With(m *model.Issue) {
r.Incidents,
n)
}
if m.Links != nil {
_ = json.Unmarshal(m.Links, &r.Links)
}
if m.Facts != nil {
_ = json.Unmarshal(m.Facts, &r.Facts)
}
if m.Labels != nil {
_ = json.Unmarshal(m.Labels, &r.Labels)
for _, l := range m.Links {
r.Links = append(r.Links, Link(l))
}
r.Facts = m.Facts
r.Labels = m.Labels
r.Effort = m.Effort
}

Expand All @@ -2203,9 +2191,11 @@ func (r *Issue) Model() (m *model.Issue) {
m.Incidents,
*n)
}
m.Links, _ = json.Marshal(r.Links)
m.Facts, _ = json.Marshal(r.Facts)
m.Labels, _ = json.Marshal(r.Labels)
for _, l := range r.Links {
m.Links = append(m.Links, model.Link(l))
}
m.Facts = r.Facts
m.Labels = r.Labels
m.Effort = r.Effort
return
}
Expand All @@ -2231,9 +2221,7 @@ func (r *TechDependency) With(m *model.TechDependency) {
r.Version = m.Version
r.Indirect = m.Indirect
r.SHA = m.SHA
if m.Labels != nil {
_ = json.Unmarshal(m.Labels, &r.Labels)
}
r.Labels = m.Labels
}

// Model builds a model.
Expand All @@ -2244,20 +2232,20 @@ func (r *TechDependency) Model() (m *model.TechDependency) {
m.Version = r.Version
m.Provider = r.Provider
m.Indirect = r.Indirect
m.Labels, _ = json.Marshal(r.Labels)
m.Labels = r.Labels
m.SHA = r.SHA
return
}

// Incident REST resource.
type Incident struct {
Resource `yaml:",inline"`
Issue uint `json:"issue"`
File string `json:"file"`
Line int `json:"line"`
Message string `json:"message"`
CodeSnip string `json:"codeSnip" yaml:"codeSnip"`
Facts FactMap `json:"facts"`
Issue uint `json:"issue"`
File string `json:"file"`
Line int `json:"line"`
Message string `json:"message"`
CodeSnip string `json:"codeSnip" yaml:"codeSnip"`
Facts Map `json:"facts"`
}

// With updates the resource with the model.
Expand All @@ -2268,9 +2256,7 @@ func (r *Incident) With(m *model.Incident) {
r.Line = m.Line
r.Message = m.Message
r.CodeSnip = m.CodeSnip
if m.Facts != nil {
_ = json.Unmarshal(m.Facts, &r.Facts)
}
r.Facts = m.Facts
}

// Model builds a model.
Expand All @@ -2280,7 +2266,7 @@ func (r *Incident) Model() (m *model.Incident) {
m.Line = r.Line
m.Message = r.Message
m.CodeSnip = r.CodeSnip
m.Facts, _ = json.Marshal(r.Facts)
m.Facts = r.Facts
return
}

Expand Down Expand Up @@ -2371,9 +2357,6 @@ type DepAppReport struct {
} `json:"dependency"`
}

// FactMap map.
type FactMap map[string]any

// IssueWriter used to create a file containing issues.
type IssueWriter struct {
encoder
Expand Down
28 changes: 13 additions & 15 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (h ApplicationHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db = h.DB(ctx).Model(m)
db = db.Omit(clause.Associations, "BucketID")
result = db.Updates(h.fields(m))
result = db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down Expand Up @@ -722,12 +722,10 @@ func (h ApplicationHandler) FactList(ctx *gin.Context, key FactKey) {
return
}

facts := FactMap{}
facts := Map{}
for i := range list {
fact := &list[i]
var v any
_ = json.Unmarshal(fact.Value, &v)
facts[fact.Key] = v
facts[fact.Key] = fact.Value
}
h.Respond(ctx, http.StatusOK, facts)
}
Expand Down Expand Up @@ -772,9 +770,7 @@ func (h ApplicationHandler) FactGet(ctx *gin.Context) {
return
}

var v any
_ = json.Unmarshal(list[0].Value, &v)
h.Respond(ctx, http.StatusOK, v)
h.Respond(ctx, http.StatusOK, list[0].Value)
}

// FactCreate godoc
Expand Down Expand Up @@ -846,12 +842,11 @@ func (h ApplicationHandler) FactPut(ctx *gin.Context) {
return
}

value, _ := json.Marshal(f.Value)
m := &model.Fact{
Key: key.Name(),
Source: key.Source(),
ApplicationID: id,
Value: value,
Value: f.Value,
}
db := h.DB(ctx)
result = db.Save(m)
Expand Down Expand Up @@ -906,7 +901,7 @@ func (h ApplicationHandler) FactDelete(ctx *gin.Context) {
// @param factmap body api.FactMap true "Fact map"
func (h ApplicationHandler) FactReplace(ctx *gin.Context, key FactKey) {
id := h.pk(ctx)
facts := FactMap{}
facts := Map{}
err := h.Bind(ctx, &facts)
if err != nil {
_ = ctx.Error(err)
Expand Down Expand Up @@ -1145,7 +1140,10 @@ func (r *Application) With(m *model.Application, tags []model.ApplicationTag) {
r.Bucket = r.refPtr(m.BucketID, m.Bucket)
r.Comments = m.Comments
r.Binary = m.Binary
_ = json.Unmarshal(m.Repository, &r.Repository)
if m.Repository != (model.Repository{}) {
repo := Repository(m.Repository)
r.Repository = &repo
}
if m.Review != nil {
ref := &Ref{}
ref.With(m.Review.ID, "")
Expand Down Expand Up @@ -1246,7 +1244,7 @@ func (r *Application) Model() (m *model.Application) {
}
m.ID = r.ID
if r.Repository != nil {
m.Repository, _ = json.Marshal(r.Repository)
m.Repository = model.Repository(*r.Repository)
}
if r.BusinessService != nil {
m.BusinessServiceID = &r.BusinessService.ID
Expand Down Expand Up @@ -1307,14 +1305,14 @@ type Fact struct {
func (r *Fact) With(m *model.Fact) {
r.Key = m.Key
r.Source = m.Source
_ = json.Unmarshal(m.Value, &r.Value)
r.Value = m.Value
}

func (r *Fact) Model() (m *model.Fact) {
m = &model.Fact{}
m.Key = r.Key
m.Source = r.Source
m.Value, _ = json.Marshal(r.Value)
m.Value = r.Value
return
}

Expand Down
2 changes: 1 addition & 1 deletion api/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (h ArchetypeHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
44 changes: 25 additions & 19 deletions api/assessment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -126,7 +125,7 @@ func (h AssessmentHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations, "Thresholds", "RiskMessages")
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand All @@ -148,21 +147,25 @@ func (h AssessmentHandler) Update(ctx *gin.Context) {
// Assessment REST resource.
type Assessment struct {
Resource `yaml:",inline"`
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
Questionnaire Ref `json:"questionnaire" binding:"required"`
Sections []assessment.Section `json:"sections" binding:"dive"`
Stakeholders []Ref `json:"stakeholders"`
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
Application *Ref `json:"application,omitempty" yaml:",omitempty" binding:"excluded_with=Archetype"`
Archetype *Ref `json:"archetype,omitempty" yaml:",omitempty" binding:"excluded_with=Application"`
Questionnaire Ref `json:"questionnaire" binding:"required"`
Sections []Section `json:"sections" binding:"dive"`
Stakeholders []Ref `json:"stakeholders"`
StakeholderGroups []Ref `json:"stakeholderGroups" yaml:"stakeholderGroups"`
// read only
Risk string `json:"risk"`
Confidence int `json:"confidence"`
Status string `json:"status"`
Thresholds assessment.Thresholds `json:"thresholds"`
RiskMessages assessment.RiskMessages `json:"riskMessages" yaml:"riskMessages"`
Required bool `json:"required"`
Risk string `json:"risk"`
Confidence int `json:"confidence"`
Status string `json:"status"`
Thresholds Thresholds `json:"thresholds"`
RiskMessages RiskMessages `json:"riskMessages" yaml:"riskMessages"`
Required bool `json:"required"`
}

type Section model.Section
type Thresholds model.Thresholds
type RiskMessages model.RiskMessages

// With updates the resource with the model.
func (r *Assessment) With(m *model.Assessment) {
r.Resource.With(&m.Model)
Expand All @@ -186,18 +189,21 @@ func (r *Assessment) With(m *model.Assessment) {
r.Required = a.Questionnaire.Required
r.Risk = a.Risk()
r.Confidence = a.Confidence()
r.RiskMessages = a.RiskMessages
r.Thresholds = a.Thresholds
r.Sections = a.Sections
r.RiskMessages = RiskMessages(a.RiskMessages)
r.Thresholds = Thresholds(a.Thresholds)
r.Sections = []Section{}
for _, s := range a.Sections {
r.Sections = append(r.Sections, Section(s))
}
r.Status = a.Status()
}

// Model builds a model.
func (r *Assessment) Model() (m *model.Assessment) {
m = &model.Assessment{}
m.ID = r.ID
if r.Sections != nil {
m.Sections, _ = json.Marshal(r.Sections)
for _, s := range r.Sections {
m.Sections = append(m.Sections, model.Section(s))
}
m.QuestionnaireID = r.Questionnaire.ID
if r.Archetype != nil {
Expand Down
6 changes: 0 additions & 6 deletions api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ func (h *BaseHandler) preLoad(db *gorm.DB, fields ...string) (tx *gorm.DB) {
return
}

// fields builds a map of fields.
func (h *BaseHandler) fields(m any) (mp map[string]any) {
mp = reflect.Fields(m)
return
}

// pk returns the PK (ID) parameter.
func (h *BaseHandler) pk(ctx *gin.Context) (id uint) {
s := ctx.Param(ID)
Expand Down
2 changes: 1 addition & 1 deletion api/businessservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (h BusinessServiceHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
2 changes: 1 addition & 1 deletion api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (h StakeholderGroupHandler) Update(ctx *gin.Context) {
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db := h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result := db.Updates(h.fields(m))
result := db.Save(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down
Loading

0 comments on commit bcbff9a

Please sign in to comment.