Skip to content

Commit

Permalink
CycloneDX#349 Go through implementing all the equal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thompson-tomo committed Sep 7, 2024
1 parent 355f20a commit 7d934d4
Show file tree
Hide file tree
Showing 61 changed files with 1,554 additions and 123 deletions.
27 changes: 25 additions & 2 deletions src/CycloneDX.Core/Models/Annotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
using System.Xml.Serialization;
using System.Text.Json.Serialization;
using ProtoBuf;
using System.Linq;

namespace CycloneDX.Models
{
[ProtoContract]
public class Annotation
public class Annotation : IEquatable<Annotation>
{
[XmlType("subject")]
public class XmlAnnotationSubject
Expand Down Expand Up @@ -81,9 +82,31 @@ public DateTime? Timestamp
set { _timestamp = BomUtils.UtcifyDateTime(value); }
}
public bool ShouldSerializeTimestamp() { return Timestamp != null; }



[XmlElement("text")]
[ProtoMember(5)]
public string Text { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as Annotation);
}

public bool Equals(Annotation obj)
{
return obj != null &&
(object.ReferenceEquals(this.Annotator, obj.Annotator) ||
this.Annotator.Equals(obj.Annotator)) &&
(object.ReferenceEquals(this.BomRef, obj.BomRef) ||
this.BomRef.Equals(obj.BomRef, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Subjects, obj.Subjects) ||
this.Subjects.SequenceEqual(obj.Subjects)) &&
(object.ReferenceEquals(this.Text, obj.Text) ||
this.Text.Equals(obj.Text)) &&
(this.Timestamp.Equals(obj.Timestamp)) &&
(object.ReferenceEquals(this.XmlSubjects, obj.XmlSubjects) ||
this.XmlSubjects.SequenceEqual(obj.XmlSubjects));
}
}
}
21 changes: 20 additions & 1 deletion src/CycloneDX.Core/Models/AnnotatorChoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.

using System;
using System.Xml.Serialization;
using ProtoBuf;

namespace CycloneDX.Models
{
[ProtoContract]
public class AnnotatorChoice
public class AnnotatorChoice : IEquatable<AnnotatorChoice>
{
[XmlElement("organization")]
[ProtoMember(1)]
Expand All @@ -38,5 +39,23 @@ public class AnnotatorChoice
[XmlElement("service")]
[ProtoMember(4)]
public Service Service { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as AnnotatorChoice);
}

public bool Equals(AnnotatorChoice obj)
{
return obj != null &&
(object.ReferenceEquals(this.Component, obj.Component) ||
this.Component.Equals(obj.Component)) &&
(object.ReferenceEquals(this.Individual, obj.Individual) ||
this.Individual.Equals(obj.Individual)) &&
(object.ReferenceEquals(this.Organization, obj.Organization) ||
this.Organization.Equals(obj.Organization)) &&
(object.ReferenceEquals(this.Service, obj.Service) ||
this.Service.Equals(obj.Service));
}
}
}
19 changes: 18 additions & 1 deletion src/CycloneDX.Core/Models/AttachedText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.

using System;
using System.Xml.Serialization;
using ProtoBuf;

namespace CycloneDX.Models
{
[ProtoContract]
public class AttachedText
public class AttachedText : IEquatable<AttachedText>
{
[XmlAttribute("content-type")]
[ProtoMember(1)]
Expand All @@ -34,5 +35,21 @@ public class AttachedText
[XmlText]
[ProtoMember(3)]
public string Content { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as AttachedText);
}

public bool Equals(AttachedText obj)
{
return obj != null &&
(object.ReferenceEquals(this.Content, obj.Content) ||
this.Content.Equals(obj.Content, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.ContentType, obj.ContentType) ||
this.ContentType.Equals(obj.ContentType, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Encoding, obj.Encoding) ||
this.Encoding.Equals(obj.Encoding, StringComparison.InvariantCultureIgnoreCase));
}
}
}
41 changes: 40 additions & 1 deletion src/CycloneDX.Core/Models/Bom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace CycloneDX.Models
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
[XmlRoot("bom", IsNullable=false)]
[ProtoContract]
public class Bom
public class Bom : IEquatable<Bom>
{
[XmlIgnore]
public string BomFormat => "CycloneDX";
Expand Down Expand Up @@ -168,5 +168,44 @@ public int NonNullableVersion
[ProtoMember(13)]
public List<Formula> Formulation { get; set; }
public bool ShouldSerializeFormulation() { return Formulation?.Count > 0; }

public override bool Equals(object obj)
{
return Equals(obj as Bom);
}

public bool Equals(Bom obj)
{
return obj != null &&
(object.ReferenceEquals(this.Annotations, obj.Annotations) ||
this.Annotations.SequenceEqual(obj.Annotations)) &&
(object.ReferenceEquals(this.BomFormat, obj.BomFormat) ||
this.BomFormat.Equals(obj.BomFormat, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Components, obj.Components) ||
this.Components.SequenceEqual(obj.Components)) &&
(object.ReferenceEquals(this.Compositions, obj.Compositions) ||
this.Compositions.SequenceEqual(obj.Compositions)) &&
(object.ReferenceEquals(this.Dependencies, obj.Dependencies) ||
this.Dependencies.SequenceEqual(obj.Dependencies)) &&
(object.ReferenceEquals(this.ExternalReferences, obj.ExternalReferences) ||
this.ExternalReferences.SequenceEqual(obj.ExternalReferences)) &&
(object.ReferenceEquals(this.Formulation, obj.Formulation) ||
this.Formulation.SequenceEqual(obj.Formulation)) &&
(object.ReferenceEquals(this.Metadata, obj.Metadata) ||
this.Metadata.Equals(obj.Metadata)) &&
(this.NonNullableVersion.Equals(obj.NonNullableVersion)) &&

(object.ReferenceEquals(this.Properties, obj.Properties) ||
this.Properties.SequenceEqual(obj.Properties)) &&
(object.ReferenceEquals(this.SerialNumber, obj.SerialNumber) ||
this.SerialNumber.Equals(obj.SerialNumber, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Services, obj.Services) ||
this.Services.SequenceEqual(obj.Services)) &&
(this.SpecVersion.Equals(obj.SpecVersion)) &&
(this.SpecVersionString.Equals(obj.SpecVersionString)) &&
(this.Version.Equals(obj.Version)) &&
(object.ReferenceEquals(this.Vulnerabilities, obj.Vulnerabilities) ||
this.Vulnerabilities.Equals(obj.Vulnerabilities));
}
}
}
39 changes: 37 additions & 2 deletions src/CycloneDX.Core/Models/Callstack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Json.Serialization;
using System.Xml;
Expand All @@ -28,11 +29,11 @@ namespace CycloneDX.Models
{
[XmlType("callstack")]
[ProtoContract]
public class Callstack
public class Callstack : IEquatable<Callstack>
{
[XmlType("frame")]
[ProtoContract]
public class Frame
public class Frame : IEquatable<Frame>
{
[XmlElement("package")]
[ProtoMember(1)]
Expand Down Expand Up @@ -63,11 +64,45 @@ public class Frame
[XmlElement("fullFilename")]
[ProtoMember(7)]
public string FullFilename { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as Frame);
}

public bool Equals(Frame obj)
{
return obj != null &&
(this.Column.Equals(obj.Column)) &&
(object.ReferenceEquals(this.FullFilename, obj.FullFilename) ||
this.FullFilename.SequenceEqual(obj.FullFilename)) &&
(object.ReferenceEquals(this.Function, obj.Function) ||
this.Function.Equals(obj.Function, StringComparison.InvariantCultureIgnoreCase)) &&
(this.Line.Equals(obj.Line)) &&
(object.ReferenceEquals(this.Module, obj.Module) ||
this.Module.SequenceEqual(obj.Module)) &&
(object.ReferenceEquals(this.Package, obj.Package) ||
this.Package.Equals(obj.Package)) &&
(object.ReferenceEquals(this.Parameters, obj.Parameters) ||
this.Parameters.SequenceEqual(obj.Parameters));
}
}

[XmlArray("frames")]
[XmlArrayItem("frame")]
[ProtoMember(1)]
public List<Frame> Frames { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as Callstack);
}

public bool Equals(Callstack obj)
{
return obj != null &&
(object.ReferenceEquals(this.Frames, obj.Frames) ||
this.Frames.SequenceEqual(obj.Frames));
}
}
}
17 changes: 16 additions & 1 deletion src/CycloneDX.Core/Models/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using ProtoBuf;
Expand All @@ -26,7 +27,7 @@ namespace CycloneDX.Models
{
[XmlType("command")]
[ProtoContract]
public class Command
public class Command : IEquatable<Command>
{
[XmlElement("executed")]
[ProtoMember(1)]
Expand All @@ -37,5 +38,19 @@ public class Command
[ProtoMember(2)]
public List<Property> Properties { get; set; }
public bool ShouldSerializeProperties() { return Properties?.Count > 0; }

public override bool Equals(object obj)
{
return Equals(obj as Command);
}

public bool Equals(Command obj)
{
return obj != null &&
(object.ReferenceEquals(this.Executed, obj.Executed) ||
this.Executed.Equals(obj.Executed, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Properties, obj.Properties) ||
this.Properties.SequenceEqual(obj.Properties));
}
}
}
23 changes: 22 additions & 1 deletion src/CycloneDX.Core/Models/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.

using System;
using System.Xml.Serialization;
using ProtoBuf;

namespace CycloneDX.Models
{
[ProtoContract]
public class Commit
public class Commit : IEquatable<Commit>
{
[XmlElement("uid")]
[ProtoMember(1)]
Expand All @@ -42,5 +43,25 @@ public class Commit
[XmlElement("message")]
[ProtoMember(5)]
public string Message { get; set; }

public override bool Equals(object obj)
{
return Equals(obj as Commit);
}

public bool Equals(Commit obj)
{
return obj != null &&
(object.ReferenceEquals(this.Author, obj.Author) ||
this.Author.Equals(obj.Author)) &&
(object.ReferenceEquals(this.Committer, obj.Committer) ||
this.Committer.Equals(obj.Committer)) &&
(object.ReferenceEquals(this.Message, obj.Message) ||
this.Message.Equals(obj.Message, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Uid, obj.Uid) ||
this.Uid.Equals(obj.Uid, StringComparison.InvariantCultureIgnoreCase)) &&
(object.ReferenceEquals(this.Url, obj.Url) ||
this.Url.Equals(obj.Url, StringComparison.InvariantCultureIgnoreCase));
}
}
}
Loading

0 comments on commit 7d934d4

Please sign in to comment.