@@ -92,6 +92,63 @@ private void Execute(SourceProductionContext spc,
9292
9393 #endregion
9494
95+ #region Get Enum Fields
96+
97+ private static ImmutableArray < EnumField > GetEnumFields ( SourceProductionContext spc , INamedTypeSymbol enumSymbol , string enumFullName )
98+ {
99+ // Iterate through enum members and get enum fields
100+ var enumFields = new List < EnumField > ( ) ;
101+ var enumError = false ;
102+ foreach ( var member in enumSymbol . GetMembers ( ) . Where ( m => m . Kind == SymbolKind . Field ) )
103+ {
104+ if ( member is IFieldSymbol fieldSymbol )
105+ {
106+ var enumFieldName = fieldSymbol . Name ;
107+
108+ // Check if the field has the EnumLocalizeKey attribute
109+ var keyAttr = fieldSymbol . GetAttributes ( )
110+ . FirstOrDefault ( a => a . AttributeClass ? . Name == Constants . EnumLocalizeKeyAttributeName ) ;
111+ var keyAttrExist = keyAttr != null ;
112+
113+ // Check if the field has the EnumLocalizeValue attribute
114+ var valueAttr = fieldSymbol . GetAttributes ( )
115+ . FirstOrDefault ( a => a . AttributeClass ? . Name == Constants . EnumLocalizeValueAttributeName ) ;
116+ var valueAttrExist = valueAttr != null ;
117+
118+ // Get the key and value from the attributes
119+ var key = keyAttr ? . ConstructorArguments . FirstOrDefault ( ) . Value ? . ToString ( ) ?? string . Empty ;
120+ var value = valueAttr ? . ConstructorArguments . FirstOrDefault ( ) . Value ? . ToString ( ) ?? string . Empty ;
121+
122+ if ( keyAttrExist && ! string . IsNullOrEmpty ( key ) )
123+ {
124+ // If localization key exists and is valid, use it
125+ enumFields . Add ( new EnumField ( enumFieldName , key , valueAttrExist ? value : null ) ) ;
126+ }
127+ else if ( valueAttrExist )
128+ {
129+ // If localization value exists, use it
130+ enumFields . Add ( new EnumField ( enumFieldName , value ) ) ;
131+ }
132+ else
133+ {
134+ // If localization key and value are not provided, do not generate the field and report a diagnostic
135+ spc . ReportDiagnostic ( Diagnostic . Create (
136+ SourceGeneratorDiagnostics . EnumFieldLocalizationKeyValueInvalid ,
137+ Location . None ,
138+ $ "{ enumFullName } .{ enumFieldName } ") ) ;
139+ enumError = true ;
140+ }
141+ }
142+ }
143+
144+ // If there was an error, do not generate the class
145+ if ( enumError ) return _emptyEnumFields ;
146+
147+ return enumFields . ToImmutableArray ( ) ;
148+ }
149+
150+ #endregion
151+
95152 #region Generate Source
96153
97154 private void GenerateSource (
@@ -259,63 +316,6 @@ private static void GenerateUpdateLabelsMethod(
259316
260317 #endregion
261318
262- #region Get Enum Fields
263-
264- private static ImmutableArray < EnumField > GetEnumFields ( SourceProductionContext spc , INamedTypeSymbol enumSymbol , string enumFullName )
265- {
266- // Iterate through enum members and get enum fields
267- var enumFields = new List < EnumField > ( ) ;
268- var enumError = false ;
269- foreach ( var member in enumSymbol . GetMembers ( ) . Where ( m => m . Kind == SymbolKind . Field ) )
270- {
271- if ( member is IFieldSymbol fieldSymbol )
272- {
273- var enumFieldName = fieldSymbol . Name ;
274-
275- // Check if the field has the EnumLocalizeKey attribute
276- var keyAttr = fieldSymbol . GetAttributes ( )
277- . FirstOrDefault ( a => a . AttributeClass ? . Name == Constants . EnumLocalizeKeyAttributeName ) ;
278- var keyAttrExist = keyAttr != null ;
279-
280- // Check if the field has the EnumLocalizeValue attribute
281- var valueAttr = fieldSymbol . GetAttributes ( )
282- . FirstOrDefault ( a => a . AttributeClass ? . Name == Constants . EnumLocalizeValueAttributeName ) ;
283- var valueAttrExist = valueAttr != null ;
284-
285- // Get the key and value from the attributes
286- var key = keyAttr ? . ConstructorArguments . FirstOrDefault ( ) . Value ? . ToString ( ) ?? string . Empty ;
287- var value = valueAttr ? . ConstructorArguments . FirstOrDefault ( ) . Value ? . ToString ( ) ?? string . Empty ;
288-
289- if ( keyAttrExist && ! string . IsNullOrEmpty ( key ) )
290- {
291- // If localization key exists and is valid, use it
292- enumFields . Add ( new EnumField ( enumFieldName , key , valueAttrExist ? value : null ) ) ;
293- }
294- else if ( valueAttrExist )
295- {
296- // If localization value exists, use it
297- enumFields . Add ( new EnumField ( enumFieldName , value ) ) ;
298- }
299- else
300- {
301- // If localization key and value are not provided, do not generate the field and report a diagnostic
302- spc . ReportDiagnostic ( Diagnostic . Create (
303- SourceGeneratorDiagnostics . EnumFieldLocalizationKeyValueInvalid ,
304- Location . None ,
305- $ "{ enumFullName } .{ enumFieldName } ") ) ;
306- enumError = true ;
307- }
308- }
309- }
310-
311- // If there was an error, do not generate the class
312- if ( enumError ) return _emptyEnumFields ;
313-
314- return enumFields . ToImmutableArray ( ) ;
315- }
316-
317- #endregion
318-
319319 #region Classes
320320
321321 public class EnumField
0 commit comments