• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / NLog.Mongo / 11217039547

07 Oct 2024 01:52PM UTC coverage: 61.67%. Remained the same
11217039547

push

github

web-flow
Create FUNDING.yml

125 of 230 branches covered (54.35%)

Branch coverage included in aggregate %.

200 of 297 relevant lines covered (67.34%)

21.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

26.19
/src/NLog.Mongo/MongoConvert.cs
1
using System;
2
using MongoDB.Bson;
3

4
namespace NLog.Mongo
5
{
6
    /// <summary>
7
    /// Convert string values to Mongo <see cref="BsonValue"/>.
8
    /// </summary>
9
    public static class MongoConvert
10
    {
11
        /// <summary>Try to convert the string to a <see cref="BsonBoolean"/>.</summary>
12
        /// <param name="value">The value to convert.</param>
13
        /// <param name="bsonValue">The BsonValue result.</param>
14
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
15
        public static bool TryBoolean(this string value, out BsonValue bsonValue)
16
        {
17
            bsonValue = new BsonBoolean(false);
×
18

19
            if (value == null)
×
20
                return false;
×
21

22
            bool result;
23
            if (bool.TryParse(value, out result))
×
24
            {
25
                bsonValue = new BsonBoolean(result);
×
26
                return true;
×
27
            }
28

29
            string v = value.Trim();
×
30

31
            if (string.Equals(v, "t", StringComparison.OrdinalIgnoreCase)
×
32
                || string.Equals(v, "true", StringComparison.OrdinalIgnoreCase)
×
33
                || string.Equals(v, "y", StringComparison.OrdinalIgnoreCase)
×
34
                || string.Equals(v, "yes", StringComparison.OrdinalIgnoreCase)
×
35
                || string.Equals(v, "1", StringComparison.OrdinalIgnoreCase)
×
36
                || string.Equals(v, "x", StringComparison.OrdinalIgnoreCase)
×
37
                || string.Equals(v, "on", StringComparison.OrdinalIgnoreCase))
×
38
                bsonValue = new BsonBoolean(true);
×
39

40
            return true;
×
41
        }
42

43
        /// <summary>Try to convert the string to a <see cref="BsonDateTime"/>.</summary>
44
        /// <param name="value">The value to convert.</param>
45
        /// <param name="bsonValue">The BsonValue result.</param>
46
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
47
        public static bool TryDateTime(this string value, out BsonValue bsonValue)
48
        {
49
            bsonValue = null;
9✔
50
            if (value == null)
9!
51
                return false;
×
52

53
            DateTime result;
54
            var r = DateTime.TryParse(value, out result);
9✔
55
            if (r) bsonValue = new BsonDateTime(result);
18✔
56

57
            return r;
9✔
58
        }
59

60
        /// <summary>Try to convert the string to a <see cref="BsonDouble"/>.</summary>
61
        /// <param name="value">The value to convert.</param>
62
        /// <param name="bsonValue">The BsonValue result.</param>
63
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
64
        public static bool TryDouble(this string value, out BsonValue bsonValue)
65
        {
66
            bsonValue = null;
×
67
            if (value == null)
×
68
                return false;
×
69

70
            double result;
71
            var r = double.TryParse(value, out result);
×
72
            if (r) bsonValue = new BsonDouble(result);
×
73

74
            return r;
×
75
        }
76

77
        /// <summary>Try to convert the string to a <see cref="BsonInt32"/>.</summary>
78
        /// <param name="value">The value to convert.</param>
79
        /// <param name="bsonValue">The BsonValue result.</param>
80
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
81
        public static bool TryInt32(this string value, out BsonValue bsonValue)
82
        {
83
            bsonValue = null;
54✔
84
            if (value == null)
54!
85
                return false;
×
86

87
            int result;
88
            var r = int.TryParse(value, out result);
54✔
89
            if (r) bsonValue = new BsonInt32(result);
108✔
90

91
            return r;
54✔
92
        }
93

94
        /// <summary>Try to convert the string to a <see cref="BsonInt64"/>.</summary>
95
        /// <param name="value">The value to convert.</param>
96
        /// <param name="bsonValue">The BsonValue result.</param>
97
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
98
        public static bool TryInt64(this string value, out BsonValue bsonValue)
99
        {
100
            bsonValue = null;
×
101
            if (value == null)
×
102
                return false;
×
103

104
            long result;
105
            var r = long.TryParse(value, out result);
×
106
            if (r) bsonValue = new BsonInt64(result);
×
107

108
            return r;
×
109
        }
110

111
        /// <summary>Try to convert the string to a <see cref="BsonDocument"/>.</summary>
112
        /// <param name="value">The value to convert.</param>
113
        /// <param name="bsonValue">The BsonValue result.</param>
114
        /// <returns><c>true</c> if the value was converted; otherwise <c>false</c>.</returns>
115
        /// <remarks>
116
        /// BsonDocument will by default convert normal Json-formatted DateTime-/DateTimerOffset-values as BsonString-values.
117
        /// BsonDocument has special requirements for recognizing DateTime-/DateTimerOffset-values.
118
        /// </remarks>
119
        public static bool TryJsonObject(this string value, out BsonValue bsonValue)
120
        {
121
            bsonValue = null;
9✔
122
            if (value == null)
9!
123
                return false;
×
124

125
            try
126
            {
127
                bsonValue = BsonDocument.Parse(value);
9✔
128
                return bsonValue != null;
9✔
129
            }
130
            catch
×
131
            {
132
                return false;
×
133
            }
134
        }
9✔
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc