/* 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.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client.Specification.IngestApi
{
/// Ingest pipelines are composed of one or more processors
[InterfaceDataContract]
[JsonFormatter(typeof(ProcessorFormatter))]
public interface IProcessor
{
/// The name of the processor, will be used as the key when persisting the processor on the pipeline
[IgnoreDataMember]
string Name { get; }
///
/// A description to explain the purpose of the specific processor instance.
///
[DataMember(Name = "description")]
string Description { get; set; }
///
/// If a processor fails, call these processors instead. Read more about handling failures here:
///
///
[DataMember(Name ="on_failure")]
IEnumerable OnFailure { get; set; }
/// A painless script predicate that can control whether this processor should be executed or not
[DataMember(Name = "if")]
string If { get; set; }
///
/// A tag is simply a string identifier of the specific instantiation of a certain processor
/// in a pipeline. The tag field does not affect the processor’s behavior, but is very useful
/// for bookkeeping and tracing errors to specific processors.
///
[DataMember(Name = "tag")]
string Tag { get; set; }
/// When a failure happens, ignore it and proceed to the next processor
[DataMember(Name = "ignore_failure")]
bool? IgnoreFailure { get; set; }
}
///
public abstract class ProcessorBase : IProcessor
{
///
public string If { get; set; }
///
public string Tag { get; set; }
///
public bool? IgnoreFailure { get; set; }
///
public IEnumerable OnFailure { get; set; }
protected abstract string Name { get; }
///
public string Description { get; set; }
string IProcessor.Name => Name;
}
///
public abstract class ProcessorDescriptorBase
: DescriptorBase, IProcessor
where TProcessorDescriptor : ProcessorDescriptorBase, TProcessorInterface
where TProcessorInterface : class, IProcessor
{
protected abstract string Name { get; }
string IProcessor.Name => Name;
string IProcessor.Description { get; set; }
IEnumerable IProcessor.OnFailure { get; set; }
string IProcessor.If { get; set; }
string IProcessor.Tag { get; set; }
bool? IProcessor.IgnoreFailure { get; set; }
///
public TProcessorDescriptor Description(string description) => Assign(description, (a, v) => a.Description = v);
///
public TProcessorDescriptor OnFailure(IEnumerable processors) => Assign(processors.ToListOrNullIfEmpty(), (a, v) => a.OnFailure = v);
///
public TProcessorDescriptor OnFailure(Func>> selector) =>
Assign(selector, (a, v) => a.OnFailure = v?.Invoke(new ProcessorsDescriptor())?.Value);
///
public TProcessorDescriptor If(string painlessPredicate) => Assign(painlessPredicate, (a, v) => a.If = v);
///
public TProcessorDescriptor Tag(string tag) => Assign(tag, (a, v) => a.Tag = v);
///
public TProcessorDescriptor IgnoreFailure(bool? ignoreFailure = true) => Assign(ignoreFailure, (a, v) => a.IgnoreFailure = v);
}
}