Add kind attribute to json serializer

This commit is contained in:
Zoe Roux 2023-11-19 01:15:13 +01:00
parent ca6a4d8ab5
commit 9ea177e2f6
4 changed files with 78 additions and 28 deletions

View File

@ -17,33 +17,17 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Kyoo.Utils;
namespace Kyoo.Abstractions.Models
namespace Kyoo.Abstractions.Models.Attributes;
/// <summary>
/// An attribute to inform that this interface is a type union
/// </summary>
[AttributeUsage(AttributeTargets.Interface)]
public class OneOfAttribute : Attribute
{
/// <summary>
/// The type of item, ether a show, a movie or a collection.
/// The types this union concist of.
/// </summary>
public enum ItemKind
{
/// <summary>
/// The <see cref="ILibraryItem"/> is a <see cref="Show"/>.
/// </summary>
Show,
/// <summary>
/// The <see cref="ILibraryItem"/> is a Movie.
/// </summary>
Movie,
/// <summary>
/// The <see cref="ILibraryItem"/> is a <see cref="Collection"/>.
/// </summary>
Collection
}
public interface ILibraryItem : IResource, IThumbnails, IMetadata, IAddedDate { }
public Type[] Types { get; set; }
}

View File

@ -0,0 +1,27 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using Kyoo.Abstractions.Models.Attributes;
namespace Kyoo.Abstractions.Models;
/// <summary>
/// A show, a movie or a collection.
/// </summary>
[OneOf(Types = new[] { typeof(Show), typeof(Movie), typeof(Collection) })]
public interface ILibraryItem : IResource, IThumbnails, IMetadata, IAddedDate { }

View File

@ -98,7 +98,7 @@ namespace Kyoo.Core.Controllers
// TODO: Implement default sort by
Sort<T>.Default => $"coalesce({string.Join(", ", tables.Select(x => $"{x}.name"))})",
Sort<T>.By(string key, bool desc) => $"coalesce({string.Join(", ", tables.Select(x => $"{x}.{key}"))}) {(desc ? "desc" : "asc")}",
Sort<T>.Random(var seed) => $"md5({seed} || coalesce({string.Join(", ", tables.Select(x => $"{x}.id"))}))",
Sort<T>.Random(var seed) => $"md5('{seed}' || coalesce({string.Join(", ", tables.Select(x => $"{x}.id"))}))",
Sort<T>.Conglomerate(var list) => string.Join(", ", list.Select(x => ProcessSort(x, tables))),
_ => throw new SwitchExpressionException(),
};

View File

@ -16,7 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
@ -58,8 +60,7 @@ namespace Kyoo.Core.Api
{
property.ShouldSerialize = _ =>
{
ICollection<string>? fields = (ICollection<string>)_httpContextAccessor.HttpContext!.Items["fields"]!;
if (fields == null)
if (_httpContextAccessor.HttpContext!.Items["fields"] is not ICollection<string> fields)
return false;
return fields.Contains(member.Name);
};
@ -71,5 +72,43 @@ namespace Kyoo.Core.Api
property.ShouldDeserialize = _ => false;
return property;
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
if (properties.All(x => x.PropertyName != "kind"))
{
properties.Add(new JsonProperty()
{
DeclaringType = type,
PropertyName = "kind",
UnderlyingName = "kind",
PropertyType = typeof(string),
ValueProvider = new FixedValueProvider(type.Name),
Readable = true,
Writable = false,
TypeNameHandling = TypeNameHandling.None,
});
}
return properties;
}
public class FixedValueProvider : IValueProvider
{
private readonly object _value;
public FixedValueProvider(object value)
{
_value = value;
}
public object GetValue(object target)
=> _value;
public void SetValue(object target, object? value)
=> throw new NotImplementedException();
}
}
}