|
1 | | -using YamlDotNet.Serialization; |
2 | | -using YamlDotNet.Serialization.NamingConventions; |
3 | | -using YamlDotNet.Serialization.TypeInspectors; |
| 1 | +using SharpYaml; |
4 | 2 |
|
5 | 3 | namespace GitVersion.Configuration; |
6 | 4 |
|
7 | 5 | internal class ConfigurationSerializer : IConfigurationSerializer |
8 | 6 | { |
9 | | - private static IDeserializer Deserializer => new DeserializerBuilder() |
10 | | - .WithNamingConvention(HyphenatedNamingConvention.Instance) |
11 | | - .WithTypeConverter(VersionStrategiesConverter.Instance) |
12 | | - .WithTypeInspector(inspector => new JsonPropertyNameInspector(inspector)) |
13 | | - .Build(); |
14 | | - |
15 | | - private static ISerializer Serializer => new SerializerBuilder() |
16 | | - .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull) |
17 | | - .WithTypeInspector(inspector => new JsonPropertyNameInspector(inspector)) |
18 | | - .WithNamingConvention(HyphenatedNamingConvention.Instance).Build(); |
19 | | - |
20 | | - public T Deserialize<T>(string input) => Deserializer.Deserialize<T>(input); |
21 | | - public string Serialize(object graph) => Serializer.Serialize(graph); |
| 7 | + private static readonly ConfigurationYamlContext GeneratedContext = ConfigurationYamlContext.Default; |
| 8 | + |
| 9 | + private static readonly YamlSerializerOptions SerializerOptions = new() |
| 10 | + { |
| 11 | + PropertyNamingPolicy = HyphenatedJsonNamingPolicy.Instance, |
| 12 | + DefaultIgnoreCondition = YamlIgnoreCondition.WhenWritingNull, |
| 13 | + Converters = [VersionStrategiesConverter.Instance] |
| 14 | + }; |
| 15 | + |
| 16 | + public T Deserialize<T>(string input) |
| 17 | + { |
| 18 | + if (typeof(T) == typeof(Dictionary<object, object?>)) |
| 19 | + { |
| 20 | + var graph = YamlSerializer.Deserialize<Dictionary<string, object?>>(input, SerializerOptions); |
| 21 | + return (T)(object)ConvertToObjectDictionary(graph); |
| 22 | + } |
| 23 | + |
| 24 | + if (typeof(T) == typeof(GitVersionConfiguration)) |
| 25 | + { |
| 26 | + try |
| 27 | + { |
| 28 | + return (T)(object)YamlSerializer.Deserialize<GitVersionConfiguration>(input, GeneratedContext)!; |
| 29 | + } |
| 30 | + catch (Exception exception) when (exception is not YamlException) |
| 31 | + { |
| 32 | + throw new YamlException(exception.Message, exception); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return YamlSerializer.Deserialize<T>(input, SerializerOptions)!; |
| 37 | + } |
| 38 | + |
| 39 | + public string Serialize(object graph) |
| 40 | + => YamlSerializer.Serialize(graph, SerializerOptions); |
| 41 | + |
22 | 42 | public IGitVersionConfiguration? ReadConfiguration(string input) => Deserialize<GitVersionConfiguration?>(input); |
23 | 43 |
|
24 | | - private sealed class JsonPropertyNameInspector(ITypeInspector innerTypeDescriptor) : TypeInspectorSkeleton |
| 44 | + private static Dictionary<object, object?> ConvertToObjectDictionary(IReadOnlyDictionary<string, object?>? source) |
25 | 45 | { |
26 | | - public override string GetEnumName(Type enumType, string name) => innerTypeDescriptor.GetEnumName(enumType, name); |
| 46 | + if (source is null) return []; |
27 | 47 |
|
28 | | - public override string GetEnumValue(object enumValue) => innerTypeDescriptor.GetEnumValue(enumValue); |
| 48 | + Dictionary<object, object?> result = []; |
| 49 | + foreach (var item in source) |
| 50 | + { |
| 51 | + result[item.Key] = ConvertValue(item.Value); |
| 52 | + } |
29 | 53 |
|
30 | | - public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object? container) => |
31 | | - innerTypeDescriptor.GetProperties(type, container) |
32 | | - .Where(p => p.GetCustomAttribute<JsonIgnoreAttribute>() == null) |
33 | | - .Select(IPropertyDescriptor (p) => |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + private static object? ConvertValue(object? value) => value switch |
| 58 | + { |
| 59 | + IReadOnlyDictionary<string, object?> dictionary => ConvertToObjectDictionary(dictionary), |
| 60 | + IDictionary<string, object?> dictionary => ConvertToObjectDictionary(new Dictionary<string, object?>(dictionary)), |
| 61 | + IList list => ConvertList(list), |
| 62 | + _ => value |
| 63 | + }; |
| 64 | + |
| 65 | + private static List<object?> ConvertList(IList list) |
| 66 | + { |
| 67 | + List<object?> result = []; |
| 68 | + foreach (var item in list) |
| 69 | + { |
| 70 | + result.Add(ConvertValue(item)); |
| 71 | + } |
| 72 | + |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + private sealed class HyphenatedJsonNamingPolicy : JsonNamingPolicy |
| 77 | + { |
| 78 | + public static JsonNamingPolicy Instance { get; } = new HyphenatedJsonNamingPolicy(); |
| 79 | + |
| 80 | + public override string ConvertName(string name) |
| 81 | + { |
| 82 | + if (string.IsNullOrEmpty(name)) return name; |
| 83 | + |
| 84 | + var builder = new StringBuilder(name.Length + 8); |
| 85 | + for (var index = 0; index < name.Length; index++) |
| 86 | + { |
| 87 | + var current = name[index]; |
| 88 | + if (char.IsUpper(current)) |
34 | 89 | { |
35 | | - var descriptor = new PropertyDescriptor(p); |
36 | | - var member = p.GetCustomAttribute<JsonPropertyNameAttribute>(); |
37 | | - if (member is not null) |
| 90 | + var hasPrevious = index > 0; |
| 91 | + var hasNext = index + 1 < name.Length; |
| 92 | + var previousIsLowerOrDigit = hasPrevious && (char.IsLower(name[index - 1]) || char.IsDigit(name[index - 1])); |
| 93 | + var nextIsLower = hasNext && char.IsLower(name[index + 1]); |
| 94 | + |
| 95 | + if (hasPrevious && (previousIsLowerOrDigit || nextIsLower)) |
38 | 96 | { |
39 | | - descriptor.Name = member.Name; |
| 97 | + builder.Append('-'); |
40 | 98 | } |
41 | 99 |
|
42 | | - return descriptor; |
43 | | - }) |
44 | | - .OrderBy(p => p.Order); |
| 100 | + builder.Append(char.ToLowerInvariant(current)); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + builder.Append(current); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return builder.ToString(); |
| 109 | + } |
45 | 110 | } |
46 | 111 | } |
0 commit comments