| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | |
|
| | 7 | | namespace Azure.AI.FormRecognizer.Models |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Represents the strongly-typed value of a field recognized from the input document and provides |
| | 11 | | /// methods for converting it to the appropriate type. |
| | 12 | | /// </summary> |
| | 13 | | public readonly struct FieldValue |
| | 14 | | { |
| | 15 | | private readonly FieldValue_internal _fieldValue; |
| | 16 | | private readonly IReadOnlyList<ReadResult> _readResults; |
| | 17 | |
|
| | 18 | | internal FieldValue(FieldValue_internal fieldValue, IReadOnlyList<ReadResult> readResults) |
| | 19 | | { |
| 1312 | 20 | | ValueType = fieldValue.Type; |
| 1312 | 21 | | _fieldValue = fieldValue; |
| 1312 | 22 | | _readResults = readResults; |
| 1312 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The data type of the field value. |
| | 27 | | /// </summary> |
| 338 | 28 | | public FieldValueType ValueType { get; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Gets the value of the field as a <see cref="string"/>. |
| | 32 | | /// </summary> |
| | 33 | | /// <returns>The value of the field converted to a <see cref="string"/>.</returns> |
| | 34 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 35 | | public string AsString() |
| | 36 | | { |
| 114 | 37 | | if (ValueType != FieldValueType.String) |
| | 38 | | { |
| 0 | 39 | | throw new InvalidOperationException($"Cannot get field as String. Field value's type is {ValueType}."); |
| | 40 | | } |
| | 41 | |
|
| 114 | 42 | | return _fieldValue?.ValueString; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets the value of the field as a <see cref="long"/>. |
| | 47 | | /// </summary> |
| | 48 | | /// <returns>The value of the field converted to a <see cref="long"/>.</returns> |
| | 49 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 50 | | public long AsInt64() |
| | 51 | | { |
| 0 | 52 | | if (ValueType != FieldValueType.Int64) |
| | 53 | | { |
| 0 | 54 | | throw new InvalidOperationException($"Cannot get field as Integer. Field value's type is {ValueType}.") |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | if (!_fieldValue.ValueInteger.HasValue) |
| | 58 | | { |
| 0 | 59 | | throw new InvalidOperationException($"Field value is null."); |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | return _fieldValue.ValueInteger.Value; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets the value of the field as a <see cref="float"/>. |
| | 67 | | /// </summary> |
| | 68 | | /// <returns>The value of the field converted to a <see cref="float"/>.</returns> |
| | 69 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 70 | | public float AsFloat() |
| | 71 | | { |
| 112 | 72 | | if (ValueType != FieldValueType.Float) |
| | 73 | | { |
| 0 | 74 | | throw new InvalidOperationException($"Cannot get field as Float. Field value's type is {ValueType}."); |
| | 75 | | } |
| | 76 | |
|
| 112 | 77 | | if (!_fieldValue.ValueNumber.HasValue) |
| | 78 | | { |
| | 79 | | // TODO: Sometimes ValueNumber isn't populated in ReceiptItems. The following is a |
| | 80 | | // workaround to get the value from Text if ValueNumber isn't there. |
| | 81 | | // https://github.com/Azure/azure-sdk-for-net/issues/10333 |
| | 82 | | float parsedFloat; |
| 32 | 83 | | if (float.TryParse(_fieldValue.Text.TrimStart('$'), out parsedFloat)) |
| | 84 | | { |
| 32 | 85 | | return parsedFloat; |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| 80 | 89 | | return _fieldValue.ValueNumber.Value; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Gets the value of the field as a <see cref="DateTime"/>. |
| | 94 | | /// </summary> |
| | 95 | | /// <returns>The value of the field converted to a <see cref="DateTime"/>.</returns> |
| | 96 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 97 | | public DateTime AsDate() |
| | 98 | | { |
| 16 | 99 | | if (ValueType != FieldValueType.Date) |
| | 100 | | { |
| 0 | 101 | | throw new InvalidOperationException($"Cannot get field as Date. Field value's type is {ValueType}."); |
| | 102 | | } |
| | 103 | |
|
| 16 | 104 | | if (!_fieldValue.ValueDate.HasValue) |
| | 105 | | { |
| 0 | 106 | | throw new InvalidOperationException($"Field value is null."); |
| | 107 | | } |
| | 108 | |
|
| 16 | 109 | | return _fieldValue.ValueDate.Value.UtcDateTime; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Gets the value of the field as a <see cref="TimeSpan"/>. |
| | 114 | | /// </summary> |
| | 115 | | /// <returns>The value of the field converted to a <see cref="TimeSpan"/>.</returns> |
| | 116 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 117 | | public TimeSpan AsTime() |
| | 118 | | { |
| 16 | 119 | | if (ValueType != FieldValueType.Time) |
| | 120 | | { |
| 0 | 121 | | throw new InvalidOperationException($"Cannot get field as Time. Field value's type is {ValueType}."); |
| | 122 | | } |
| | 123 | |
|
| 16 | 124 | | if (!_fieldValue.ValueTime.HasValue) |
| | 125 | | { |
| 0 | 126 | | throw new InvalidOperationException($"Field value is null."); |
| | 127 | | } |
| | 128 | |
|
| 16 | 129 | | return _fieldValue.ValueTime.Value; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Gets the value of the field as a phone number <see cref="string"/>. |
| | 134 | | /// </summary> |
| | 135 | | /// <returns>The value of the field converted to a phone number <see cref="string"/>.</returns> |
| | 136 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 137 | | public string AsPhoneNumber() |
| | 138 | | { |
| 0 | 139 | | if (ValueType != FieldValueType.PhoneNumber) |
| | 140 | | { |
| 0 | 141 | | throw new InvalidOperationException($"Cannot get field as PhoneNumber. Field value's type is {ValueType |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | return _fieldValue.ValuePhoneNumber; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | /// <summary> |
| | 148 | | /// Gets the value of the field as an <see cref="IReadOnlyList{T}"/>. |
| | 149 | | /// </summary> |
| | 150 | | /// <returns>The value of the field converted to an <see cref="IReadOnlyList{T}"/>.</returns> |
| | 151 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 152 | | public IReadOnlyList<FormField> AsList() |
| | 153 | | { |
| 16 | 154 | | if (ValueType != FieldValueType.List) |
| | 155 | | { |
| 0 | 156 | | throw new InvalidOperationException($"Cannot get field as List. Field value's type is {ValueType}."); |
| | 157 | | } |
| | 158 | |
|
| 16 | 159 | | List<FormField> fieldList = new List<FormField>(); |
| 96 | 160 | | foreach (var fieldValue in _fieldValue.ValueArray) |
| | 161 | | { |
| 32 | 162 | | fieldList.Add(new FormField(null, fieldValue, _readResults)); |
| | 163 | | } |
| | 164 | |
|
| 16 | 165 | | return fieldList; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Gets the value of the field as a <see cref="Dictionary{TKey, TValue}"/>. |
| | 170 | | /// </summary> |
| | 171 | | /// <returns>The value of the field converted to a <see cref="Dictionary{TKey, TValue}"/>.</returns> |
| | 172 | | /// <exception cref="InvalidOperationException">Thrown when <see cref="ValueType"/> is not <see cref="FieldValue |
| | 173 | | public IReadOnlyDictionary<string, FormField> AsDictionary() |
| | 174 | | { |
| 32 | 175 | | if (ValueType != FieldValueType.Dictionary) |
| | 176 | | { |
| 0 | 177 | | throw new InvalidOperationException($"Cannot get field as Dictionary. Field value's type is {ValueType} |
| | 178 | | } |
| | 179 | |
|
| 32 | 180 | | Dictionary<string, FormField> fieldDictionary = new Dictionary<string, FormField>(); |
| | 181 | |
|
| 240 | 182 | | foreach (var kvp in _fieldValue.ValueObject) |
| | 183 | | { |
| 88 | 184 | | fieldDictionary[kvp.Key] = new FormField(kvp.Key, kvp.Value, _readResults); |
| | 185 | | } |
| | 186 | |
|
| 32 | 187 | | return fieldDictionary; |
| | 188 | | } |
| | 189 | | } |
| | 190 | | } |