Skip to content

Conversation

@carlosjdelgado
Copy link
Contributor

With this change I try to fix the following case:

 return new KrpcQuery
 {
         Id = queryDictionary.Get<BString>("id")?.ToString(),
         InfoHash = queryDictionary.Get<BString>("info_hash")?.ToString(),
         Target = queryDictionary.Get<BString>("target")?.ToString(),
         ImpliedPort = queryDictionary.Get<BNumber>("implied_port"), // ImpliedPort is an int?
         Token = queryDictionary.Get<BString>("token")?.ToString()
 };

This throws InvalidCastException, when try to get "implied_port" key, I want to get null if does not exist in the dictionary.

With this change I try to fix the following case:
```
            return new KrpcQuery
            {
                Id = queryDictionary.Get<BString>("id")?.ToString(),
                InfoHash = queryDictionary.Get<BString>("info_hash")?.ToString(),
                Target = queryDictionary.Get<BString>("target")?.ToString(),
                ImpliedPort = queryDictionary.Get<BNumber>("implied_port"), // ImpliedPort is an int?
                Token = queryDictionary.Get<BString>("token")?.ToString()
            };
```
This throws InvalidCastException, when try to get "implied_port" key, I want to get null if does not exist in the dictionary.
@coveralls
Copy link

coveralls commented Feb 8, 2019

Coverage Status

Coverage increased (+0.3%) to 77.633% when pulling 81f38a5 on carlosjdelgado:add-nullable-bnumber-castings into fe550e9 on Krusen:master.

@Krusen
Copy link
Owner

Krusen commented Feb 10, 2019

Thanks for the PR.

Your tests doesn't actually test your added code but instead tests casting from int?/long? to BNumber.

If you can change the tests to the following I can get this merged :)

[Fact]
public void CanCastToNullableInt_WhenNull()
{
    BNumber bnumber = null;
    var number = (int?) bnumber;
    number.Should().BeNull();
}

[Fact]
public void CanCastToNullableLong_WhenNull()
{
    BNumber bnumber = null;
    var number = (long?) bnumber;
    number.Should().BeNull();
}

[Fact]
public void CanCastToNullableInt_WhenNotNull()
{
    var bnumber = new BNumber(42);
    var number = (int?) bnumber;
    number.Should().Be(42);
}

[Fact]
public void CanCastToNullableLong_WhenNotNull()
{
    var bnumber = new BNumber(42);
    var number = (long?) bnumber;
    number.Should().Be(42);
}

@carlosjdelgado
Copy link
Contributor Author

What a mistake 😌, I'll fix it as soon as possible, thanks!

@carlosjdelgado
Copy link
Contributor Author

carlosjdelgado commented Feb 11, 2019

@Krusen I had fixed tests covering all cases (casting from and to int, long, nullable int and nullable long)

Will it be possible to have a new nuget version with these changes?

@Krusen Krusen changed the title Added casting operators for int? and long? Add casting operators for int? and long? Feb 11, 2019
@Krusen Krusen merged commit 867e120 into Krusen:master Feb 11, 2019
@Krusen
Copy link
Owner

Krusen commented Feb 11, 2019

Thanks!

Version 2.3.0 has been published.

@carlosjdelgado carlosjdelgado deleted the add-nullable-bnumber-castings branch February 11, 2019 18:50
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.

4 participants