-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathCatalogIndexEntry.cs
More file actions
150 lines (124 loc) · 4.34 KB
/
CatalogIndexEntry.cs
File metadata and controls
150 lines (124 loc) · 4.34 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using NuGet.Packaging.Core;
using NuGet.Versioning;
namespace NuGet.Services.Metadata.Catalog
{
public sealed class CatalogIndexEntry : IComparable<CatalogIndexEntry>
{
[JsonConstructor]
private CatalogIndexEntry()
{
Types = Enumerable.Empty<string>();
}
public CatalogIndexEntry(
Uri uri,
string type,
string commitId,
DateTime commitTs,
PackageIdentity packageIdentity)
{
if (string.IsNullOrWhiteSpace(type))
{
throw new ArgumentException(Strings.ArgumentMustNotBeNullEmptyOrWhitespace, nameof(type));
}
Initialize(uri, new[] { type }, commitId, commitTs, packageIdentity);
}
public CatalogIndexEntry(
Uri uri,
IReadOnlyList<string> types,
string commitId,
DateTime commitTs,
PackageIdentity packageIdentity)
{
Initialize(uri, types, commitId, commitTs, packageIdentity);
}
[JsonProperty("@id")]
[JsonRequired]
public Uri Uri { get; private set; }
[JsonProperty("@type")]
[JsonRequired]
[JsonConverter(typeof(CatalogTypeConverter))]
public IEnumerable<string> Types { get; private set; }
[JsonProperty("commitId")]
[JsonRequired]
public string CommitId { get; private set; }
[JsonProperty("commitTimeStamp")]
[JsonRequired]
public DateTime CommitTimeStamp { get; private set; }
[JsonProperty("nuget:id")]
[JsonRequired]
public string Id { get; private set; }
[JsonProperty("nuget:version")]
[JsonRequired]
public NuGetVersion Version { get; private set; }
[JsonIgnore]
public bool IsDelete
{
get
{
return Types.Any(type => type == "nuget:PackageDelete");
}
}
public int CompareTo(CatalogIndexEntry other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
return CommitTimeStamp.CompareTo(other.CommitTimeStamp);
}
public static CatalogIndexEntry Create(CatalogCommitItem commitItem)
{
if (commitItem == null)
{
throw new ArgumentNullException(nameof(commitItem));
}
return new CatalogIndexEntry(
commitItem.Uri,
commitItem.Types,
commitItem.CommitId,
commitItem.CommitTimeStamp,
commitItem.PackageIdentity);
}
private void Initialize(
Uri uri,
IReadOnlyList<string> types,
string commitId,
DateTime commitTs,
PackageIdentity packageIdentity)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
if (types == null || !types.Any())
{
throw new ArgumentException(Strings.ArgumentMustNotBeNullOrEmpty, nameof(types));
}
if (types.Any(type => string.IsNullOrWhiteSpace(type)))
{
throw new ArgumentException(Strings.ArgumentMustNotBeNullEmptyOrWhitespace, nameof(types));
}
Types = types;
if (string.IsNullOrWhiteSpace(commitId))
{
throw new ArgumentException(Strings.ArgumentMustNotBeNullOrEmpty, nameof(commitId));
}
CommitId = commitId;
CommitTimeStamp = commitTs;
if (packageIdentity == null)
{
throw new ArgumentNullException(nameof(packageIdentity));
}
if (packageIdentity.Version is null)
{
throw new ArgumentNullException("The ID and version of the package identity must not be null.");
}
Utils.AssertValidPackageId(packageIdentity.Id);
Id = packageIdentity.Id;
Version = packageIdentity.Version;
}
}
}