Skip to content

Add support to parse IP and MAC addresses - #535

Merged
austindrenski merged 2 commits into
npgsql:devfrom
austindrenski:network-parse-translations
Jul 23, 2018
Merged

austindrenski merged 2 commits into
npgsql:devfrom
austindrenski:network-parse-translations

Conversation

@austindrenski

Copy link
Copy Markdown
Contributor
  • IPAddress.Parse(string)
  • PhysicalAddres.Parse(string)

Related

#534

- `IPAddress.Parse(string)`
- `PhysicalAddres.Parse(string)`
public Expression Translate(MethodCallExpression expression)
{
if (expression.Method.DeclaringType != typeof(NpgsqlNetworkExtensions))
var declaringType = expression.Method.DeclaringType;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it if we handled the IPAddress and PhysicalAddress methods separately, rather than include them in the big switch below. As it is, in theory, if a method called LessThan (or any other method below) existed on IPAddress or PhysicalAddress, it would get mistakenly translated. So I'd first take care of the Parse() cases, and then move ahead as before to the switch over all NpgsqlNetworkExtensions methods.

I do admit this is probably a bit of theoretical OCD on my side :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea. I'd prefer to still use one translator class though, but with the Parse(string) handling separated from the main switch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, no need for multiple translators.

public NpgsqlEvaluatableExpressionFilter([NotNull] IModel model) : base(model) {}

/// <inheritdoc />
public override bool IsEvaluatableMethodCall(MethodCallExpression methodCallExpression)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained in #534 (comment), we shouldn't be touching NpgsqlEvaluatableExpressionFilter at all.

switch (expression.Method.Name)
{
case nameof(IPAddress.Parse) when declaringType == typeof(IPAddress):
return new ExplicitCastExpression(expression.Arguments[0], typeof(IPAddress));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing the OCD mood from above, if the expression is a constant we may want to use the literal PostgreSQL syntax: IPADDR '127.0.0.1' rater than the cast syntax CAST('127.0.0.1' AS IPADDR). The former is cleaner SQL and clearly indicates the constant-ness in the expression (and therefore cannot also be parameterized). You can use SqlFragmentExpression to easily generate it.

However, this is really a nit and we don't have to do it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't actually have a chance to do this, per #534 (comment). Though, I like the idea about SqlFragmentExpression.


/// <inheritdoc />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> base.OnConfiguring(optionsBuilder.EnableSensitiveDataLogging());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what is the need for this? You don't seem to actually be looking at parameter data in these tests...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of the panic, I was looking at the values being assigned to the parameters. Reverting this in the next commit.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! See my tiny nit and merge at will... Thanks!

public Expression Translate(MethodCallExpression expression)
{
if (expression.Method.DeclaringType != typeof(NpgsqlNetworkExtensions))
MethodInfo method = expression.Method;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use var

@austindrenski
austindrenski force-pushed the network-parse-translations branch from 07a3f3d to f4aaec8 Compare July 23, 2018 21:55
@austindrenski
austindrenski merged commit 00c3ee1 into npgsql:dev Jul 23, 2018
@austindrenski
austindrenski deleted the network-parse-translations branch July 23, 2018 21:56
@austindrenski austindrenski self-assigned this Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants