From df8875893f0df5dcd40441e59563f8df1d2ba69e Mon Sep 17 00:00:00 2001 From: Cody Soyland Date: Fri, 20 Dec 2024 12:43:15 -0500 Subject: [PATCH] Simplify custom Marshal, remove Unmarshal Signed-off-by: Cody Soyland --- pkg/verify/signed_entity.go | 43 +++++++++++-------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/pkg/verify/signed_entity.go b/pkg/verify/signed_entity.go index 21c0483..1054c0b 100644 --- a/pkg/verify/signed_entity.go +++ b/pkg/verify/signed_entity.go @@ -223,45 +223,26 @@ func NewVerificationResult() *VerificationResult { } } -// verificationResultRawStatement is a helper struct to marshal/unmarshal -// It is used because in_toto.Statement is a protobuf message and we want to -// store it as a raw message so we can use protojson to marshal/unmarshal it -// -// See https://github.com/in-toto/attestation/issues/363 -type verificationResultRawStatement struct { - MediaType string `json:"mediaType"` - Statement json.RawMessage `json:"statement,omitempty"` - Signature *SignatureVerificationResult `json:"signature,omitempty"` - VerifiedTimestamps []TimestampVerificationResult `json:"verifiedTimestamps"` - VerifiedIdentity *CertificateIdentity `json:"verifiedIdentity,omitempty"` -} - +// MarshalJSON deals with protojson needed for the Statement. +// Can be removed when https://github.com/in-toto/attestation/pull/403 is merged. +// TODO: Should we also add UnmarshalJSON? func (b *VerificationResult) MarshalJSON() ([]byte, error) { statement, err := protojson.Marshal(b.Statement) if err != nil { return nil, err } - return json.Marshal(&verificationResultRawStatement{ - MediaType: b.MediaType, - Statement: statement, - Signature: b.Signature, - VerifiedTimestamps: b.VerifiedTimestamps, - VerifiedIdentity: b.VerifiedIdentity, + // creating a type alias to avoid infinite recursion, as MarshalJSON is + // not copied into the alias. + type Alias VerificationResult + return json.Marshal(struct { + Alias + Statement json.RawMessage `json:"statement,omitempty"` + }{ + Alias: Alias(*b), + Statement: statement, }) } -func (b *VerificationResult) UnmarshalJSON(data []byte) error { - var aux verificationResultRawStatement - if err := json.Unmarshal(data, &aux); err != nil { - return err - } - b.MediaType = aux.MediaType - b.Signature = aux.Signature - b.VerifiedTimestamps = aux.VerifiedTimestamps - b.VerifiedIdentity = aux.VerifiedIdentity - return protojson.Unmarshal(aux.Statement, b.Statement) -} - type PolicyOption func(*PolicyConfig) error type ArtifactPolicyOption func(*PolicyConfig) error