Skip to content

Commit 38b0d2b

Browse files
committed
refactor: update UnmarshalJSON() function
1 parent a2b229d commit 38b0d2b

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

pkg/types/finding.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,41 @@ func NewModifiedFinding(f finding, status FindingStatus, statement, source strin
5454

5555
// UnmarshalJSON unmarshals ModifiedFinding given the type and `UnmarshalJSON` functions of struct fields
5656
func (m *ModifiedFinding) UnmarshalJSON(data []byte) error {
57-
raw := struct {
58-
Type FindingType `json:"Type"`
59-
Status FindingStatus `json:"Status"`
60-
Statement string `json:"Statement"`
61-
Source string `json:"Source"`
62-
Finding json.RawMessage `json:"Finding"`
63-
}{}
57+
type Alias ModifiedFinding
58+
aux := &struct {
59+
Finding json.RawMessage `json:"Finding"`
60+
*Alias
61+
}{
62+
Alias: (*Alias)(m),
63+
}
6464

65-
if err := json.Unmarshal(data, &raw); err != nil {
65+
if err := json.Unmarshal(data, &aux); err != nil {
6666
return err
6767
}
6868

69-
m.Type = raw.Type
70-
m.Status = raw.Status
71-
m.Statement = raw.Statement
72-
m.Source = raw.Source
73-
7469
// Select struct by m.Type to avoid errors with Unmarshal
7570
switch m.Type {
7671
case FindingTypeVulnerability:
7772
rawFinding := DetectedVulnerability{}
78-
if err := json.Unmarshal(raw.Finding, &rawFinding); err != nil {
73+
if err := json.Unmarshal(aux.Finding, &rawFinding); err != nil {
7974
return xerrors.Errorf("unable to unmarshal %q type: %w", m.Type, err)
8075
}
8176
m.Finding = rawFinding
8277
case FindingTypeMisconfiguration:
8378
rawFinding := DetectedMisconfiguration{}
84-
if err := json.Unmarshal(raw.Finding, &rawFinding); err != nil {
79+
if err := json.Unmarshal(aux.Finding, &rawFinding); err != nil {
8580
return xerrors.Errorf("unable to unmarshal %q type: %w", m.Type, err)
8681
}
8782
m.Finding = rawFinding
8883
case FindingTypeSecret:
8984
rawFinding := DetectedSecret{}
90-
if err := json.Unmarshal(raw.Finding, &rawFinding); err != nil {
85+
if err := json.Unmarshal(aux.Finding, &rawFinding); err != nil {
9186
return xerrors.Errorf("unable to unmarshal %q type: %w", m.Type, err)
9287
}
9388
m.Finding = rawFinding
9489
case FindingTypeLicense:
9590
rawFinding := DetectedLicense{}
96-
if err := json.Unmarshal(raw.Finding, &rawFinding); err != nil {
91+
if err := json.Unmarshal(aux.Finding, &rawFinding); err != nil {
9792
return xerrors.Errorf("unable to unmarshal %q type: %w", m.Type, err)
9893
}
9994
m.Finding = rawFinding

0 commit comments

Comments
 (0)