Skip to content

Commit 8670966

Browse files
Mpdreamzrusscam
authored andcommitted
add support for the bytes processor (#3440)
(cherry picked from commit 577888a)
1 parent ad4f02c commit 8670966

File tree

5 files changed

+99
-4
lines changed

5 files changed

+99
-4
lines changed

src/Nest/Ingest/PipelineJsonConverter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private List<IProcessor> GetProcessors(JToken jsonProcessors, JsonSerializer ser
108108
case "urldecode":
109109
processors.Add(jsonProcessor.ToObject<UrlDecodeProcessor>(serializer));
110110
break;
111+
case "bytes":
112+
processors.Add(jsonProcessor.ToObject<BytesProcessor>(serializer));
113+
break;
111114
}
112115
}
113116
return processors;

src/Nest/Ingest/ProcessorJsonConverter.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using Newtonsoft.Json;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace Nest
95
{
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

src/Nest/Ingest/ProcessorsDescriptor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,9 @@ public ProcessorsDescriptor Kv<T>(Func<KeyValueProcessorDescriptor<T>, IKeyValue
137137
/// </summary>
138138
public ProcessorsDescriptor UrlDecode<T>(Func<UrlDecodeProcessorDescriptor<T>, IUrlDecodeProcessor> selector) where T : class =>
139139
Assign(a => a.AddIfNotNull(selector?.Invoke(new UrlDecodeProcessorDescriptor<T>())));
140+
141+
/// <inheritdoc cref="IBytesProcessor"/>
142+
public ProcessorsDescriptor Bytes<T>(Func<BytesProcessorDescriptor<T>, IBytesProcessor> selector) where T : class =>
143+
Assign(a => a.AddIfNotNull(selector?.Invoke(new BytesProcessorDescriptor<T>())));
140144
}
141145
}

src/Tests/Tests/Ingest/ProcessorAssertions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,22 @@ public class UrlDecode : ProcessorAssertion
342342
);
343343
}
344344

345+
346+
[SkipVersion("<6.4.0", "")]
347+
public class Bytes : ProcessorAssertion
348+
{
349+
public override string Key => "bytes";
350+
351+
public override object Json => new { field = "description", ignore_missing = true };
352+
353+
public override IProcessor Initializer => new BytesProcessor { Field = "description", IgnoreMissing = true };
354+
355+
public override Func<ProcessorsDescriptor, IPromise<IList<IProcessor>>> Fluent => d => d
356+
.Bytes<Project>(ud => ud
357+
.Field(p => p.Description)
358+
);
359+
}
360+
345361
public class KeyValue : ProcessorAssertion
346362
{
347363
public override string Key => "kv";

0 commit comments

Comments
 (0)