/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client.Specification.IngestApi
{
///
/// Processor to automatically parse messages (or specific event fields) which are of the key=value variety.
///
[InterfaceDataContract]
public interface IKeyValueProcessor : IProcessor
{
/// List of keys to exclude from document
[DataMember(Name = "exclude_keys")]
IEnumerable ExcludeKeys { get; set; }
/// The field to be parsed
[DataMember(Name ="field")]
Field Field { get; set; }
/// Regex pattern to use for splitting key-value pairs
[DataMember(Name ="field_split")]
string FieldSplit { get; set; }
///
/// If true and does not exist or is `null`,
/// the processor quietly exits without modifying the document
///
[DataMember(Name ="ignore_missing")]
bool? IgnoreMissing { get; set; }
/// List of keys to filter and insert into document. Defaults to including all keys
[DataMember(Name ="include_keys")]
IEnumerable IncludeKeys { get; set; }
/// Prefix to be added to extracted keys
[DataMember(Name = "prefix")]
string Prefix { get; set; }
/// If true strip brackets (), <>, [] as well as quotes ' and " from extracted values
[DataMember(Name ="strip_brackets")]
bool? StripBrackets { get; set; }
/// The field to insert the extracted keys into. Defaults to the root of the document
[DataMember(Name ="target_field")]
Field TargetField { get; set; }
/// String of characters to trim from extracted keys
[DataMember(Name ="trim_key")]
string TrimKey { get; set; }
/// String of characters to trim from extracted values
[DataMember(Name ="trim_value")]
string TrimValue { get; set; }
/// Regex pattern to use for splitting the key from the value within a key-value pair
[DataMember(Name ="value_split")]
string ValueSplit { get; set; }
}
///
public class KeyValueProcessor : ProcessorBase, IKeyValueProcessor
{
///
public IEnumerable ExcludeKeys { get; set; }
///
public Field Field { get; set; }
///
public string FieldSplit { get; set; }
///
public bool? IgnoreMissing { get; set; }
///
public IEnumerable IncludeKeys { get; set; }
///
public string Prefix { get; set; }
///
public bool? StripBrackets { get; set; }
///
public Field TargetField { get; set; }
///
public string TrimKey { get; set; }
///
public string TrimValue { get; set; }
///
public string ValueSplit { get; set; }
protected override string Name => "kv";
}
///
public class KeyValueProcessorDescriptor : ProcessorDescriptorBase, IKeyValueProcessor>, IKeyValueProcessor
where T : class
{
protected override string Name => "kv";
IEnumerable IKeyValueProcessor.ExcludeKeys { get; set; }
Field IKeyValueProcessor.Field { get; set; }
string IKeyValueProcessor.FieldSplit { get; set; }
bool? IKeyValueProcessor.IgnoreMissing { get; set; }
IEnumerable IKeyValueProcessor.IncludeKeys { get; set; }
string IKeyValueProcessor.Prefix { get; set; }
bool? IKeyValueProcessor.StripBrackets { get; set; }
Field IKeyValueProcessor.TargetField { get; set; }
string IKeyValueProcessor.TrimKey { get; set; }
string IKeyValueProcessor.TrimValue { get; set; }
string IKeyValueProcessor.ValueSplit { get; set; }
///
public KeyValueProcessorDescriptor Field(Field field) => Assign(field, (a, v) => a.Field = v);
///
public KeyValueProcessorDescriptor Field(Expression> objectPath) => Assign(objectPath, (a, v) => a.Field = v);
///
public KeyValueProcessorDescriptor TargetField(Field field) => Assign(field, (a, v) => a.TargetField = v);
///
public KeyValueProcessorDescriptor TargetField(Expression> objectPath) => Assign(objectPath, (a, v) => a.TargetField = v);
///
public KeyValueProcessorDescriptor FieldSplit(string split) => Assign(split, (a, v) => a.FieldSplit = v);
///
public KeyValueProcessorDescriptor ValueSplit(string split) => Assign(split, (a, v) => a.ValueSplit = v);
///
public KeyValueProcessorDescriptor Prefix(string prefix) => Assign(prefix, (a, v) => a.Prefix = v);
///
public KeyValueProcessorDescriptor IgnoreMissing(bool? ignoreMissing = true) => Assign(ignoreMissing, (a, v) => a.IgnoreMissing = v);
///
public KeyValueProcessorDescriptor IncludeKeys(IEnumerable includeKeys) => Assign(includeKeys, (a, v) => a.IncludeKeys = v);
///
public KeyValueProcessorDescriptor IncludeKeys(params string[] includeKeys) => Assign(includeKeys, (a, v) => a.IncludeKeys = v);
///
public KeyValueProcessorDescriptor ExcludeKeys(IEnumerable excludeKeys) => Assign(excludeKeys, (a, v) => a.ExcludeKeys = v);
///
public KeyValueProcessorDescriptor ExcludeKeys(params string[] excludeKeys) => Assign(excludeKeys, (a, v) => a.ExcludeKeys = v);
///
public KeyValueProcessorDescriptor TrimKey(string trimKeys) => Assign(trimKeys, (a, v) => a.TrimKey = v);
///
public KeyValueProcessorDescriptor TrimValue(string trimValues) => Assign(trimValues, (a, v) => a.TrimValue = v);
///
public KeyValueProcessorDescriptor StripBrackets(bool? skip = true) => Assign(skip, (a, v) => a.StripBrackets = v);
}
}