From d77d5a01059a578e480ad55800d53bdfca69f346 Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Wed, 9 Apr 2025 10:53:32 +1200 Subject: [PATCH] Adds custom marshaller for Project to support empty LastBomImports Signed-off-by: Blaize Kaye --- project.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/project.go b/project.go index 8d2b1f0..c2b293b 100644 --- a/project.go +++ b/project.go @@ -2,6 +2,7 @@ package dtrack import ( "context" + "encoding/json" "fmt" "net/http" "strconv" @@ -32,6 +33,24 @@ type Project struct { ExternalReferences []ExternalReference `json:"externalReferences,omitempty"` } +// Here we write a custom MarshalJSON function to give us more control over the JSON output. +func (p Project) MarshalJSON() ([]byte, error) { + type Alias Project // Avoid infinite recursion + aux := struct { + Alias + LastBOMImport *int `json:"lastBomImport,omitempty"` + }{ + Alias: (Alias)(p), + LastBOMImport: nil, + } + // In particular, sending a 0 to the API gives us an invalid date + // i.e. the beginning of the epoch. Better to be nil. + if p.LastBOMImport != 0 { + aux.LastBOMImport = &p.LastBOMImport + } + return json.Marshal(aux) +} + type ParentRef struct { UUID uuid.UUID `json:"uuid,omitempty"` }