2024-08-21 08:45:17 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2023-09-20 08:13:29 +00:00
|
|
|
|
|
2023-11-12 09:07:23 +00:00
|
|
|
|
namespace KubernetesWorkflow.Recipe
|
2023-09-20 08:13:29 +00:00
|
|
|
|
{
|
|
|
|
|
public class ContainerAdditionals
|
|
|
|
|
{
|
|
|
|
|
public ContainerAdditionals(Additional[] additionals)
|
|
|
|
|
{
|
|
|
|
|
Additionals = additionals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ContainerAdditionals CreateFromUserData(IEnumerable<object> userData)
|
|
|
|
|
{
|
|
|
|
|
return new ContainerAdditionals(userData.Select(ConvertToAdditional).ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Additional[] Additionals { get; }
|
|
|
|
|
|
|
|
|
|
public T? Get<T>()
|
|
|
|
|
{
|
|
|
|
|
var typeName = GetTypeName(typeof(T));
|
|
|
|
|
var userData = Additionals.SingleOrDefault(a => a.Type == typeName);
|
2023-11-12 09:07:23 +00:00
|
|
|
|
if (userData == null) return default;
|
2024-08-21 08:45:17 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(userData.UserData);
|
2023-09-20 08:13:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Additional ConvertToAdditional(object userData)
|
|
|
|
|
{
|
|
|
|
|
var typeName = GetTypeName(userData.GetType());
|
2024-08-21 08:45:17 +00:00
|
|
|
|
return new Additional(typeName, JsonConvert.SerializeObject(userData));
|
2023-09-20 08:13:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetTypeName(Type type)
|
|
|
|
|
{
|
|
|
|
|
var typeName = type.FullName;
|
|
|
|
|
if (string.IsNullOrEmpty(typeName)) throw new Exception("Object type fullname is null or empty: " + type);
|
|
|
|
|
return typeName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Additional
|
|
|
|
|
{
|
2024-08-21 08:45:17 +00:00
|
|
|
|
public Additional(string type, string userData)
|
2023-09-20 08:13:29 +00:00
|
|
|
|
{
|
|
|
|
|
Type = type;
|
|
|
|
|
UserData = userData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Type { get; }
|
2024-08-21 08:45:17 +00:00
|
|
|
|
public string UserData { get; }
|
2023-09-20 08:13:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|