From 18b7193bc0d643f3d18648efbb8b5af7eeeb785b Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Tue, 1 Oct 2024 04:16:11 -0400 Subject: [PATCH] feat: non null slice on parser result --- internal/stage/model.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/stage/model.go b/internal/stage/model.go index 224b687..bd21dd7 100644 --- a/internal/stage/model.go +++ b/internal/stage/model.go @@ -1,6 +1,7 @@ package stage import ( + "encoding/json" "fmt" "strconv" @@ -162,8 +163,17 @@ type ParserResult struct { Comment string `json:"comment"` } -type StageResult struct { - Name string `json:"name"` - Results []ParserResult `json:"results"` - ForceQuit bool `json:"force_quit"` +type NonNullSlice[T any] []T + +func (s NonNullSlice[T]) MarshalJSON() ([]byte, error) { + if len(s) == 0 { + return []byte("[]"), nil + } + return json.Marshal([]T(s)) +} + +type StageResult struct { + Name string `json:"name"` + Results NonNullSlice[ParserResult] `json:"results"` + ForceQuit bool `json:"force_quit"` }