Repository navigation
Expand file tree
/
Copy pathUrnId.cs
More file actions
129 lines (112 loc) · 4.53 KB
/
Copy pathUrnId.cs
File metadata and controls
129 lines (112 loc) · 4.53 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
using System;
using System.Text;
using ServiceStack.Text;
namespace ServiceStack
{
/// <summary>
/// Creates a Unified Resource Name (URN) with the following formats:
///
/// - urn:{TypeName}:{IdFieldValue} e.g. urn:UserSession:1
/// - urn:{TypeName}:{IdFieldName}:{IdFieldValue} e.g. urn:UserSession:UserId:1
///
/// </summary>
public class UrnId
{
private const char FieldSeperator = ':';
private const char FieldPartsSeperator = '/';
public string TypeName { get; private set; }
public string IdFieldValue { get; private set; }
public string IdFieldName { get; private set; }
const int HasNoIdFieldName = 3;
const int HasIdFieldName = 4;
private UrnId() { }
public static UrnId Parse(string urnId)
{
var urnParts = urnId.Split(FieldSeperator);
if (urnParts.Length == HasNoIdFieldName)
{
return new UrnId { TypeName = urnParts[1], IdFieldValue = urnParts[2] };
}
if (urnParts.Length == HasIdFieldName)
{
return new UrnId { TypeName = urnParts[1], IdFieldName = urnParts[2], IdFieldValue = urnParts[3] };
}
throw new ArgumentException("Cannot parse invalid urn: '{0}'", urnId);
}
public static string Create(string objectTypeName, string idFieldValue)
{
if (objectTypeName.Contains(FieldSeperator.ToString()))
{
throw new ArgumentException("objectTypeName cannot have the illegal characters: ':'", "objectTypeName");
}
if (idFieldValue.Contains(FieldSeperator.ToString()))
{
throw new ArgumentException("idFieldValue cannot have the illegal characters: ':'", "idFieldValue");
}
return string.Format("urn:{0}:{1}", objectTypeName, idFieldValue);
}
public static string CreateWithParts(string objectTypeName, params string[] keyParts)
{
if (objectTypeName.Contains(FieldSeperator.ToString()))
{
throw new ArgumentException("objectTypeName cannot have the illegal characters: ':'", "objectTypeName");
}
var sb = StringBuilderCache.Allocate();
foreach (var keyPart in keyParts)
{
if (sb.Length > 0)
sb.Append(FieldPartsSeperator);
sb.Append(keyPart);
}
return string.Format("urn:{0}:{1}", objectTypeName, StringBuilderCache.ReturnAndFree(sb));
}
public static string CreateWithParts<T>(params string[] keyParts)
{
return CreateWithParts(typeof(T).Name, keyParts);
}
public static string Create<T>(string idFieldValue)
{
return Create(typeof(T), idFieldValue);
}
public static string Create<T>(object idFieldValue)
{
return Create(typeof(T), idFieldValue.ToString());
}
public static string Create(Type objectType, string idFieldValue)
{
if (idFieldValue.Contains(FieldSeperator.ToString()))
{
throw new ArgumentException("idFieldValue cannot have the illegal characters: ':'", "idFieldValue");
}
return string.Format("urn:{0}:{1}", objectType.Name, idFieldValue);
}
public static string Create<T>(string idFieldName, string idFieldValue)
{
return Create(typeof(T), idFieldName, idFieldValue);
}
public static string Create(Type objectType, string idFieldName, string idFieldValue)
{
if (idFieldValue.Contains(FieldSeperator.ToString()))
{
throw new ArgumentException("idFieldValue cannot have the illegal characters: ':'", "idFieldValue");
}
if (idFieldName.Contains(FieldSeperator.ToString()))
{
throw new ArgumentException("idFieldName cannot have the illegal characters: ':'", "idFieldName");
}
return string.Format("urn:{0}:{1}:{2}", objectType.Name, idFieldName, idFieldValue);
}
public static string GetStringId(string urn)
{
return Parse(urn).IdFieldValue;
}
public static Guid GetGuidId(string urn)
{
return new Guid(Parse(urn).IdFieldValue);
}
public static long GetLongId(string urn)
{
return long.Parse(Parse(urn).IdFieldValue);
}
}
}