1
- using System ;
2
1
using System . IO ;
3
2
using GitVersion . Model . Configuration ;
4
- using YamlDotNet . Core ;
5
- using YamlDotNet . Core . Events ;
6
3
using YamlDotNet . Serialization ;
7
4
using YamlDotNet . Serialization . NamingConventions ;
8
5
@@ -14,7 +11,6 @@ public static Config Read(TextReader reader)
14
11
{
15
12
var deserializer = new DeserializerBuilder ( )
16
13
. WithNamingConvention ( HyphenatedNamingConvention . Instance )
17
- . WithTypeConverter ( new YamlNullableEnumTypeConverter ( ) )
18
14
. Build ( ) ;
19
15
var deserialize = deserializer . Deserialize < Config > ( reader ) ;
20
16
return deserialize ?? new Config ( ) ;
@@ -25,70 +21,8 @@ public static void Write(Config config, TextWriter writer)
25
21
var serializer = new SerializerBuilder ( )
26
22
. ConfigureDefaultValuesHandling ( DefaultValuesHandling . OmitDefaults )
27
23
. WithNamingConvention ( HyphenatedNamingConvention . Instance )
28
- . WithTypeConverter ( new YamlNullableEnumTypeConverter ( ) )
29
24
. Build ( ) ;
30
25
serializer . Serialize ( writer , config ) ;
31
26
}
32
27
}
33
-
34
- public class YamlNullableEnumTypeConverter : IYamlTypeConverter
35
- {
36
- public bool Accepts ( Type type )
37
- {
38
- return Nullable . GetUnderlyingType ( type ) ? . IsEnum ?? false ;
39
- }
40
-
41
- public object ReadYaml ( IParser parser , Type type )
42
- {
43
- type = Nullable . GetUnderlyingType ( type ) ?? throw new ArgumentException ( "Expected nullable enum type for ReadYaml" ) ;
44
-
45
- if ( parser . Accept < NodeEvent > ( out var @event ) )
46
- {
47
- if ( NodeIsNull ( @event ) )
48
- {
49
- parser . SkipThisAndNestedEvents ( ) ;
50
- return null ;
51
- }
52
- }
53
-
54
- var scalar = parser . Consume < Scalar > ( ) ;
55
- try
56
- {
57
- return Enum . Parse ( type , scalar . Value , ignoreCase : true ) ;
58
- }
59
- catch ( Exception ex )
60
- {
61
- throw new Exception ( $ "Invalid value: \" { scalar . Value } \" for { type . Name } ", ex ) ;
62
- }
63
- }
64
-
65
- public void WriteYaml ( IEmitter emitter , object value , Type type )
66
- {
67
- type = Nullable . GetUnderlyingType ( type ) ?? throw new ArgumentException ( "Expected nullable enum type for WriteYaml" ) ;
68
-
69
- if ( value != null )
70
- {
71
- var toWrite = Enum . GetName ( type , value ) ?? throw new InvalidOperationException ( $ "Invalid value { value } for enum: { type } ") ;
72
- emitter . Emit ( new Scalar ( null , null , toWrite , ScalarStyle . Any , true , false ) ) ;
73
- }
74
- }
75
-
76
- private static bool NodeIsNull ( NodeEvent nodeEvent )
77
- {
78
- // http://yaml.org/type/null.html
79
-
80
- if ( nodeEvent . Tag == "tag:yaml.org,2002:null" )
81
- {
82
- return true ;
83
- }
84
-
85
- if ( nodeEvent is Scalar scalar && scalar . Style == ScalarStyle . Plain )
86
- {
87
- var value = scalar . Value ;
88
- return value is "" or "~" or "null" or "Null" or "NULL" ;
89
- }
90
-
91
- return false ;
92
- }
93
- }
94
28
}
0 commit comments