Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/src/FieldValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public string AsString()
throw new InvalidOperationException($"Cannot get field as String. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueString;
Expand All @@ -242,14 +243,15 @@ public long AsInt64()
throw new InvalidOperationException($"Cannot get field as Integer. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueInteger;
}

if (!_fieldValue.ValueInteger.HasValue)
{
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Int64)} type. Consider using the 'ValueData.Text' property.");
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Int64)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
}

return _fieldValue.ValueInteger.Value;
Expand All @@ -267,6 +269,7 @@ public float AsFloat()
throw new InvalidOperationException($"Cannot get field as Float. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueNumber;
Expand All @@ -281,7 +284,7 @@ public float AsFloat()
}
else
{
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Float)} type. Consider using the 'ValueData.Text' property.");
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Float)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
}
}

Expand All @@ -300,14 +303,15 @@ public DateTime AsDate()
throw new InvalidOperationException($"Cannot get field as Date. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueDate;
}

if (!_fieldValue.ValueDate.HasValue)
{
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Date)} type. Consider using the 'ValueData.Text' property.");
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Date)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
}

return _fieldValue.ValueDate.Value.UtcDateTime;
Expand All @@ -325,14 +329,15 @@ public TimeSpan AsTime()
throw new InvalidOperationException($"Cannot get field as Time. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueTime;
}

if (!_fieldValue.ValueTime.HasValue)
{
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Time)} type. Consider using the 'ValueData.Text' property.");
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Time)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
}

return _fieldValue.ValueTime.Value;
Expand All @@ -350,6 +355,7 @@ public string AsPhoneNumber()
throw new InvalidOperationException($"Cannot get field as PhoneNumber. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueString;
Expand All @@ -370,6 +376,7 @@ public IReadOnlyList<FormField> AsList()
throw new InvalidOperationException($"Cannot get field as List. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueList;
Expand All @@ -396,6 +403,7 @@ public IReadOnlyDictionary<string, FormField> AsDictionary()
throw new InvalidOperationException($"Cannot get field as Dictionary. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueDictionary;
Expand Down Expand Up @@ -430,14 +438,15 @@ public SelectionMarkState AsSelectionMarkState()
throw new InvalidOperationException($"Cannot get field as SelectionMark. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueSelectionMark;
}

if (!_fieldValue.ValueSelectionMark.HasValue)
{
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.SelectionMark)} type. Consider using the 'ValueData.Text' property.");
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.SelectionMark)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
}

return _fieldValue.ValueSelectionMark.Value;
Expand All @@ -455,6 +464,7 @@ public string AsCountryCode()
throw new InvalidOperationException($"Cannot get field as country code. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueString;
Expand All @@ -475,14 +485,15 @@ public FieldValueGender AsGender()
throw new InvalidOperationException($"Cannot get field as gender. Field value's type is {ValueType}.");
}

// Use when mocking
if (_fieldValue == null)
{
return ValueGender;
}

if (!_fieldValue.ValueGender.HasValue)
{
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Gender)} type. Consider using the 'ValueData.Text' property.");
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Gender)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
}

return _fieldValue.ValueGender.Value;
Expand Down