Skip to content
This repository was archived by the owner on Oct 6, 2022. It is now read-only.

Commit 148488f

Browse files
committed
Add INamingStrategy for implicit property names
1 parent e3c7d14 commit 148488f

File tree

14 files changed

+227
-49
lines changed

14 files changed

+227
-49
lines changed

docs/configuring.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ with the ``EdgeDBConfig`` class like so:
4040
- The amount of miliseconds to wait for a message to be received.
4141
* - RetryMode
4242
- ``ConnectionRetryMode``
43-
- The retry mode for when a connection can't be established
43+
- The retry mode for when a connection can't be established
44+
* - ExplicitObjectIds
45+
- ``bool``
46+
- Whether or not to always return object ids.
47+
* - ImplicitTypeNames
48+
- ``bool``
49+
- Whether or not to implicitly return type names.
50+
* - ImplicitTypeIds
51+
- ``bool``
52+
- Whether or not to implicitly return type ids.
53+
* - SerializerNamingStrategy
54+
- ``INamingStrategy``
55+
- The default naming strategy for the type builder.

docs/datatypes.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ We can write at class that represents person as follows:
7474
7575
public class Person
7676
{
77-
[EdgeDBProperty("name")]
7877
public string? Name { get; set; }
7978
80-
[EdgeDBProperty("email")]
8179
public string? Email { get; set; }
8280
}
8381
8482
.. note::
8583

86-
Since the naming convention of properties is diffent from the EdgeDB naming convention,
87-
you can specify the name with the ``EdgeDBProperty`` attribute. You can also specify
88-
the name of the type with the ``EdgeDBType`` attribute.
84+
Since the naming convention of properties is diffent from the EdgeDB naming convention,
85+
The class responsible for deserializing the schema type will use a ``INamingStrategy``
86+
to map the EdgeDB properties to the dotnet properties. You can change the default naming
87+
strategy in the config. You can also specify the name with the ``EdgeDBProperty`` attribute.
88+
You can also specify the name of the type with the ``EdgeDBType`` attribute.
8989

9090
You can find an example with custom types `here <https://github.com/quinchs/EdgeDB.Net/blob/dev/examples/EdgeDB.ExampleApp/Examples/QueryResults.cs>`_
9191

examples/EdgeDB.Examples.ExampleApp/Examples/CustomDeserializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.Logging;
1+
using EdgeDB.Serializer;
2+
using Microsoft.Extensions.Logging;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;

examples/EdgeDB.Examples.ExampleApp/Examples/QueryResults.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public class QueryResults : IExample
2323
// We have a type called 'Person' with a few properties, we can create a class that resembles this object like so
2424
public class Person
2525
{
26-
[EdgeDBProperty("name")]
2726
public string? Name { get; set; }
28-
29-
[EdgeDBProperty("email")]
3027
public string? Email { get; set; }
3128
}
3229

examples/EdgeDB.Examples.ExampleApp/Program.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@
2020
loggingBuilder.AddSerilog(dispose: true);
2121
});
2222

23-
services.AddSingleton((provider) =>
24-
{
25-
return new EdgeDBClient(new EdgeDBClientPoolConfig
26-
{
27-
Logger = provider.GetService<ILoggerFactory>()!.CreateLogger("EdgeDB"),
28-
ExplicitObjectIds = true,
29-
});
30-
});
23+
services.AddEdgeDB();
3124

3225
services.AddSingleton<ExampleRunner>();
3326
}).Build();

src/EdgeDB.Net.Driver/EdgeDBConfig.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.Logging;
1+
using EdgeDB.Serializer;
2+
using Microsoft.Extensions.Logging;
23

34
namespace EdgeDB
45
{
@@ -105,5 +106,17 @@ public class EdgeDBConfig
105106
/// (equivalent to having an implicit “__tid__ := .__type__.id” computed property.)
106107
/// </remarks>
107108
public bool ImplicitTypeIds { get; set; }
109+
110+
/// <summary>
111+
/// Gets or sets the default naming strategy for the type builder.
112+
/// </summary>
113+
/// <remarks>
114+
/// See <seealso cref="TypeBuilder.NamingStrategy"/>
115+
/// </remarks>
116+
public INamingStrategy SerializerNamingStrategy
117+
{
118+
get => TypeBuilder.NamingStrategy;
119+
set => TypeBuilder.NamingStrategy = value;
120+
}
108121
}
109122
}

src/EdgeDB.Net.Driver/Extensions/EdgeDBHostingExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -13,12 +14,21 @@ public static IServiceCollection AddEdgeDB(this IServiceCollection collection, E
1314
Action<EdgeDBClientPoolConfig>? configure = null)
1415
{
1516
var conn = connection ?? EdgeDBConnection.ResolveConnection();
16-
var config = new EdgeDBClientPoolConfig();
17-
configure?.Invoke(config);
1817

1918
collection.AddSingleton(conn);
20-
collection.AddSingleton(config);
19+
collection.AddSingleton<EdgeDBClientPoolConfig>((provider) =>
20+
{
21+
var config = new EdgeDBClientPoolConfig();
22+
configure?.Invoke(config);
23+
24+
if (config.Logger is null)
25+
{
26+
config.Logger = provider.GetService<ILoggerFactory>()?.CreateLogger("EdgeDB");
27+
}
28+
return config;
29+
});
2130
collection.AddSingleton<EdgeDBClient>();
31+
2232
return collection;
2333
}
2434
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace EdgeDB.Serializer
9+
{
10+
public sealed class AttributeNamingStrategy : INamingStrategy
11+
{
12+
public string GetName(PropertyInfo property)
13+
{
14+
return property.GetCustomAttribute<EdgeDBPropertyAttribute>()?.Name ?? property.Name;
15+
}
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace EdgeDB.Serializer
9+
{
10+
public sealed class CamelCaseNamingStrategy : INamingStrategy
11+
{
12+
public string GetName(PropertyInfo property)
13+
{
14+
var name = property.Name;
15+
return $"{char.ToLowerInvariant(name[0])}{name[1..].Replace("_", string.Empty)}";
16+
}
17+
}
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace EdgeDB.Serializer
9+
{
10+
/// <summary>
11+
/// Represents an abstract naming strategy used to convert property names within
12+
/// a dotnet type to a name within a schema file.
13+
/// </summary>
14+
public interface INamingStrategy
15+
{
16+
/// <summary>
17+
/// Gets the attribute-based naming strategy.
18+
/// </summary>
19+
public static INamingStrategy AttributeNamingStrategy
20+
=> new AttributeNamingStrategy();
21+
22+
/// <summary>
23+
/// Gets the 'camelCase' naming strategy.
24+
/// </summary>
25+
public static INamingStrategy CamelCaseNamingStrategy
26+
=> new CamelCaseNamingStrategy();
27+
28+
/// <summary>
29+
/// Gets the 'PascalCase' naming strategy.
30+
/// </summary>
31+
public static INamingStrategy PascalNamingStrategy
32+
=> new PascalNamingStrategy();
33+
34+
/// <summary>
35+
/// Gets the 'snake-case' naming strategy.
36+
/// </summary>
37+
/// <remarks>
38+
/// This is the default naming strategy for the <see cref="TypeBuilder"/>.
39+
/// </remarks>
40+
public static INamingStrategy SnakeCaseNamingStrategy
41+
=> new SnakeCaseNamingStrategy();
42+
43+
/// <summary>
44+
/// Gets the name defined in the objects schema given the types property info.
45+
/// </summary>
46+
/// <param name="property">The property info of which to convert its name.</param>
47+
/// <returns>The name defined in the schema.</returns>
48+
public string GetName(PropertyInfo property);
49+
}
50+
}

0 commit comments

Comments
 (0)