|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.Linq.Expressions; |
| 4 | + |
| 5 | +namespace Nest |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Converts a human readable byte value (e.g. 1kb) to its value in bytes (e.g. 1024). |
| 9 | + /// Supported human readable units are "b", "kb", "mb", "gb", "tb", "pb" case insensitive. |
| 10 | + /// An error will occur if the field is not a supported format or resultant value exceeds 2^63. |
| 11 | + /// </summary> |
| 12 | + [JsonObject(MemberSerialization.OptIn)] |
| 13 | + [JsonConverter(typeof(ProcessorJsonConverter<BytesProcessor>))] |
| 14 | + public interface IBytesProcessor : IProcessor |
| 15 | + { |
| 16 | + /// <summary> The field to convert bytes from </summary> |
| 17 | + [JsonProperty("field")] |
| 18 | + Field Field { get; set; } |
| 19 | + |
| 20 | + /// <summary> The field to assign the converted value to, by default <see cref="Field"/> is updated in-place </summary> |
| 21 | + [JsonProperty("target_field")] |
| 22 | + Field TargetField { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// If <c>true</c> and <see cref="Field"/> does not exist or is null, |
| 26 | + /// the processor quietly exits without modifying the document. Default is <c>false</c> |
| 27 | + /// </summary> |
| 28 | + [JsonProperty("ignore_missing")] |
| 29 | + bool? IgnoreMissing { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + /// <inheritdoc cref="IBytesProcessor"/> |
| 33 | + public class BytesProcessor : ProcessorBase, IBytesProcessor |
| 34 | + { |
| 35 | + protected override string Name => "bytes"; |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + [JsonProperty("field")] |
| 39 | + public Field Field { get; set; } |
| 40 | + |
| 41 | + /// <inheritdoc /> |
| 42 | + [JsonProperty("target_field")] |
| 43 | + public Field TargetField { get; set; } |
| 44 | + |
| 45 | + /// <inheritdoc /> |
| 46 | + [JsonProperty("ignore_missing")] |
| 47 | + public bool? IgnoreMissing { get; set; } |
| 48 | + } |
| 49 | + |
| 50 | + /// <inheritdoc cref="IBytesProcessor"/> |
| 51 | + public class BytesProcessorDescriptor<T> |
| 52 | + : ProcessorDescriptorBase<BytesProcessorDescriptor<T>, IBytesProcessor>, IBytesProcessor |
| 53 | + where T : class |
| 54 | + { |
| 55 | + protected override string Name => "bytes"; |
| 56 | + |
| 57 | + Field IBytesProcessor.Field { get; set; } |
| 58 | + Field IBytesProcessor.TargetField { get; set; } |
| 59 | + bool? IBytesProcessor.IgnoreMissing { get; set; } |
| 60 | + |
| 61 | + /// <inheritdoc cref="IBytesProcessor.Field"/> |
| 62 | + public BytesProcessorDescriptor<T> Field(Field field) => Assign(a => a.Field = field); |
| 63 | + |
| 64 | + /// <inheritdoc cref="IBytesProcessor.Field"/> |
| 65 | + public BytesProcessorDescriptor<T> Field(Expression<Func<T, object>> objectPath) => Assign(a => a.Field = objectPath); |
| 66 | + |
| 67 | + /// <inheritdoc cref="IBytesProcessor.TargetField"/> |
| 68 | + public BytesProcessorDescriptor<T> TargetField(Field field) => Assign(a => a.TargetField = field); |
| 69 | + |
| 70 | + /// <inheritdoc cref="IBytesProcessor.TargetField"/> |
| 71 | + public BytesProcessorDescriptor<T> TargetField(Expression<Func<T, object>> objectPath) => Assign(a => a.TargetField = objectPath); |
| 72 | + |
| 73 | + /// <inheritdoc cref="IBytesProcessor.IgnoreMissing"/> |
| 74 | + public BytesProcessorDescriptor<T> IgnoreMissing(bool? ignoreMissing = true) => Assign(a => a.IgnoreMissing = ignoreMissing); |
| 75 | + } |
| 76 | +} |
0 commit comments