1
1
using System . Diagnostics . CodeAnalysis ;
2
- using System . Reflection ;
3
2
4
3
namespace GqlPlus . Structures ;
5
4
@@ -89,20 +88,16 @@ public Structured AddIf(bool optional, Func<Structured, Structured>? onTrue = nu
89
88
public Structured AddSet < TEnum > ( string key , TEnum set , string ? tag = null , bool flow = true )
90
89
where TEnum : Enum
91
90
{
92
- Type type = typeof ( TEnum ) ;
91
+ string [ ] ? flags = set . FlagNames ( ) ;
93
92
94
- if ( type . GetCustomAttributes < FlagsAttribute > ( ) . Any ( ) ) {
95
- int flags = ( int ) ( object ) set ;
93
+ if ( flags is not null ) {
96
94
Dict result = [ ] ;
97
95
98
- foreach ( object ? value in Enum . GetValues ( type ) ) {
99
- int flag = ( int ) value ;
100
- if ( flag . IsSingleFlag ( ) && ( flags & flag ) == flag ) {
101
- result . Add ( new ( Enum . GetName ( type , value ) ) , new ( "_" ) ) ;
102
- }
96
+ foreach ( string flag in flags ) {
97
+ result . Add ( new ( flag ) , new ( "_" ) ) ;
103
98
}
104
99
105
- return Add ( key , new ( result , $ "_Set({ tag ?? type . TypeTag ( ) } )", flow : flow ) ) ;
100
+ return Add ( key , new ( result , $ "_Set({ tag ?? typeof ( TEnum ) . TypeTag ( ) } )", flow : flow ) ) ;
106
101
}
107
102
108
103
return this ;
@@ -111,16 +106,13 @@ public Structured AddSet<TEnum>(string key, TEnum set, string? tag = null, bool
111
106
public bool Equals ( Structured ? other )
112
107
=> string . Equals ( Tag , other ? . Tag , StringComparison . Ordinal )
113
108
&& ( Value . BothValued ( other ? . Value ) ? Value . Equals ( other . Value )
114
- : List . BothValued ( other ? . List ) ? List . SequenceEqual ( other . List )
115
- : Map . BothValued ( other ? . Map ) && Map . Equals ( other . Map ) ) ;
109
+ : List . BothAny ( other ? . List ) ? List . SequenceEqual ( other . List )
110
+ : Map . BothAny ( other ? . Map ) && Map . Equals ( other . Map ) ) ;
116
111
117
112
public override bool Equals ( object ? obj )
118
113
=> Equals ( obj as Structured ) ;
119
114
public override int GetHashCode ( )
120
- => HashCode . Combine ( Tag ,
121
- Value ? . GetHashCode ( ) ?? 0 ,
122
- List ? . GetHashCode ( ) ?? 0 ,
123
- Map ? . GetHashCode ( ) ?? 0 ) ;
115
+ => HashCode . Combine ( Tag , Value ? . GetHashCode ( ) ?? 0 , List . GetHashCode ( ) , Map . GetHashCode ( ) ) ;
124
116
}
125
117
126
118
#pragma warning disable CA1034 // Nested types should not be visible
@@ -134,10 +126,16 @@ internal sealed class Dict
134
126
internal Dict ( ) : base ( ) { }
135
127
internal Dict ( IDictionary < TValue , TObject > dictionary )
136
128
: base ( dictionary ) { }
129
+
130
+ public bool Equals ( IDict other )
131
+ => other is not null
132
+ && Keys . OrderedEqual ( other . Keys )
133
+ && Keys . All ( k => this [ k ] . Equals ( other [ k ] ) ) ;
137
134
}
138
135
139
136
public interface IDict
140
137
: IDictionary < TValue , TObject >
138
+ , IEquatable < IDict >
141
139
{ }
142
140
143
141
public TValue ? Value { get ; }
0 commit comments