1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-05 14:23:30 +01:00
bitwarden-server/util/Setup/YamlComments.cs

108 lines
3.6 KiB
C#
Raw Normal View History

using System.ComponentModel;
2018-08-30 17:35:44 +02:00
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.ObjectGraphVisitors;
using YamlDotNet.Serialization.TypeInspectors;
// ref: https://github.com/aaubry/YamlDotNet/issues/152#issuecomment-349034754
2022-08-29 22:06:55 +02:00
namespace Bit.Setup;
public class CommentGatheringTypeInspector : TypeInspectorSkeleton
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
private readonly ITypeInspector _innerTypeDescriptor;
public CommentGatheringTypeInspector(ITypeInspector innerTypeDescriptor)
{
_innerTypeDescriptor = innerTypeDescriptor ?? throw new ArgumentNullException(nameof(innerTypeDescriptor));
}
public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
return _innerTypeDescriptor.GetProperties(type, container).Select(d => new CommentsPropertyDescriptor(d));
}
2018-08-30 17:35:44 +02:00
2022-08-29 22:06:55 +02:00
private sealed class CommentsPropertyDescriptor : IPropertyDescriptor
{
private readonly IPropertyDescriptor _baseDescriptor;
public CommentsPropertyDescriptor(IPropertyDescriptor baseDescriptor)
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
_baseDescriptor = baseDescriptor;
Name = baseDescriptor.Name;
2018-08-30 17:35:44 +02:00
}
2022-08-29 22:06:55 +02:00
public string Name { get; set; }
public int Order { get; set; }
public Type Type => _baseDescriptor.Type;
public bool CanWrite => _baseDescriptor.CanWrite;
public Type TypeOverride
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
get { return _baseDescriptor.TypeOverride; }
set { _baseDescriptor.TypeOverride = value; }
2018-08-30 17:35:44 +02:00
}
2022-08-29 22:06:55 +02:00
public ScalarStyle ScalarStyle
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
get { return _baseDescriptor.ScalarStyle; }
set { _baseDescriptor.ScalarStyle = value; }
}
2022-08-29 22:06:55 +02:00
public void Write(object target, object value)
{
_baseDescriptor.Write(target, value);
}
2022-08-29 22:06:55 +02:00
public T GetCustomAttribute<T>() where T : Attribute
{
return _baseDescriptor.GetCustomAttribute<T>();
}
2022-08-29 22:06:55 +02:00
public IObjectDescriptor Read(object target)
{
var description = _baseDescriptor.GetCustomAttribute<DescriptionAttribute>();
return description != null ?
new CommentsObjectDescriptor(_baseDescriptor.Read(target), description.Description) :
_baseDescriptor.Read(target);
2018-08-30 17:35:44 +02:00
}
}
2022-08-29 22:06:55 +02:00
}
public sealed class CommentsObjectDescriptor : IObjectDescriptor
{
private readonly IObjectDescriptor _innerDescriptor;
2018-08-30 17:35:44 +02:00
2022-08-29 22:06:55 +02:00
public CommentsObjectDescriptor(IObjectDescriptor innerDescriptor, string comment)
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
_innerDescriptor = innerDescriptor;
Comment = comment;
}
2018-08-30 17:35:44 +02:00
2022-08-29 22:06:55 +02:00
public string Comment { get; private set; }
public object Value => _innerDescriptor.Value;
public Type Type => _innerDescriptor.Type;
public Type StaticType => _innerDescriptor.StaticType;
public ScalarStyle ScalarStyle => _innerDescriptor.ScalarStyle;
}
2018-08-30 17:35:44 +02:00
2022-08-29 22:06:55 +02:00
public class CommentsObjectGraphVisitor : ChainedObjectGraphVisitor
{
public CommentsObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisitor)
: base(nextVisitor) { }
2018-08-30 17:35:44 +02:00
2022-08-29 22:06:55 +02:00
public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
if (value is CommentsObjectDescriptor commentsDescriptor && commentsDescriptor.Comment != null)
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
context.Emit(new Comment(string.Empty, false));
foreach (var comment in commentsDescriptor.Comment.Split(Environment.NewLine))
2018-08-30 17:35:44 +02:00
{
2022-08-29 22:06:55 +02:00
context.Emit(new Comment(comment, false));
2018-08-30 17:35:44 +02:00
}
}
2022-08-29 22:06:55 +02:00
return base.EnterMapping(key, value, context);
2018-08-30 17:35:44 +02:00
}
}