-
Notifications
You must be signed in to change notification settings - Fork 51
/
FolderName.cs
208 lines (184 loc) · 10.4 KB
/
FolderName.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2018 Google LLC
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
using gax = Google.Api.Gax;
using sys = System;
namespace Google.Api.Gax.ResourceNames
{
/// <summary>
/// Resource name for the 'folder' resource which is widespread across Google Cloud.
/// While most resource names are generated on a per-API basis, many APIs use a folder resource, and it's
/// useful to be able to pass values from one API to another.
/// </summary>
public sealed partial class FolderName : gax::IResourceName, sys::IEquatable<FolderName>
{
/// <summary>The possible contents of <see cref="FolderName"/>.</summary>
public enum ResourceNameType
{
/// <summary>An unparsed resource name.</summary>
Unparsed = 0,
/// <summary>A resource name with pattern <c>folders/{folder}</c>.</summary>
Folder = 1
}
private static gax::PathTemplate s_folder = new gax::PathTemplate("folders/{folder}");
/// <summary>Creates a <see cref="FolderName"/> containing an unparsed resource name.</summary>
/// <param name="unparsedResourceName">The unparsed resource name. Must not be <c>null</c>.</param>
/// <returns>
/// A new instance of <see cref="FolderName"/> containing the provided <paramref name="unparsedResourceName"/>.
/// </returns>
public static FolderName FromUnparsed(gax::UnparsedResourceName unparsedResourceName) =>
new FolderName(ResourceNameType.Unparsed, gax::GaxPreconditions.CheckNotNull(unparsedResourceName, nameof(unparsedResourceName)));
/// <summary>Creates a <see cref="FolderName"/> with the pattern <c>folders/{folder}</c>.</summary>
/// <param name="folderId">The <c>Folder</c> ID. Must not be <c>null</c> or empty.</param>
/// <returns>A new instance of <see cref="FolderName"/> constructed from the provided ids.</returns>
public static FolderName FromFolder(string folderId) =>
new FolderName(ResourceNameType.Folder, folderId: gax::GaxPreconditions.CheckNotNullOrEmpty(folderId, nameof(folderId)));
/// <summary>
/// Formats the IDs into the string representation of this <see cref="FolderName"/> with pattern
/// <c>folders/{folder}</c>.
/// </summary>
/// <param name="folderId">The <c>Folder</c> ID. Must not be <c>null</c> or empty.</param>
/// <returns>
/// The string representation of this <see cref="FolderName"/> with pattern <c>folders/{folder}</c>.
/// </returns>
public static string Format(string folderId) => FormatFolder(folderId);
/// <summary>
/// Formats the IDs into the string representation of this <see cref="FolderName"/> with pattern
/// <c>folders/{folder}</c>.
/// </summary>
/// <param name="folderId">The <c>Folder</c> ID. Must not be <c>null</c> or empty.</param>
/// <returns>
/// The string representation of this <see cref="FolderName"/> with pattern <c>folders/{folder}</c>.
/// </returns>
public static string FormatFolder(string folderId) =>
s_folder.Expand(gax::GaxPreconditions.CheckNotNullOrEmpty(folderId, nameof(folderId)));
/// <summary>Parses the given resource name string into a new <see cref="FolderName"/> instance.</summary>
/// <remarks>
/// To parse successfully, the resource name must be formatted as one of the following:
/// <list type="bullet"><item><description><c>folders/{folder}</c></description></item></list>
/// </remarks>
/// <param name="folderName">The resource name in string form. Must not be <c>null</c>.</param>
/// <returns>The parsed <see cref="FolderName"/> if successful.</returns>
public static FolderName Parse(string folderName) => Parse(folderName, false);
/// <summary>
/// Parses the given resource name string into a new <see cref="FolderName"/> instance; optionally allowing an
/// unparseable resource name.
/// </summary>
/// <remarks>
/// To parse successfully, the resource name must be formatted as one of the following:
/// <list type="bullet"><item><description><c>folders/{folder}</c></description></item></list>
/// Or may be in any format if <paramref name="allowUnparsed"/> is <c>true</c>.
/// </remarks>
/// <param name="folderName">The resource name in string form. Must not be <c>null</c>.</param>
/// <param name="allowUnparsed">
/// If <c>true</c> will successfully store an unparseable resource name into the <see cref="UnparsedResource"/>
/// property; otherwise will throw an <see cref="sys::ArgumentException"/> if an unparseable resource name is
/// specified.
/// </param>
/// <returns>The parsed <see cref="FolderName"/> if successful.</returns>
public static FolderName Parse(string folderName, bool allowUnparsed) =>
TryParse(folderName, allowUnparsed, out FolderName result) ? result : throw new sys::ArgumentException("The given resource-name matches no pattern.");
/// <summary>
/// Tries to parse the given resource name string into a new <see cref="FolderName"/> instance.
/// </summary>
/// <remarks>
/// To parse successfully, the resource name must be formatted as one of the following:
/// <list type="bullet"><item><description><c>folders/{folder}</c></description></item></list>
/// </remarks>
/// <param name="folderName">The resource name in string form. Must not be <c>null</c>.</param>
/// <param name="result">
/// When this method returns, the parsed <see cref="FolderName"/>, or <c>null</c> if parsing failed.
/// </param>
/// <returns><c>true</c> if the name was parsed successfully; <c>false</c> otherwise.</returns>
public static bool TryParse(string folderName, out FolderName result) => TryParse(folderName, false, out result);
/// <summary>
/// Tries to parse the given resource name string into a new <see cref="FolderName"/> instance; optionally
/// allowing an unparseable resource name.
/// </summary>
/// <remarks>
/// To parse successfully, the resource name must be formatted as one of the following:
/// <list type="bullet"><item><description><c>folders/{folder}</c></description></item></list>
/// Or may be in any format if <paramref name="allowUnparsed"/> is <c>true</c>.
/// </remarks>
/// <param name="folderName">The resource name in string form. Must not be <c>null</c>.</param>
/// <param name="allowUnparsed">
/// If <c>true</c> will successfully store an unparseable resource name into the <see cref="UnparsedResource"/>
/// property; otherwise will throw an <see cref="sys::ArgumentException"/> if an unparseable resource name is
/// specified.
/// </param>
/// <param name="result">
/// When this method returns, the parsed <see cref="FolderName"/>, or <c>null</c> if parsing failed.
/// </param>
/// <returns><c>true</c> if the name was parsed successfully; <c>false</c> otherwise.</returns>
public static bool TryParse(string folderName, bool allowUnparsed, out FolderName result)
{
gax::GaxPreconditions.CheckNotNull(folderName, nameof(folderName));
gax::TemplatedResourceName resourceName;
if (s_folder.TryParseName(folderName, out resourceName))
{
result = FromFolder(resourceName[0]);
return true;
}
if (allowUnparsed)
{
if (gax::UnparsedResourceName.TryParse(folderName, out gax::UnparsedResourceName unparsedResourceName))
{
result = FromUnparsed(unparsedResourceName);
return true;
}
}
result = null;
return false;
}
private FolderName(ResourceNameType type, gax::UnparsedResourceName unparsedResourceName = null, string folderId = null)
{
Type = type;
UnparsedResource = unparsedResourceName;
FolderId = folderId;
}
/// <summary>
/// Constructs a new instance of a <see cref="FolderName"/> class from the component parts of pattern
/// <c>folders/{folder}</c>
/// </summary>
/// <param name="folderId">The <c>Folder</c> ID. Must not be <c>null</c> or empty.</param>
public FolderName(string folderId) : this(ResourceNameType.Folder, folderId: gax::GaxPreconditions.CheckNotNullOrEmpty(folderId, nameof(folderId)))
{
}
/// <summary>The <see cref="ResourceNameType"/> of the contained resource name.</summary>
public ResourceNameType Type { get; }
/// <summary>
/// The contained <see cref="gax::UnparsedResourceName"/>. Only non-<c>null</c>if this instance contains an
/// unparsed resource name.
/// </summary>
public gax::UnparsedResourceName UnparsedResource { get; }
/// <summary>
/// The <c>Folder</c> ID. Will not be <c>null</c>, unless this instance contains an unparsed resource name.
/// </summary>
public string FolderId { get; }
/// <inheritdoc/>
public bool IsKnownPattern => Type != ResourceNameType.Unparsed;
/// <inheritdoc/>
public override string ToString()
{
switch (Type)
{
case ResourceNameType.Unparsed: return UnparsedResource.ToString();
case ResourceNameType.Folder: return s_folder.Expand(FolderId);
default: throw new sys::InvalidOperationException("Unrecognized resource-type.");
}
}
/// <inheritdoc/>
public override int GetHashCode() => ToString().GetHashCode();
/// <inheritdoc/>
public override bool Equals(object obj) => Equals(obj as FolderName);
/// <inheritdoc/>
public bool Equals(FolderName other) => ToString() == other?.ToString();
/// <inheritdoc/>
public static bool operator ==(FolderName a, FolderName b) => ReferenceEquals(a, b) || (a?.Equals(b) ?? false);
/// <inheritdoc/>
public static bool operator !=(FolderName a, FolderName b) => !(a == b);
}
}