From 696a674b645659e47f78fa77dfed5fe58fd5bf26 Mon Sep 17 00:00:00 2001 From: Dominik Richter <dominik.richter@gmail.com> Date: Tue, 16 Jul 2024 01:16:12 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20add=20json=20marshal=20for=20sco?= =?UTF-8?q?ring=20system=20(#4345)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- explorer/impact.go | 17 +++++++++++++++++ explorer/impact_test.go | 14 ++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/explorer/impact.go b/explorer/impact.go index f08be6692d..86c08dc62d 100644 --- a/explorer/impact.go +++ b/explorer/impact.go @@ -164,6 +164,23 @@ func (s *ScoringSystem) UnmarshalYAML(node *yaml.Node) error { return nil } +func (s *ScoringSystem) MarshalJSON() ([]byte, error) { + switch *s { + case ScoringSystem_WORST: + return []byte("highest impact"), nil + case ScoringSystem_WEIGHTED: + return []byte("weighted"), nil + case ScoringSystem_AVERAGE: + return []byte("average"), nil + case ScoringSystem_BANDED: + return []byte("banded"), nil + case ScoringSystem_DECAYED: + return []byte("decayed"), nil + default: + return []byte("unknown"), nil + } +} + func (s *ScoringSystem) MarshalYAML() (interface{}, error) { switch *s { case ScoringSystem_WORST: diff --git a/explorer/impact_test.go b/explorer/impact_test.go index c508baccc2..ec967f7cba 100644 --- a/explorer/impact_test.go +++ b/explorer/impact_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestParsing(t *testing.T) { +func TestImpactParsing(t *testing.T) { tests := []struct { title string data string @@ -81,7 +81,7 @@ func TestParsing(t *testing.T) { } } -func TestMerging(t *testing.T) { +func TestImpactMerging(t *testing.T) { tests := []struct { title string base *Impact @@ -131,3 +131,13 @@ func TestMerging(t *testing.T) { }) } } + +func TestScoringSystemParsing(t *testing.T) { + s := ScoringSystem_DECAYED + raw, err := json.Marshal(s) + require.NoError(t, err) + + err = json.Unmarshal(raw, &s) + require.NoError(t, err) + assert.Equal(t, ScoringSystem_DECAYED, s) +}