Skip to content

Commit

Permalink
Simplify custom Marshal, remove Unmarshal
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Soyland <codysoyland@github.com>
  • Loading branch information
codysoyland committed Dec 20, 2024
1 parent 00bc626 commit df88758
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions pkg/verify/signed_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit df88758

Please sign in to comment.