/* 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; namespace OpenSearch.Client { public interface IClrTypeMapping { /// /// The CLR type the mapping relates to /// Type ClrType { get; } /// /// The property for the given to resolve ids from. /// string IdPropertyName { get; set; } /// /// The default OpenSearch index name for the given /// string IndexName { get; set; } /// /// The relation name for the given to resolve to. /// string RelationName { get; set; } /// Disables Id inference for the given . /// By default, the _id value for a document is inferred from a property named Id, /// or from the property named by , if set. /// bool DisableIdInference { get; set; } } public interface IClrTypeMapping : IClrTypeMapping where TDocument : class { /// Set a default Id property on CLR type that OpenSearch.Client will evaluate Expression> IdProperty { get; set; } /// /// Ignore or rename certain properties of CLR type /// IList> Properties { get; set; } /// Provide a default routing parameter lookup based on Expression> RoutingProperty { get; set; } } public class ClrTypeMapping : IClrTypeMapping { /// /// Initializes a new instance of /// public ClrTypeMapping(Type type) => ClrType = type; /// public Type ClrType { get; } /// public string IdPropertyName { get; set; } /// public string IndexName { get; set; } /// public string RelationName { get; set; } /// public bool DisableIdInference { get; set; } } public class ClrTypeMapping : ClrTypeMapping, IClrTypeMapping where TDocument : class { public ClrTypeMapping() : base(typeof(TDocument)) { } /// public Expression> IdProperty { get; set; } /// public IList> Properties { get; set; } /// public Expression> RoutingProperty { get; set; } } public class ClrTypeMappingDescriptor : DescriptorBase, IClrTypeMapping { private readonly Type _type; /// /// Instantiates a new instance of /// /// The CLR type to map public ClrTypeMappingDescriptor(Type type) => _type = type; Type IClrTypeMapping.ClrType => _type; string IClrTypeMapping.IdPropertyName { get; set; } string IClrTypeMapping.IndexName { get; set; } string IClrTypeMapping.RelationName { get; set; } bool IClrTypeMapping.DisableIdInference { get; set; } /// public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(indexName, (a, v) => a.IndexName = v); /// public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(relationName, (a, v) => a.RelationName = v); /// public ClrTypeMappingDescriptor IdProperty(string idProperty) => Assign(idProperty, (a, v) => a.IdPropertyName = v); /// public ClrTypeMappingDescriptor DisableIdInference(bool disable = true) => Assign(disable, (a, v) => a.DisableIdInference = v); } public class ClrTypeMappingDescriptor : DescriptorBase, IClrTypeMapping>, IClrTypeMapping where TDocument : class { Type IClrTypeMapping.ClrType { get; } = typeof(TDocument); Expression> IClrTypeMapping.IdProperty { get; set; } string IClrTypeMapping.IdPropertyName { get; set; } string IClrTypeMapping.IndexName { get; set; } IList> IClrTypeMapping.Properties { get; set; } = new List>(); string IClrTypeMapping.RelationName { get; set; } Expression> IClrTypeMapping.RoutingProperty { get; set; } bool IClrTypeMapping.DisableIdInference { get; set; } /// /// The default OpenSearch index name for /// public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(indexName, (a, v) => a.IndexName = v); /// /// The relation name for to resolve to. /// public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(relationName, (a, v) => a.RelationName = v); /// /// Set a default Id property on CLR type that OpenSearch.Client will evaluate /// public ClrTypeMappingDescriptor IdProperty(Expression> property) => Assign(property, (a, v) => a.IdProperty = v); /// /// Set a default Id property on CLR type that OpenSearch.Client will evaluate /// public ClrTypeMappingDescriptor IdProperty(string property) => Assign(property, (a, v) => a.IdPropertyName = v); /// Provide a default routing parameter lookup based on public ClrTypeMappingDescriptor RoutingProperty(Expression> property) => Assign(property, (a, v) => a.RoutingProperty = v); /// /// Ignore on CLR type /// public ClrTypeMappingDescriptor Ignore(Expression> property) => Assign(property, (a, v) => a.Properties.Add(new IgnoreClrPropertyMapping(v))); /// /// Rename on CLR type /// public ClrTypeMappingDescriptor PropertyName(Expression> property, string newName) => Assign(new RenameClrPropertyMapping(property, newName), (a, v) => a.Properties.Add(v)); /// public ClrTypeMappingDescriptor DisableIdInference(bool disable = true) => Assign(disable, (a, v) => a.DisableIdInference = v); } }