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"` }