@@ -89,8 +89,8 @@ private static bool IsPartialClass(SyntaxNode node)
8989 DefaultValue = GetDefaultValue ( f ) ,
9090 IsEnum = f . Type . TypeKind == TypeKind . Enum ,
9191 EnumSymbol = f . Type . TypeKind == TypeKind . Enum ? f . Type as INamedTypeSymbol : null ,
92- Limit = GetCustomLimit ( f ) ,
9392 Minimum = GetCustomMinimum ( f ) ,
93+ Maximum = GetCustomMaximum ( f ) ,
9494 CustomSerializerName = GetCustomFlagSerializer ( f ) ,
9595 } ) . ToList ( ) ;
9696
@@ -153,29 +153,29 @@ private static bool HasReactiveAttribute(IFieldSymbol field)
153153 . Any ( attr => attr . AttributeClass ? . Name . StartsWith ( "Reactive" ) ?? false ) ;
154154 }
155155
156- private static int ? GetCustomLimit ( IFieldSymbol property )
156+ private static int ? GetCustomMinimum ( IFieldSymbol property )
157157 {
158158 var customAttr = property . GetAttributes ( )
159- . FirstOrDefault ( attr => attr . AttributeClass ? . Name . StartsWith ( "Limit " ) ?? false ) ;
159+ . FirstOrDefault ( attr => attr . AttributeClass ? . Name . StartsWith ( "Minimum " ) ?? false ) ;
160160
161161 if ( customAttr ? . ConstructorArguments . Length > 0 &&
162- customAttr . ConstructorArguments [ 0 ] . Value is int limit )
162+ customAttr . ConstructorArguments [ 0 ] . Value is int minimum )
163163 {
164- return limit ;
164+ return minimum ;
165165 }
166166
167167 return null ;
168168 }
169169
170- private static int ? GetCustomMinimum ( IFieldSymbol property )
170+ private static int ? GetCustomMaximum ( IFieldSymbol property )
171171 {
172172 var customAttr = property . GetAttributes ( )
173- . FirstOrDefault ( attr => attr . AttributeClass ? . Name . StartsWith ( "Minimum " ) ?? false ) ;
173+ . FirstOrDefault ( attr => attr . AttributeClass ? . Name . StartsWith ( "Maximum " ) ?? false ) ;
174174
175175 if ( customAttr ? . ConstructorArguments . Length > 0 &&
176- customAttr . ConstructorArguments [ 0 ] . Value is int minimum )
176+ customAttr . ConstructorArguments [ 0 ] . Value is int maximum )
177177 {
178- return minimum ;
178+ return maximum ;
179179 }
180180
181181 return null ;
@@ -393,15 +393,21 @@ private static void GenerateSerializerList(StringBuilder sb, List<SerializedFiel
393393
394394 private static string GetSerializeCall ( SerializedFieldInfo field )
395395 {
396- var limitExpression = field . Limit != null ? $ "{ field . Limit } " : "null" ;
397- var minExpression = $ "{ field . Minimum ?? 0 } ";
398396 var output = new StringBuilder ( ) ;
399- if ( field . FieldType is "int" or "int?" && field . Limit == null )
400- output . AppendLine ( $ "#error Numeric type { field . FieldName } must have a `Limit` attribute!") ;
397+ if ( field . FieldType is "int" or "int?" )
398+ {
399+ if ( field . Minimum == null )
400+ {
401+ output . AppendLine ( $ "#error Numeric type { field . FieldName } must have a `Minimum` attribute!") ;
402+ }
403+ if ( field . Maximum == null )
404+ {
405+ output . AppendLine ( $ "#error Numeric type { field . FieldName } must have a `Maximum` attribute!") ;
406+ }
407+ }
401408 output . Append ( field . FieldType switch
402409 {
403- "int" or "System.Int32" => $ "SerializeInt(flags, \" { field . FieldName } \" , { field . FieldName } , false, { limitExpression } , { minExpression } )",
404- "int?" or "System.Int32?" => $ "SerializeInt(flags, \" { field . FieldName } \" , { field . FieldName } , true, { limitExpression } , { minExpression } )",
410+ "int" or "int?" => $ "SerializeInt(flags, \" { field . FieldName } \" , { field . FieldName } , { field . Minimum } , { field . Maximum } )",
405411 "bool" or "System.Boolean" => $ "SerializeBool(flags, \" { field . FieldName } \" , { field . FieldName } , false)",
406412 "bool?" or "System.Boolean?" => $ "SerializeBool(flags, \" { field . FieldName } \" , { field . FieldName } , true)",
407413 var type when field . IsEnum => $ "SerializeEnum<{ type } >(flags, \" { field . FieldName } \" , { field . FieldName } )",
@@ -412,16 +418,11 @@ private static string GetSerializeCall(SerializedFieldInfo field)
412418
413419 private static string GetDeserializeCall ( SerializedFieldInfo field )
414420 {
415- var limitExpression = field . Limit != null ? $ "{ field . Limit } " : "null" ;
416- var minExpression = $ "{ field . Minimum ?? 0 } ";
417421 var output = new StringBuilder ( ) ;
418422 var propName = GetPropertyName ( field . FieldName ) ;
419- if ( field . FieldType is "int" or "int?" && field . Limit == null )
420- output . AppendLine ( $ "#error Numeric type { field . FieldName } must have a `Limit` attribute!") ;
421423 output . Append ( field . FieldType switch
422424 {
423- "int" or "System.Int32" => $ "{ propName } = DeserializeInt(flags, \" { field . FieldName } \" , { limitExpression } , { minExpression } )",
424- "int?" or "System.Int32?" => $ "{ propName } = DeserializeNullableInt(flags, \" { field . FieldName } \" , { limitExpression } , { minExpression } )",
425+ "int" or "int?" => $ "{ propName } = DeserializeInt(flags, \" { field . FieldName } \" , { field . Minimum } , { field . Maximum } )",
425426 "bool" or "System.Boolean" => $ "{ propName } = DeserializeBool(flags, \" { field . FieldName } \" )",
426427 "bool?" or "System.Boolean?" => $ "{ propName } = DeserializeNullableBool(flags, \" { field . FieldName } \" )",
427428 var type when field . IsEnum => $ "{ propName } = DeserializeEnum<{ type } >(flags, \" { field . FieldName } \" )",
@@ -582,7 +583,7 @@ public class SerializedFieldInfo
582583 public string ? DefaultValue { get ; set ; }
583584 public bool IsEnum { get ; set ; }
584585 public INamedTypeSymbol ? EnumSymbol { get ; set ; }
585- public int ? Limit { get ; set ; }
586+ public int ? Maximum { get ; set ; }
586587 public int ? Minimum { get ; set ; }
587588 public string ? CustomSerializerName { get ; set ; }
588589}
0 commit comments