Skip to content

Commit

Permalink
Merge pull request #2179 from jaroslavbliznak/fix/2045-fixing-conditi…
Browse files Browse the repository at this point in the history
…ons-empty-strings-using-constants

FIX-2045 BE-fix conditions, empty strings and using constants
  • Loading branch information
jaroslavbliznak authored Dec 13, 2023
2 parents 286f4eb + bfe3599 commit 5c82937
Show file tree
Hide file tree
Showing 68 changed files with 315 additions and 276 deletions.
30 changes: 30 additions & 0 deletions Src/Witsml/CommonConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,35 @@ public static class CommonConstants
{
public const int DefaultNumberOfRoundedPlaces = 3;
public const string DataSeparator = ",";
public const string PropertySeparator = ".";
public const string NewLine = "\n";
public const int DefaultClientRequestTimeOutSeconds = 90;
public const int DefaultReloadIntervalMinutes = 15;

public static class DepthIndex
{
public const string DefaultUnit = Unit.Meter;
public const double NullValue = -999.25;
public const double OffsetEpsilon = 1e-3;
public const double Epsilon = 1e-5;
}

public static class DateTimeIndex
{
public const string IsoPattern = "yyyy-MM-ddTHH:mm:ss.fffZ";
public const string NullValue = "1900-01-01T00:00:00.000Z";
}

public static class TimeSpanIndex
{
public const string Pattern = @"hh\:mm\:ss";
}

public static class Unit
{
public const string Unitless = "unitless";
public const string Meter = "m";
public const string Feet = "ft";
public const string Second = "s";
}
}
11 changes: 4 additions & 7 deletions Src/Witsml/Data/Curves/DateTimeIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ namespace Witsml.Data.Curves
{
public class DateTimeIndex : Index
{

public DateTime Value { get; }
public const string IsoPattern = "yyyy-MM-ddTHH:mm:ss.fffZ";
public const string NullValue = "1900-01-01T00:00:00.000Z";

public DateTimeIndex(DateTime dateTime)
{
Expand All @@ -17,14 +14,14 @@ public DateTimeIndex(DateTime dateTime)

public static DateTimeIndex FromString(string dateString)
{
return DateTime.TryParseExact(dateString, IsoPattern, null, DateTimeStyles.None, out DateTime parsedDateTime)
return DateTime.TryParseExact(dateString, CommonConstants.DateTimeIndex.IsoPattern, null, DateTimeStyles.None, out DateTime parsedDateTime)
? new DateTimeIndex(parsedDateTime)
: throw new Exception($"Date format not recognized: {dateString}");
}

public static bool TryParseISODate(string dateString, out DateTimeIndex dateTimeIndex)
{
if (DateTime.TryParseExact(dateString, IsoPattern, null, DateTimeStyles.None, out DateTime tmpDateTime))
if (DateTime.TryParseExact(dateString, CommonConstants.DateTimeIndex.IsoPattern, null, DateTimeStyles.None, out DateTime tmpDateTime))
{
dateTimeIndex = new DateTimeIndex(tmpDateTime);
return true;
Expand All @@ -48,7 +45,7 @@ public override int CompareTo(Index that)

public override string GetValueAsString()
{
return Value.ToUniversalTime().ToString(IsoPattern, CultureInfo.InvariantCulture);
return Value.ToUniversalTime().ToString(CommonConstants.DateTimeIndex.IsoPattern, CultureInfo.InvariantCulture);
}

public override bool IsContinuous(Index that)
Expand All @@ -65,7 +62,7 @@ public override bool IsNegative()

public override bool IsNullValue()
{
return Value.Date.Equals(DateTime.Parse(NullValue));
return Value.Date.Equals(DateTime.Parse(CommonConstants.DateTimeIndex.NullValue));
}

public override string ToString()
Expand Down
10 changes: 3 additions & 7 deletions Src/Witsml/Data/Curves/DepthIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace Witsml.Data.Curves
{
public class DepthIndex : Index
{
private const double OffsetEpsilon = 1e-3;
private const double Epsilon = 1e-5;
public const double NullValue = -999.25;

public double Value { get; }
public DepthUnit Uom { get; }

Expand Down Expand Up @@ -40,7 +36,7 @@ public DepthIndex(int value)
private bool HasSameUnitAs(DepthIndex that) => Uom.Equals(that.Uom);

[Obsolete("AddEpsilon is deprecated due to assuming 3 decimals of precision for depth indexes. Some WITSML servers do not use 3 decimals.")]
public override Index AddEpsilon() => new DepthIndex(Value + OffsetEpsilon, Uom);
public override Index AddEpsilon() => new DepthIndex(Value + CommonConstants.DepthIndex.OffsetEpsilon, Uom);

private Index Subtract(Index that)
{
Expand All @@ -55,7 +51,7 @@ public override int CompareTo(Index that)
{
throw new ArgumentException("Cannot compare depths with different unit types");
}
var isEqual = Math.Abs(Value - thatDepthIndex.Value) < Epsilon;
var isEqual = Math.Abs(Value - thatDepthIndex.Value) < CommonConstants.DepthIndex.Epsilon;
return isEqual ? 0 : Value.CompareTo(thatDepthIndex.Value);
}

Expand All @@ -82,7 +78,7 @@ public override bool IsContinuous(Index that)
public override bool IsNegative() => Value <= 0.0;
public override bool IsNullValue()
{
return Math.Abs(Value - NullValue) < 0;
return Math.Abs(Value - CommonConstants.DepthIndex.NullValue) < 0;
}


Expand Down
8 changes: 4 additions & 4 deletions Src/Witsml/Data/Curves/DepthUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ namespace Witsml.Data.Curves
{
public class DepthUnit : Unit
{
public static readonly DepthUnit Meter = new("m");
public static readonly DepthUnit Feet = new("ft");
public static readonly DepthUnit Meter = new(CommonConstants.Unit.Meter);
public static readonly DepthUnit Feet = new(CommonConstants.Unit.Feet);

public DepthUnit(string unitCode) : base(unitCode) { }

public static DepthUnit FromString(string unitCode)
{
return "m".Equals(unitCode, StringComparison.InvariantCulture)
return CommonConstants.Unit.Meter.Equals(unitCode, StringComparison.InvariantCulture)
? Meter
: ("ft".Equals(unitCode, StringComparison.InvariantCulture)
: (CommonConstants.Unit.Feet.Equals(unitCode, StringComparison.InvariantCulture)
? Feet
: throw new ArgumentException($"Unit \"{unitCode}\" is not supported!"));
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Witsml/Data/Curves/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Index Start(WitsmlLog log, string customIndexValue = "")
: new DepthIndex(
double.Parse(customIndexValue.IsNumeric() ? customIndexValue : log.StartIndex.Value,
CultureInfo.InvariantCulture),
log.StartIndex?.Uom ?? "m"),
log.StartIndex?.Uom ?? CommonConstants.DepthIndex.DefaultUnit),
WitsmlLog.WITSML_INDEX_TYPE_DATE_TIME => log.StartDateTimeIndex == null && string.IsNullOrEmpty(customIndexValue)
? null
: new DateTimeIndex(DateTime.Parse(customIndexValue.NullIfEmpty() ?? log.StartDateTimeIndex,
Expand All @@ -77,7 +77,7 @@ public static Index End(WitsmlLog log, string customIndexValue = "")
: new DepthIndex(
double.Parse(customIndexValue.IsNumeric() ? customIndexValue : log.EndIndex.Value,
CultureInfo.InvariantCulture),
log.EndIndex?.Uom ?? "m"),
log.EndIndex?.Uom ?? CommonConstants.DepthIndex.DefaultUnit),
WitsmlLog.WITSML_INDEX_TYPE_DATE_TIME => log.EndDateTimeIndex == null && string.IsNullOrEmpty(customIndexValue)
? null
: new DateTimeIndex(DateTime.Parse(customIndexValue.NullIfEmpty() ?? log.EndDateTimeIndex,
Expand Down
7 changes: 5 additions & 2 deletions Src/Witsml/Data/Curves/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Point

public Point(string commaSeparated)
{
string[] values = commaSeparated.Split(",");
string[] values = commaSeparated.Split(CommonConstants.DataSeparator);
Index = DateTimeIndex.TryParseISODate(values.First(), out DateTimeIndex witsmlDateTime)
? witsmlDateTime
: new DepthIndex(double.Parse(values.First(), CultureInfo.InvariantCulture));
Expand All @@ -26,7 +26,10 @@ public string GetValueAsString()

public int GetValueAsInt()
{
return Value is DoubleValue value ? (int)value.Get() : throw new InvalidCastException("Unable to cast String values to integers");
return Value is DoubleValue value
? (int)value.Get()
: throw new InvalidCastException(
"Unable to cast String values to integers");
}

public double GetValueAsDouble()
Expand Down
2 changes: 1 addition & 1 deletion Src/Witsml/Data/Curves/Row.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Row

public Row(string commaSeparated)
{
string[] row = commaSeparated.Split(",");
string[] row = commaSeparated.Split(CommonConstants.DataSeparator);
Index = DateTimeIndex.TryParseISODate(row.First(), out DateTimeIndex witsmlDateTime)
? witsmlDateTime
: new DepthIndex(double.Parse(row.First(), CultureInfo.InvariantCulture));
Expand Down
4 changes: 1 addition & 3 deletions Src/Witsml/Data/Curves/TimeSpanIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Witsml.Data.Curves;
/// </summary>
public class TimeSpanIndex : Index
{
private const string TimeSpanPattern = @"hh\:mm\:ss";

public TimeSpan Value { get; }

public TimeSpanIndex(TimeSpan value)
Expand All @@ -36,7 +34,7 @@ public override int CompareTo(Index that)

public override string GetValueAsString()
{
return Value.ToString(TimeSpanPattern, CultureInfo.InvariantCulture);
return Value.ToString(CommonConstants.TimeSpanIndex.Pattern, CultureInfo.InvariantCulture);
}

public override bool IsContinuous(Index that)
Expand Down
3 changes: 1 addition & 2 deletions Src/Witsml/Data/Curves/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ public class Unit
{
private readonly string _unitCode;

public static readonly Unit NoUnit = new("");
public static readonly Unit TimeUnit = new("s");
public static readonly Unit TimeUnit = new(CommonConstants.Unit.Second);

public Unit(string unitCode)
{
Expand Down
4 changes: 2 additions & 2 deletions Src/Witsml/Data/Measures/Measure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class Measure
{
return new()
{
Uom = "",
Value = ""
Uom = string.Empty,
Value = string.Empty
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions Src/Witsml/Data/Measures/WitsmlMeasureWithDatum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static WitsmlMeasureWithDatum ToFetch()
{
return new()
{
Uom = "",
Value = "",
Datum = ""
Uom = string.Empty,
Value = string.Empty,
Datum = string.Empty
};
}
}
Expand Down
3 changes: 2 additions & 1 deletion Src/Witsml/Data/Tubular/WitsmlHoleOpener.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;

Expand All @@ -15,7 +16,7 @@ public class WitsmlHoleOpener
[XmlElement("numCutter")]
public string NumCutterText
{
get => NumCutter.HasValue ? XmlConvert.ToString(NumCutter.Value) : null;
get => NumCutter?.ToString(CultureInfo.InvariantCulture);
set => NumCutter = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand Down
12 changes: 6 additions & 6 deletions Src/Witsml/Data/Tubular/WitsmlMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WitsmlMotor
[XmlElement("presLossFact")]
public string PresLossFactText
{
get => PresLossFact.HasValue ? XmlConvert.ToString(PresLossFact.Value) : null;
get => PresLossFact?.ToString(CultureInfo.InvariantCulture);
set => PresLossFact = string.IsNullOrEmpty(value) ? default(double?) : double.Parse(value, CultureInfo.InvariantCulture);
}

Expand All @@ -37,7 +37,7 @@ public string PresLossFactText
[XmlElement("lobesRotor")]
public string LobesRotorText
{
get => LobesRotor.HasValue ? XmlConvert.ToString(LobesRotor.Value) : null;
get => LobesRotor?.ToString(CultureInfo.InvariantCulture);
set => LobesRotor = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand All @@ -46,7 +46,7 @@ public string LobesRotorText
[XmlElement("lobesStator")]
public string LobesStatorText
{
get => LobesStator.HasValue ? XmlConvert.ToString(LobesStator.Value) : null;
get => LobesStator?.ToString(CultureInfo.InvariantCulture);
set => LobesStator = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand All @@ -61,7 +61,7 @@ public string LobesStatorText
[XmlElement("rotorCatcher")]
public string RotorCatcherText
{
get => RotorCatcher.HasValue ? XmlConvert.ToString(RotorCatcher.Value) : null;
get => RotorCatcher?.ToString(CultureInfo.InvariantCulture);
set => RotorCatcher = string.IsNullOrEmpty(value) ? default(bool?) : bool.Parse(value);
}

Expand All @@ -70,7 +70,7 @@ public string RotorCatcherText
[XmlElement("dumpValve")]
public string DumpValveText
{
get => DumpValve.HasValue ? XmlConvert.ToString(DumpValve.Value) : null;
get => DumpValve?.ToString(CultureInfo.InvariantCulture);
set => DumpValve = string.IsNullOrEmpty(value) ? default(bool?) : bool.Parse(value);
}

Expand All @@ -82,7 +82,7 @@ public string DumpValveText
[XmlElement("rotatable")]
public string RotatableText
{
get => Rotatable.HasValue ? XmlConvert.ToString(Rotatable.Value) : null;
get => Rotatable?.ToString(CultureInfo.InvariantCulture);
set => Rotatable = string.IsNullOrEmpty(value) ? default(bool?) : bool.Parse(value);
}

Expand Down
3 changes: 2 additions & 1 deletion Src/Witsml/Data/Tubular/WitsmlNozzle.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;

Expand All @@ -15,7 +16,7 @@ public class WitsmlNozzle
[XmlElement("index")]
public string IndexText
{
get => Index.HasValue ? XmlConvert.ToString(Index.Value) : null;
get => Index?.ToString(CultureInfo.InvariantCulture);
set => Index = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand Down
4 changes: 2 additions & 2 deletions Src/Witsml/Data/Tubular/WitsmlRotarySteerableTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class WitsmlRotarySteerableTool
[XmlElement("pressLossFact")]
public string PressLossFactText
{
get => PressLossFact.HasValue ? XmlConvert.ToString(PressLossFact.Value) : null;
get => PressLossFact?.ToString(CultureInfo.InvariantCulture);
set => PressLossFact = string.IsNullOrEmpty(value) ? default(double?) : double.Parse(value, CultureInfo.InvariantCulture);
}

Expand All @@ -59,7 +59,7 @@ public string PressLossFactText
[XmlElement("padCount")]
public string PadCountText
{
get => PadCount.HasValue ? XmlConvert.ToString(PadCount.Value) : null;
get => PadCount?.ToString(CultureInfo.InvariantCulture);
set => PadCount = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand Down
2 changes: 1 addition & 1 deletion Src/Witsml/Data/Tubular/WitsmlStabilizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class WitsmlStabilizer
[XmlElement("factFric")]
public string FactFricText
{
get => FactFric.HasValue ? XmlConvert.ToString(FactFric.Value) : null;
get => FactFric?.ToString(CultureInfo.InvariantCulture);
set => FactFric = string.IsNullOrEmpty(value) ? default(double?) : double.Parse(value, CultureInfo.InvariantCulture);
}

Expand Down
5 changes: 3 additions & 2 deletions Src/Witsml/Data/Tubular/WitsmlTubular.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;

Expand All @@ -25,7 +26,7 @@ public override WitsmlTubulars AsItemInWitsmlList()
[XmlElement("valveFloat")]
public string ValveFloatText
{
get => ValveFloat.HasValue ? XmlConvert.ToString(ValveFloat.Value) : null;
get => ValveFloat?.ToString(CultureInfo.InvariantCulture);
set => ValveFloat = string.IsNullOrEmpty(value) ? default(bool?) : bool.Parse(value);
}

Expand All @@ -34,7 +35,7 @@ public string ValveFloatText
[XmlElement("sourceNuclear")]
public string SourceNuclearText
{
get => SourceNuclear.HasValue ? XmlConvert.ToString(SourceNuclear.Value) : null;
get => SourceNuclear?.ToString(CultureInfo.InvariantCulture);
set => SourceNuclear = string.IsNullOrEmpty(value) ? default(bool?) : bool.Parse(value);
}

Expand Down
5 changes: 3 additions & 2 deletions Src/Witsml/Data/Tubular/WitsmlTubularComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;

Expand All @@ -19,7 +20,7 @@ public class WitsmlTubularComponent
[XmlElement("sequence")]
public string SequenceText
{
get => Sequence.HasValue ? XmlConvert.ToString(Sequence.Value) : null;
get => Sequence?.ToString(CultureInfo.InvariantCulture);
set => Sequence = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand All @@ -46,7 +47,7 @@ public string SequenceText
[XmlElement("numJointStand")]
public string NumJointStandText
{
get => NumJointStand.HasValue ? XmlConvert.ToString(NumJointStand.Value) : null;
get => NumJointStand?.ToString(CultureInfo.InvariantCulture);
set => NumJointStand = string.IsNullOrEmpty(value) ? default(int?) : int.Parse(value);
}

Expand Down
Loading

0 comments on commit 5c82937

Please sign in to comment.