1+ // Copyright (c) The LEGO Group. All rights reserved.
2+
3+ namespace LEGO . AsyncAPI . Bindings . Sqs ;
4+
5+ using System ;
6+ using System . Collections . Generic ;
7+ using System . Linq ;
8+ using LEGO . AsyncAPI . Models . Interfaces ;
9+ using LEGO . AsyncAPI . Readers . ParseNodes ;
10+ using LEGO . AsyncAPI . Writers ;
11+
12+ public class Condition : IAsyncApiElement
13+ {
14+ public Dictionary < string , Dictionary < string , StringOrStringList > > Value { get ; private set ; }
15+
16+ public Condition ( Dictionary < string , Dictionary < string , StringOrStringList > > value )
17+ {
18+ this . Value = value ;
19+ }
20+
21+ public void Serialize ( IAsyncApiWriter writer )
22+ {
23+ if ( writer is null )
24+ {
25+ throw new ArgumentNullException ( nameof ( writer ) ) ;
26+ }
27+
28+ writer . WriteStartObject ( ) ;
29+ foreach ( var conditionValue in this . Value )
30+ {
31+ writer . WriteRequiredMap ( conditionValue . Key , conditionValue . Value , ( w , t ) => t . Value . Write ( w ) ) ;
32+ }
33+
34+ writer . WriteEndObject ( ) ;
35+ }
36+
37+ public static Condition Parse ( ParseNode node )
38+ {
39+ switch ( node )
40+ {
41+ case MapNode mapNode :
42+ {
43+ var conditionValues = new Dictionary < string , Dictionary < string , StringOrStringList > > ( ) ;
44+ foreach ( var conditionNode in mapNode )
45+ {
46+ switch ( conditionNode . Value )
47+ {
48+ case MapNode conditionValueNode :
49+ conditionValues . Add ( conditionNode . Name , new Dictionary < string , StringOrStringList > ( conditionValueNode . Select ( x =>
50+ new KeyValuePair < string , StringOrStringList > ( x . Name , StringOrStringList . Parse ( x . Value ) ) )
51+ . ToDictionary ( x => x . Key , x => x . Value ) ) ) ;
52+ break ;
53+ default :
54+ throw new ArgumentException ( $ "An error occured while parsing a { nameof ( Condition ) } node. " +
55+ $ "AWS condition values should be one or more key value pairs.") ;
56+ }
57+ }
58+
59+ return new Condition ( conditionValues ) ;
60+ }
61+
62+ default :
63+ throw new ArgumentException ( $ "An error occured while parsing a { nameof ( Condition ) } node. " +
64+ $ "Node should contain a collection of AWS condition types.") ;
65+ }
66+ }
67+ }
0 commit comments