Skip to content

Commit fecef0f

Browse files
committed
Add IgnoredAttribute, update errors and error messages
1 parent 0826ef4 commit fecef0f

4 files changed

Lines changed: 12 additions & 10 deletions

File tree

PowerSync/PowerSync.Common/DB/Schema/Attributes/AttributeParser.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ public Dictionary<string, ColumnType> ParseColumns()
4141

4242
foreach (var prop in _type.GetProperties())
4343
{
44+
if (prop.GetCustomAttribute<IgnoredAttribute>() != null) continue;
45+
4446
var columnAttr = prop.GetCustomAttribute<ColumnAttribute>();
4547
var columnName = columnAttr?.Name ?? prop.Name;
4648

4749
// Handle 'id' field separately
48-
// TODO prevent defining multiple id columns (eg. 'id', 'Id', 'ID')
4950
if (columnName.ToLowerInvariant() == "id")
5051
{
52+
if (idProperty != null)
53+
{
54+
throw new InvalidOperationException($"Cannot define multiple ID columns for table '{_tableAttr.Name}'.");
55+
}
5156
idProperty = prop;
5257
continue;
5358
}
@@ -64,11 +69,11 @@ public Dictionary<string, ColumnType> ParseColumns()
6469
// Validate 'id' property exists and is a string
6570
if (idProperty == null)
6671
{
67-
throw new InvalidOperationException("A public string 'id' property is required.");
72+
throw new InvalidOperationException($"An 'id' property is required for table '{_tableAttr.Name}'.");
6873
}
6974
if (idProperty.PropertyType != typeof(string))
7075
{
71-
throw new InvalidOperationException($"Property '{idProperty.Name}' must be of type string.");
76+
throw new InvalidOperationException($"ID Property '{idProperty.Name}' must be of type string.");
7277
}
7378
var idAttr = idProperty.GetCustomAttribute<ColumnAttribute>();
7479
if (idAttr != null)
@@ -78,7 +83,7 @@ public Dictionary<string, ColumnType> ParseColumns()
7883
{
7984
throw new InvalidOperationException
8085
(
81-
$"Property '{idProperty.Name}' must have ColumnType set to either ColumnType.Text or ColumnType.Inferred."
86+
$"ID Property '{idProperty.Name}' must have ColumnType set to ColumnType.Text or ColumnType.Inferred."
8287
);
8388
}
8489
}
@@ -135,8 +140,7 @@ private ColumnType PropertyTypeToColumnType(Type propertyType)
135140
_ when propertyType == typeof(double) => ColumnType.Real,
136141

137142
// Fallback
138-
// TODO: Maybe raise a console warning / throw an error if unable to infer type?
139-
_ => ColumnType.Text
143+
_ => throw new InvalidOperationException($"Unable to automatically infer ColumnType of property type '{propertyType.Name}'."),
140144
};
141145
}
142146

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace PowerSync.Common.DB.Schema.Attributes;
22

3-
[AttributeUsage(AttributeTargets.Property)]
3+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
44
public class IgnoredAttribute : Attribute { }
55

PowerSync/PowerSync.Common/DB/Schema/Attributes/TableAttribute.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace PowerSync.Common.DB.Schema.Attributes;
33
[Flags]
44
public enum TrackPrevious
55
{
6-
// TODO: Consider finding a method that can't represent invalid states (eg. TrackPrevious.Table | TrackPrevious.Columns)
76
None = 0,
87
Table = 1 << 0,
98
Columns = 1 << 1,

PowerSync/PowerSync.Common/DB/Schema/ColumnJSON.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class ColumnJSON(ColumnJSONOptions options)
2626

2727
public object ToJSONObject()
2828
{
29-
// TODO find good exception type / message, or catch/throw somewhere else
30-
if (Type == ColumnType.Inferred) throw new InvalidOperationException("Attempted to serialise Inferred column.");
29+
if (Type == ColumnType.Inferred) throw new InvalidOperationException("Attempted to serialise Inferred column. ColumnType.Inferred is only valid as an argument to ColumnAttribute.");
3130

3231
return new
3332
{

0 commit comments

Comments
 (0)