|
9 | 9 |
|
10 | 10 | namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata |
11 | 11 | { |
| 12 | + /// <summary> |
| 13 | + /// Represents the metadata for a PostgreSQL enum. |
| 14 | + /// </summary> |
| 15 | + [PublicAPI] |
12 | 16 | public class PostgresEnum |
13 | 17 | { |
14 | | - readonly IAnnotatable _annotatable; |
15 | | - readonly string _annotationName; |
16 | | - |
17 | | - internal PostgresEnum(IAnnotatable annotatable, string annotationName) |
| 18 | + [NotNull] readonly IAnnotatable _annotatable; |
| 19 | + [NotNull] readonly string _annotationName; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Creates a <see cref="PostgresEnum"/>. |
| 23 | + /// </summary> |
| 24 | + /// <param name="annotatable">The annotatable to search for the annotation.</param> |
| 25 | + /// <param name="annotationName">The annotation name to search for in the annotatable.</param> |
| 26 | + /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception> |
| 27 | + /// <exception cref="ArgumentNullException"><paramref name="annotationName"/></exception> |
| 28 | + internal PostgresEnum([NotNull] IAnnotatable annotatable, [NotNull] string annotationName) |
18 | 29 | { |
19 | | - _annotatable = annotatable; |
20 | | - _annotationName = annotationName; |
| 30 | + _annotatable = Check.NotNull(annotatable, nameof(annotatable)); |
| 31 | + _annotationName = Check.NotNull(annotationName, nameof(annotationName)); |
21 | 32 | } |
22 | 33 |
|
| 34 | + /// <summary> |
| 35 | + /// Gets or adds a <see cref="PostgresEnum"/> from or to the <see cref="IMutableAnnotatable"/>. |
| 36 | + /// </summary> |
| 37 | + /// <param name="annotatable">The annotatable from which to get or add the enum.</param> |
| 38 | + /// <param name="schema">The enum schema or null to use the model's default schema.</param> |
| 39 | + /// <param name="name">The enum name.</param> |
| 40 | + /// <param name="labels">The enum labels.</param> |
| 41 | + /// <returns> |
| 42 | + /// The <see cref="PostgresEnum"/> from the <see cref="IMutableAnnotatable"/>. |
| 43 | + /// </returns> |
| 44 | + /// <exception cref="ArgumentException"><paramref name="schema"/></exception> |
| 45 | + /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception> |
| 46 | + /// <exception cref="ArgumentNullException"><paramref name="name"/></exception> |
| 47 | + /// <exception cref="ArgumentNullException"><paramref name="labels"/></exception> |
| 48 | + [NotNull] |
23 | 49 | public static PostgresEnum GetOrAddPostgresEnum( |
24 | 50 | [NotNull] IMutableAnnotatable annotatable, |
25 | 51 | [CanBeNull] string schema, |
26 | 52 | [NotNull] string name, |
27 | 53 | [NotNull] string[] labels) |
28 | 54 | { |
| 55 | + Check.NotNull(annotatable, nameof(annotatable)); |
| 56 | + Check.NullButNotEmpty(schema, nameof(schema)); |
| 57 | + Check.NotEmpty(name, nameof(name)); |
| 58 | + Check.NotNull(labels, nameof(labels)); |
| 59 | + |
29 | 60 | if (FindPostgresEnum(annotatable, schema, name) is PostgresEnum enumType) |
30 | 61 | return enumType; |
31 | 62 |
|
32 | | - enumType = new PostgresEnum(annotatable, BuildAnnotationName(schema, name)); |
33 | | - enumType.SetData(labels); |
34 | | - return enumType; |
| 63 | + var annotationName = BuildAnnotationName(schema, name); |
| 64 | + |
| 65 | + return new PostgresEnum(annotatable, annotationName) { Labels = labels }; |
35 | 66 | } |
36 | 67 |
|
| 68 | + /// <summary> |
| 69 | + /// Gets or adds a <see cref="PostgresEnum"/> from or to the <see cref="IMutableAnnotatable"/>. |
| 70 | + /// </summary> |
| 71 | + /// <param name="annotatable">The annotatable from which to get or add the enum.</param> |
| 72 | + /// <param name="name">The enum name.</param> |
| 73 | + /// <param name="labels">The enum labels.</param> |
| 74 | + /// <returns> |
| 75 | + /// The <see cref="PostgresEnum"/> from the <see cref="IMutableAnnotatable"/>. |
| 76 | + /// </returns> |
| 77 | + /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception> |
| 78 | + /// <exception cref="ArgumentNullException"><paramref name="name"/></exception> |
| 79 | + /// <exception cref="ArgumentNullException"><paramref name="labels"/></exception> |
| 80 | + [NotNull] |
37 | 81 | public static PostgresEnum GetOrAddPostgresEnum( |
38 | 82 | [NotNull] IMutableAnnotatable annotatable, |
39 | 83 | [NotNull] string name, |
40 | 84 | [NotNull] string[] labels) |
41 | 85 | => GetOrAddPostgresEnum(annotatable, null, name, labels); |
42 | 86 |
|
| 87 | + /// <summary> |
| 88 | + /// Finds a <see cref="PostgresEnum"/> in the <see cref="IAnnotatable"/>, or returns null if not found. |
| 89 | + /// </summary> |
| 90 | + /// <param name="annotatable">The annotatable to search for the enum.</param> |
| 91 | + /// <param name="schema">The enum schema or null to use the model's default schema.</param> |
| 92 | + /// <param name="name">The enum name.</param> |
| 93 | + /// <returns> |
| 94 | + /// The <see cref="PostgresEnum"/> from the <see cref="IAnnotatable"/>. |
| 95 | + /// </returns> |
| 96 | + /// <exception cref="ArgumentException"><paramref name="schema"/></exception> |
| 97 | + /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception> |
| 98 | + /// <exception cref="ArgumentNullException"><paramref name="name"/></exception> |
| 99 | + [CanBeNull] |
43 | 100 | public static PostgresEnum FindPostgresEnum( |
44 | 101 | [NotNull] IAnnotatable annotatable, |
45 | 102 | [CanBeNull] string schema, |
46 | 103 | [NotNull] string name) |
47 | 104 | { |
48 | 105 | Check.NotNull(annotatable, nameof(annotatable)); |
| 106 | + Check.NullButNotEmpty(schema, nameof(schema)); |
49 | 107 | Check.NotEmpty(name, nameof(name)); |
50 | 108 |
|
51 | 109 | var annotationName = BuildAnnotationName(schema, name); |
52 | 110 |
|
53 | 111 | return annotatable[annotationName] == null ? null : new PostgresEnum(annotatable, annotationName); |
54 | 112 | } |
55 | 113 |
|
| 114 | + [NotNull] |
56 | 115 | static string BuildAnnotationName(string schema, string name) |
57 | | - => NpgsqlAnnotationNames.EnumPrefix + (schema == null ? name : schema + '.' + name); |
58 | | - |
| 116 | + => schema != null |
| 117 | + ? $"{NpgsqlAnnotationNames.EnumPrefix}{schema}.{name}" |
| 118 | + : $"{NpgsqlAnnotationNames.EnumPrefix}{name}"; |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets the collection of <see cref="PostgresEnum"/> stored in the <see cref="IAnnotatable"/>. |
| 122 | + /// </summary> |
| 123 | + /// <param name="annotatable">The annotatable to search for <see cref="PostgresEnum"/> annotations.</param> |
| 124 | + /// <returns> |
| 125 | + /// The collection of <see cref="PostgresEnum"/> stored in the <see cref="IAnnotatable"/>. |
| 126 | + /// </returns> |
| 127 | + /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception> |
| 128 | + [NotNull] |
| 129 | + [ItemNotNull] |
59 | 130 | public static IEnumerable<PostgresEnum> GetPostgresEnums([NotNull] IAnnotatable annotatable) |
60 | | - { |
61 | | - Check.NotNull(annotatable, nameof(annotatable)); |
62 | | - |
63 | | - return annotatable.GetAnnotations() |
64 | | - .Where(a => a.Name.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)) |
65 | | - .Select(a => new PostgresEnum(annotatable, a.Name)); |
66 | | - } |
67 | | - |
| 131 | + => Check.NotNull(annotatable, nameof(annotatable)) |
| 132 | + .GetAnnotations() |
| 133 | + .Where(a => a.Name.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)) |
| 134 | + .Select(a => new PostgresEnum(annotatable, a.Name)); |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// The <see cref="Annotatable"/> that stores the enum. |
| 138 | + /// </summary> |
| 139 | + [NotNull] |
68 | 140 | public Annotatable Annotatable => (Annotatable)_annotatable; |
69 | 141 |
|
| 142 | + /// <summary> |
| 143 | + /// The enum schema or null to represent the default schema. |
| 144 | + /// </summary> |
| 145 | + [CanBeNull] |
70 | 146 | public string Schema => GetData().Schema; |
71 | 147 |
|
| 148 | + /// <summary> |
| 149 | + /// The enum name. |
| 150 | + /// </summary> |
| 151 | + [NotNull] |
72 | 152 | public string Name => GetData().Name; |
73 | 153 |
|
74 | | - public string[] Labels |
| 154 | + /// <summary> |
| 155 | + /// The enum labels. |
| 156 | + /// </summary> |
| 157 | + [NotNull] |
| 158 | + public IReadOnlyList<string> Labels |
75 | 159 | { |
76 | 160 | get => GetData().Labels; |
77 | 161 | set => SetData(value); |
78 | 162 | } |
79 | 163 |
|
80 | 164 | (string Schema, string Name, string[] Labels) GetData() |
81 | | - { |
82 | | - return !(Annotatable[_annotationName] is string annotationValue) |
83 | | - ? (null, null, null) |
84 | | - : Deserialize(_annotationName, annotationValue); |
85 | | - } |
| 165 | + => Deserialize(Annotatable.FindAnnotation(_annotationName)); |
86 | 166 |
|
87 | | - void SetData(string[] labels) |
| 167 | + void SetData([NotNull] IEnumerable<string> labels) |
88 | 168 | => Annotatable[_annotationName] = string.Join(",", labels); |
89 | 169 |
|
90 | | - static (string schema, string name, string[] labels) Deserialize( |
91 | | - [NotNull] string annotationName, |
92 | | - [NotNull] string annotationValue) |
| 170 | + static (string Schema, string Name, string[] Labels) Deserialize([CanBeNull] IAnnotation annotation) |
93 | 171 | { |
94 | | - Check.NotEmpty(annotationValue, nameof(annotationValue)); |
| 172 | + if (annotation == null || !(annotation.Value is string value) || string.IsNullOrEmpty(value)) |
| 173 | + return (null, null, null); |
95 | 174 |
|
96 | | - var labels = annotationValue.Split(',').ToArray(); |
| 175 | + var labels = value.Split(','); |
97 | 176 |
|
| 177 | + // TODO: This would be a safer operation if we stored schema and name in the annotation value (see Sequence.cs). |
98 | 178 | // Yes, this doesn't support dots in the schema/enum name, let somebody complain first. |
99 | | - var schemaAndName = annotationName.Substring(NpgsqlAnnotationNames.EnumPrefix.Length).Split('.'); |
| 179 | + var schemaAndName = annotation.Name.Substring(NpgsqlAnnotationNames.EnumPrefix.Length).Split('.'); |
100 | 180 | switch (schemaAndName.Length) |
101 | 181 | { |
102 | 182 | case 1: |
103 | 183 | return (null, schemaAndName[0], labels); |
104 | 184 | case 2: |
105 | 185 | return (schemaAndName[0], schemaAndName[1], labels); |
106 | 186 | default: |
107 | | - throw new ArgumentException("Cannot parse enum name from annotation: " + annotationName); |
| 187 | + throw new ArgumentException($"Cannot parse enum name from annotation: {annotation.Name}"); |
108 | 188 | } |
109 | 189 | } |
110 | 190 | } |
|
0 commit comments