Skip to content

Commit 8f4e119

Browse files
CornielCorniel Nobel
andauthored
CSHARP-4689: MongoUrl should have a registered TypeConverter
Co-authored-by: Corniel Nobel <[email protected]>
1 parent f0fdba6 commit 8f4e119

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/MongoDB.Driver/MongoUrl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.ComponentModel;
1819
using System.Linq;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -29,6 +30,7 @@ namespace MongoDB.Driver
2930
/// Represents an immutable URL style connection string. See also MongoUrlBuilder.
3031
/// </summary>
3132
[Serializable]
33+
[TypeConverter(typeof(MongoUrlTypeConverter))]
3234
public class MongoUrl : IEquatable<MongoUrl>
3335
{
3436
// private static fields
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.ComponentModel;
18+
using System.Globalization;
19+
20+
namespace MongoDB.Driver
21+
{
22+
/// <summary>Implements a <see cref="TypeConverter"/> for converting <see cref="MongoUrl"/>.</summary>
23+
public sealed class MongoUrlTypeConverter : TypeConverter
24+
{
25+
/// <inheritdoc />
26+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
27+
=> sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
28+
29+
/// <inheritdoc />
30+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
31+
=> value is string str
32+
? new MongoUrl(str)
33+
: base.ConvertFrom(context, culture, value);
34+
}
35+
}

tests/MongoDB.Driver.Tests/MongoUrlTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.ComponentModel;
1819
using System.Threading.Tasks;
1920
using FluentAssertions;
2021
using MongoDB.Bson;
2122
using MongoDB.Bson.TestHelpers;
22-
using MongoDB.TestHelpers.XunitExtensions;
2323
using MongoDB.Driver.Core.Clusters;
2424
using MongoDB.Driver.Core.Compression;
2525
using MongoDB.Driver.Core.Configuration;
26+
using MongoDB.TestHelpers.XunitExtensions;
2627
using Xunit;
2728

2829
namespace MongoDB.Driver.Tests
@@ -457,6 +458,34 @@ public void TestSrvMaxHosts()
457458
subject.SrvMaxHosts.Should().Be(2);
458459
}
459460

461+
[Fact]
462+
public void Decorated_with_TypeConverter()
463+
{
464+
var converter = TypeDescriptor.GetConverter(typeof(MongoUrl));
465+
466+
converter.Should().BeOfType<MongoUrlTypeConverter>();
467+
}
468+
469+
[Fact]
470+
public void Convert_from_string_should_work()
471+
{
472+
var converter = TypeDescriptor.GetConverter(typeof(MongoUrl));
473+
474+
var result = converter.ConvertFrom("mongodb://localhost");
475+
476+
result.Should().Be(new MongoUrl("mongodb://localhost"));
477+
}
478+
479+
[Fact]
480+
public void Convert_to_string_should_work()
481+
{
482+
var converter = TypeDescriptor.GetConverter(typeof(MongoUrl));
483+
484+
var result = converter.ConvertTo(new MongoUrl("mongodb://localhost"), typeof(string));
485+
486+
result.Should().Be("mongodb://localhost");
487+
}
488+
460489
// private methods
461490
private IEnumerable<MongoUrl> EnumerateBuiltAndParsedUrls(
462491
MongoUrlBuilder built,

0 commit comments

Comments
 (0)