@@ -20,103 +20,49 @@ public class ModelTypeMapping
2020 public string [ ] ? Usage { get ; }
2121 public string [ ] ? Formats { get ; }
2222
23- public ModelTypeMapping ( INamedTypeSymbol modelAttribute , INamedTypeSymbol memberAttribute , INamedTypeSymbol serializationAttribute , INamedTypeSymbol serializationHooksAttribute , INamedTypeSymbol ? existingType )
23+ public ModelTypeMapping ( CodeGenAttributes codeGenAttributes , INamedTypeSymbol existingType )
2424 {
2525 _existingType = existingType ;
2626 _propertyMappings = new ( ) ;
2727 _serializationMappings = new ( SymbolEqualityComparer . Default ) ;
2828
2929 foreach ( ISymbol member in GetMembers ( existingType ) )
3030 {
31+ string [ ] ? serializationPath = null ;
32+ ( string ? SerializationHook , string ? DeserializationHook ) ? serializationHooks = null ;
3133 foreach ( var attributeData in member . GetAttributes ( ) )
3234 {
33- var attributeTypeSymbol = attributeData . AttributeClass ;
3435 // handle CodeGenMember attribute
35- if ( SymbolEqualityComparer . Default . Equals ( attributeTypeSymbol , memberAttribute ) && TryGetCodeGenMemberAttributeValue ( member , attributeData , out var schemaMemberName ) )
36+ if ( codeGenAttributes . TryGetCodeGenMemberAttributeValue ( attributeData , out var schemaMemberName ) )
3637 {
3738 _propertyMappings . Add ( schemaMemberName , member ) ;
3839 }
39- string [ ] ? serializationPath = null ;
40- ( string ? SerializationHook , string ? DeserializationHook ) ? serializationHooks = null ;
41- if ( SymbolEqualityComparer . Default . Equals ( attributeTypeSymbol , serializationAttribute ) && TryGetSerializationAttributeValue ( member , attributeData , out var pathResult ) )
40+ // handle CodeGenMemberSerialization attribute
41+ if ( codeGenAttributes . TryGetCodeGenMemberSerializationAttributeValue ( attributeData , out var pathResult ) )
4242 {
4343 serializationPath = pathResult ;
4444 }
45- if ( SymbolEqualityComparer . Default . Equals ( attributeTypeSymbol , serializationHooksAttribute ) && TryGetSerializationHooks ( member , attributeData , out var hooks ) )
45+ // handle CodeGenMemberSerializationHooks attribute
46+ if ( codeGenAttributes . TryGetCodeGenMemberSerializationHooksAttributeValue ( attributeData , out var hooks ) )
4647 {
4748 serializationHooks = hooks ;
4849 }
49- if ( serializationPath != null || serializationHooks != null )
50- {
51- _serializationMappings . Add ( member , new SourcePropertySerializationMapping ( member , serializationPath , serializationHooks ? . SerializationHook , serializationHooks ? . DeserializationHook ) ) ;
52- }
5350 }
54- }
55-
56- if ( existingType != null )
57- {
58- foreach ( var attributeData in existingType . GetAttributes ( ) )
51+ if ( serializationPath != null || serializationHooks != null )
5952 {
60- if ( SymbolEqualityComparer . Default . Equals ( attributeData . AttributeClass , modelAttribute ) )
61- {
62- foreach ( var namedArgument in attributeData . NamedArguments )
63- {
64- switch ( namedArgument . Key )
65- {
66- case nameof ( CodeGenModelAttribute . Usage ) :
67- Usage = ToStringArray ( namedArgument . Value . Values ) ;
68- break ;
69- case nameof ( CodeGenModelAttribute . Formats ) :
70- Formats = ToStringArray ( namedArgument . Value . Values ) ;
71- break ;
72- }
73- }
74- }
53+ _serializationMappings . Add ( member , new SourcePropertySerializationMapping ( member , serializationPath , serializationHooks ? . SerializationHook , serializationHooks ? . DeserializationHook ) ) ;
7554 }
7655 }
77- }
78-
79- private static bool TryGetSerializationHooks ( ISymbol symbol , AttributeData attributeData , out ( string ? SerializationHook , string ? DeserializationHook ) hooks )
80- {
81- string ? serializationHook = null ;
82- string ? deserializationHook = null ;
83-
84- var arguments = attributeData . ConstructorArguments ;
85- serializationHook = arguments [ 0 ] . Value as string ;
86- deserializationHook = arguments [ 1 ] . Value as string ;
8756
88- hooks = ( serializationHook , deserializationHook ) ;
89- return serializationHook != null || deserializationHook != null ;
90- }
91-
92- private static bool TryGetCodeGenMemberAttributeValue ( ISymbol symbol , AttributeData attributeData , [ MaybeNullWhen ( false ) ] out string name )
93- {
94- name = attributeData . ConstructorArguments . FirstOrDefault ( ) . Value as string ;
95- return name != null ;
96- }
97-
98- private static bool TryGetSerializationAttributeValue ( ISymbol symbol , AttributeData attributeData , [ MaybeNullWhen ( false ) ] out string [ ] propertyNames )
99- {
100- propertyNames = null ;
101- if ( attributeData . ConstructorArguments . Length > 0 )
57+ foreach ( var attributeData in existingType . GetAttributes ( ) )
10258 {
103- propertyNames = ToStringArray ( attributeData . ConstructorArguments [ 0 ] . Values ) ;
104- }
105-
106- return propertyNames != null ;
107- }
108-
109- private static string [ ] ? ToStringArray ( ImmutableArray < TypedConstant > values )
110- {
111- if ( values . IsDefaultOrEmpty )
112- {
113- return null ;
59+ // handle CodeGenModel attribute
60+ if ( codeGenAttributes . TryGetCodeGenModelAttributeValue ( attributeData , out var usage , out var formats ) )
61+ {
62+ Usage = usage ;
63+ Formats = formats ;
64+ }
11465 }
115-
116- return values
117- . Select ( v => ( string ? ) v . Value )
118- . OfType < string > ( )
119- . ToArray ( ) ;
12066 }
12167
12268 public SourceMemberMapping ? GetForMember ( string name )
0 commit comments