@@ -31,7 +31,7 @@ private void Generate(SourceProductionContext context, ImmutableArray<GeneratorA
3131 string namespaceName = group . Key . GetNamespace ( ) ;
3232
3333 SourceBuilder builder = new SourceBuilder ( ) ;
34- builder . AddUsings ( "System.Globalization" , "System.Text" , "System.Text.RegularExpressions" , "Microsoft.AspNetCore.WebUtilities" , "ImageWizard" , "ImageWizard.Attributes" , "ImageWizard.Processing" ) ;
34+ builder . AddUsings ( "System.Globalization" , "System.Text" , "System.Text.RegularExpressions" , "Microsoft.AspNetCore.WebUtilities" , "ImageWizard" , "ImageWizard.Attributes" , "ImageWizard.Processing" , "ImageWizard.Utils" ) ;
3535 builder . AddNamespace ( namespaceName , x =>
3636 {
3737 x . AddClass ( Modifier . Public , group . Key . Identifier . Text , x =>
@@ -120,7 +120,7 @@ or SpecialType.System_Decimal
120120 } ) ;
121121 }
122122
123- return $@ "\\({ string . Join ( "," , parameterItems . Select ( x => x . Pattern ) . ToArray ( ) ) } \\)";
123+ return $@ "^ \\({ string . Join ( "," , parameterItems . Select ( x => x . Pattern ) . ToArray ( ) ) } \\)$ ";
124124 }
125125
126126 private string CreateParameterParser ( IMethodSymbol methodSymbol )
@@ -138,26 +138,55 @@ private string CreateParameterParser(IMethodSymbol methodSymbol)
138138 }
139139 else
140140 {
141- m = parameterSymbol . Type . OriginalDefinition . SpecialType switch
141+ ParameterType parameterType = parameterSymbol . Type . OriginalDefinition . SpecialType switch
142142 {
143- SpecialType . System_Byte => $ "byte { parameterSymbol . Name } = byte.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
144- SpecialType . System_Int16 => $ "short { parameterSymbol . Name } = short.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
145- SpecialType . System_Int32 => $ "int { parameterSymbol . Name } = int.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
146- SpecialType . System_Int64 => $ "long { parameterSymbol . Name } = long.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
147- SpecialType . System_Single => $ "float { parameterSymbol . Name } = float.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
148- SpecialType . System_Double => $ "double { parameterSymbol . Name } = double.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
149- SpecialType . System_Decimal => $ "decimal { parameterSymbol . Name } = decimal.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);" ,
150- SpecialType . System_Boolean => $ "bool { parameterSymbol . Name } = bool.Parse(group[ \" { parameterSymbol . Name } \" ].ValueSpan);" ,
151- SpecialType . System_String => $ "string { parameterSymbol . Name } = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(group[ \" { parameterSymbol . Name } \" ].Value));" ,
143+ SpecialType . System_Byte => new ParameterType ( "byte" , true , true , true ) ,
144+ SpecialType . System_Int16 => new ParameterType ( "short" , true , true , true ) ,
145+ SpecialType . System_Int32 => new ParameterType ( "int" , true , true , true ) ,
146+ SpecialType . System_Int64 => new ParameterType ( "long" , true , true , true ) ,
147+ SpecialType . System_Single => new ParameterType ( "float" , true , true , true ) ,
148+ SpecialType . System_Double => new ParameterType ( "double" , true , true , true ) ,
149+ SpecialType . System_Decimal => new ParameterType ( "decimal" , true , true , true ) ,
150+ SpecialType . System_Boolean => new ParameterType ( "bool" , true , false , false ) ,
151+ SpecialType . System_String => new ParameterType ( "string" , false , false , false ) ,
152152
153153 _ => throw new Exception ( )
154154 } ;
155+
156+ //use string
157+ if ( parameterType . UseParsing == false )
158+ {
159+ m = $ "{ parameterType . Name } { parameterSymbol . Name } = group[\" { parameterSymbol . Name } \" ].Value;";
160+ }
161+ else
162+ {
163+ //parse value
164+ if ( parameterType . UseCultureInfoForParsing )
165+ {
166+ m = $ "{ parameterType . Name } { parameterSymbol . Name } = { parameterType . Name } .Parse(group[\" { parameterSymbol . Name } \" ].ValueSpan, CultureInfo.InvariantCulture);";
167+ }
168+ else
169+ {
170+ m = $ "{ parameterType . Name } { parameterSymbol . Name } = { parameterType . Name } .Parse(group[\" { parameterSymbol . Name } \" ].ValueSpan);";
171+ }
172+
173+ //check DPR attribute
174+ if ( parameterSymbol . GetAttributes ( ) is ImmutableArray < AttributeData > attributes && attributes . Length > 0 )
175+ {
176+ string dpr = attributes [ 0 ] . AttributeClass . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
177+
178+ if ( dpr == "global::ImageWizard.Attributes.DPRAttribute" )
179+ {
180+ m += $ "if (filter.Context.ProcessingContext.ClientHints.DPR != null) {{ { parameterSymbol . Name } = ({ parameterType . Name } ) ((double){ parameterSymbol . Name } * filter.Context.ProcessingContext.ClientHints.DPR.Value); }}";
181+ }
182+ }
183+ }
155184 }
156185
157186 builder . Append ( m ) ;
158187 }
159188
160- builder . Append ( $ "filter.{ methodSymbol . Name } ({ string . Join ( "," , methodSymbol . Parameters . Select ( x=> x . Name ) ) } );") ;
189+ builder . Append ( $ "filter.{ methodSymbol . Name } ({ string . Join ( "," , methodSymbol . Parameters . Select ( x => x . Name ) ) } );") ;
161190
162191 return builder . ToString ( ) . Trim ( ) ;
163192 }
@@ -171,3 +200,20 @@ class ParameterItem
171200
172201 public string CodeLine { get ; set ; }
173202}
203+
204+ class ParameterType
205+ {
206+ public ParameterType ( string name , bool useParsing , bool useCultureInfoForParsing , bool useDPR )
207+ {
208+ Name = name ;
209+ UseParsing = useParsing ;
210+ UseCultureInfoForParsing = useCultureInfoForParsing ;
211+ UseDPR = useDPR ;
212+ }
213+
214+ public string Name { get ; set ; }
215+ public bool UseParsing { get ; set ; }
216+ public bool UseCultureInfoForParsing { get ; set ; }
217+
218+ public bool UseDPR { get ; set ; }
219+ }
0 commit comments