|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using Newtonsoft.Json.Serialization; |
| 7 | +using YamlDotNet.Serialization; |
| 8 | +using YamlDotNet.Serialization.NamingConventions; |
| 9 | + |
| 10 | +namespace KubeOps.Operator.Serialization |
| 11 | +{ |
| 12 | + internal class NamingConvention : CamelCasePropertyNamesContractResolver, INamingConvention |
| 13 | + { |
| 14 | + private readonly INamingConvention _yamlNaming = CamelCaseNamingConvention.Instance; |
| 15 | + |
| 16 | + private readonly IDictionary<string, string> _rename = new Dictionary<string, string> |
| 17 | + { |
| 18 | + {"namespaceProperty", "namespace"}, |
| 19 | + {"enumProperty", "enum"}, |
| 20 | + {"objectProperty", "object"}, |
| 21 | + }; |
| 22 | + |
| 23 | + public string Apply(string value) |
| 24 | + { |
| 25 | + var (key, renamedValue) = _rename.FirstOrDefault(p => |
| 26 | + string.Equals(value, p.Key, StringComparison.InvariantCultureIgnoreCase)); |
| 27 | + |
| 28 | + if (key != default) |
| 29 | + { |
| 30 | + value = renamedValue; |
| 31 | + } |
| 32 | + |
| 33 | + return _yamlNaming.Apply(value); |
| 34 | + } |
| 35 | + |
| 36 | + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) |
| 37 | + { |
| 38 | + var property = base.CreateProperty(member, memberSerialization); |
| 39 | + |
| 40 | + var (key, renamedValue) = _rename.FirstOrDefault(p => |
| 41 | + string.Equals(property.PropertyName, p.Key, StringComparison.InvariantCultureIgnoreCase)); |
| 42 | + |
| 43 | + if (key != default) |
| 44 | + { |
| 45 | + property.PropertyName = renamedValue; |
| 46 | + } |
| 47 | + |
| 48 | + return property; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments