-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathGoComponent.cs
More file actions
87 lines (71 loc) · 2.79 KB
/
GoComponent.cs
File metadata and controls
87 lines (71 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.TypedComponent;
using System;
using System.Text.Json.Serialization;
using PackageUrl;
public class GoComponent : TypedComponent, IEquatable<GoComponent>
{
public GoComponent(string name, string version)
{
this.Name = this.ValidateRequiredInput(name, nameof(this.Name), nameof(ComponentType.Go));
this.Version = this.ValidateRequiredInput(version, nameof(this.Version), nameof(ComponentType.Go));
this.Hash = string.Empty;
}
public GoComponent(string name, string version, string hash)
{
this.Name = this.ValidateRequiredInput(name, nameof(this.Name), nameof(ComponentType.Go));
this.Version = this.ValidateRequiredInput(version, nameof(this.Version), nameof(ComponentType.Go));
this.Hash = this.ValidateRequiredInput(hash, nameof(this.Hash), nameof(ComponentType.Go));
}
public GoComponent()
{
/* Reserved for deserialization */
}
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("version")]
public string Version { get; set; }
[JsonPropertyName("hash")]
public string Hash { get; set; }
// Commit should be used in place of version when available
// https://github.com/package-url/purl-spec/blame/180c46d266c45aa2bd81a2038af3f78e87bb4a25/README.rst#L610
// The golang purl spec requires a namespace: https://github.com/package-url/purl-spec/blob/master/types/golang-definition.json
[JsonPropertyName("packageUrl")]
public override PackageURL PackageUrl
{
get
{
var version = string.IsNullOrWhiteSpace(this.Hash) ? this.Version : this.Hash;
var (ns, name) = this.GetNamespaceAndName();
return new PackageURL("golang", ns, name, version, null, null);
}
}
[JsonIgnore]
public override ComponentType Type => ComponentType.Go;
protected override string ComputeBaseId() => $"{this.Name} {this.Version} - {this.Type}";
private (string Namespace, string Name) GetNamespaceAndName()
{
var lastSlash = this.Name.LastIndexOf('/');
if (lastSlash > 0)
{
return (this.Name.Substring(0, lastSlash), this.Name.Substring(lastSlash + 1));
}
return (null, this.Name);
}
public override bool Equals(object obj)
{
return obj is GoComponent otherComponent && this.Equals(otherComponent);
}
public bool Equals(GoComponent other)
{
if (other == null)
{
return false;
}
return this.Name == other.Name && this.Version == other.Version && this.Hash == other.Hash;
}
public override int GetHashCode()
{
return this.Name.GetHashCode() ^ this.Version.GetHashCode() ^ this.Hash.GetHashCode();
}
}