Fix incorrect work with class Expression

This commit is contained in:
man-y
2024-01-24 18:26:16 +03:00
parent 1c76a80aa8
commit 6d6958f422
21 changed files with 53 additions and 52 deletions
@@ -175,44 +175,45 @@ class DartProperty(Property):
def get_parse_strategy(self) -> str:
prop_type = cast(DartPropertyType, self.property_type)
prop_type_decl = prop_type.declaration()
required_cast = '' if self.optional or self.has_default else '!'
required_cast = '' if self.optional and not self.has_default else '!'
fallback = f' fallback: {self.fallback_declaration},' if self.has_default else ''
expr = 'Expr' if self.supports_expressions else ''
fallback = f', fallback: {self.fallback_declaration}' if self.supports_expressions and self.has_default else ''
if isinstance(prop_type, Int):
return f"safeParseInt{expr}(json['{self.name}']{fallback}){required_cast}"
return f"safeParseInt{expr}(json['{self.name}'],{fallback}){required_cast}"
elif isinstance(prop_type, Color):
return f"safeParseColor{expr}(json['{self.name}']{fallback}){required_cast}"
return f"safeParseColor{expr}(json['{self.name}'],{fallback}){required_cast}"
elif isinstance(prop_type, Double):
return f"safeParseDouble{expr}(json['{self.name}']{fallback}){required_cast}"
return f"safeParseDouble{expr}(json['{self.name}'],{fallback}){required_cast}"
elif isinstance(prop_type, (Bool, BoolInt)):
return f"safeParseBool{expr}(json['{self.name}']{fallback}){required_cast}"
return f"safeParseBool{expr}(json['{self.name}'],{fallback}){required_cast}"
elif isinstance(prop_type, (String, StaticString)):
return f"safeParseStr{expr}(json['{self.name}']?.toString(){fallback}){required_cast}"
return f"safeParseStr{expr}(json['{self.name}']?.toString(),{fallback}){required_cast}"
elif isinstance(prop_type, Dictionary):
return f"safeParseClass{expr}(json{fallback}){required_cast}"
return f"safeParseObj{expr}(json,{fallback}){required_cast}"
elif isinstance(prop_type, RawArray):
return f"safeParseClass{expr}(json['{self.name}']{fallback}){required_cast}"
return f"safeParseObj{expr}(json['{self.name}'],{fallback}){required_cast}"
elif isinstance(prop_type, Url):
return f"safeParseUri{expr}(json['{self.name}']){required_cast}"
elif prop_type.is_string_enumeration():
return f"safeParseStrEnum{expr}(json['{self.name}'], parse: {prop_type_decl}.fromJson,{fallback}){required_cast}"
elif prop_type.is_class():
return f"safeParseClass{expr}({prop_type_decl}.fromJson(json['{self.name}']){fallback}){required_cast}"
return f"safeParseObj{expr}({prop_type_decl}.fromJson(json['{self.name}']),{fallback}){required_cast}"
elif prop_type.is_list():
inner_item_type = prop_type.get_list_inner_class()
if inner_item_type.is_class():
if inner_item_type.is_string_enumeration():
return f"safeParseClass{expr}((json['{self.name}'] as List<dynamic>{'?' if self.optional or self.has_default else ''})" \
return f"safeParseObj{expr}((json['{self.name}'] as List<dynamic>{'?' if self.optional or self.has_default else ''})" \
f"{'?' if self.optional or self.has_default else ''}.map((s) => {prop_type_decl[5:-1]}" \
f".fromJson(s)!).toList(){fallback}){'' if self.optional or self.has_default else '!'}"
f".fromJson(s)!).toList(),{fallback}){'' if self.optional or self.has_default else '!'}"
else:
return f"safeParseClass{expr}((json['{self.name}'] as List<dynamic>{'?' if self.optional or self.has_default else ''})" \
return f"safeParseObj{expr}((json['{self.name}'] as List<dynamic>{'?' if self.optional or self.has_default else ''})" \
f"{'?' if self.optional or self.has_default else ''}.map((j) => {prop_type_decl[5:-1]}." \
f"fromJson(j as Map <String, dynamic>)!{fallback}).toList()){'' if self.optional or self.has_default else '!'}"
f"fromJson(j as Map <String, dynamic>)!,{fallback}).toList()){'' if self.optional or self.has_default else '!'}"
else:
return f"safeParseClass{expr}((json['{self.name}'] as List<dynamic>{'?' if self.optional or self.has_default else ''})" \
return f"safeParseObj{expr}((json['{self.name}'] as List<dynamic>{'?' if self.optional or self.has_default else ''})" \
f"{'?' if self.optional or self.has_default else ''}.map((v) => (v as {prop_type_decl[5:-1]}))" \
f".toList(){fallback}){'' if self.optional or self.has_default else '!'}"
f".toList(),{fallback}){'' if self.optional or self.has_default else '!'}"
@property
def fallback_declaration(self) -> str:
@@ -128,7 +128,7 @@ class DartGenerator(Generator):
result += f' return {full_name}('
for prop in entity.instance_properties:
decode_strategy = prop.get_parse_strategy()
result += f" {utils.lower_camel_case(prop.name)}: {prop.add_default_value_to(decode_strategy, in_constructor=False)},"
result += f" {utils.lower_camel_case(prop.name)}: {decode_strategy},"
result += ' );'
result += ' }'
else:
@@ -24,7 +24,7 @@ class EntityWithArray with EquatableMixin {
return null;
}
return EntityWithArray(
array: safeParseClass((json['array'] as List<dynamic>).map((j) => Entity.fromJson(j as Map <String, dynamic>)!).toList())!,
array: safeParseObj((json['array'] as List<dynamic>).map((j) => Entity.fromJson(j as Map <String, dynamic>)!,).toList())!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithArrayOfEnums with EquatableMixin {
return null;
}
return EntityWithArrayOfEnums(
items: safeParseClass((json['items'] as List<dynamic>).map((s) => EntityWithArrayOfEnumsItem.fromJson(s)!).toList())!,
items: safeParseObj((json['items'] as List<dynamic>).map((s) => EntityWithArrayOfEnumsItem.fromJson(s)!).toList(),)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithArrayOfExpressions with EquatableMixin {
return null;
}
return EntityWithArrayOfExpressions(
items: safeParseClassExpr((json['items'] as List<dynamic>).map((v) => (v as String)).toList())!,
items: safeParseObjExpr((json['items'] as List<dynamic>).map((v) => (v as String)).toList(),)!,
);
}
}
@@ -24,7 +24,7 @@ class EntityWithArrayOfNestedItems with EquatableMixin {
return null;
}
return EntityWithArrayOfNestedItems(
items: safeParseClass((json['items'] as List<dynamic>).map((j) => EntityWithArrayOfNestedItemsItem.fromJson(j as Map <String, dynamic>)!).toList())!,
items: safeParseObj((json['items'] as List<dynamic>).map((j) => EntityWithArrayOfNestedItemsItem.fromJson(j as Map <String, dynamic>)!,).toList())!,
);
}
}
@@ -51,8 +51,8 @@ class EntityWithArrayOfNestedItemsItem with EquatableMixin {
return null;
}
return EntityWithArrayOfNestedItemsItem(
entity: safeParseClass(Entity.fromJson(json['entity']))!,
property: safeParseStrExpr(json['property']?.toString())!,
entity: safeParseObj(Entity.fromJson(json['entity']),)!,
property: safeParseStrExpr(json['property']?.toString(),)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithArrayWithTransform with EquatableMixin {
return null;
}
return EntityWithArrayWithTransform(
array: safeParseClassExpr((json['array'] as List<dynamic>).map((v) => (v as int)).toList())!,
array: safeParseObjExpr((json['array'] as List<dynamic>).map((v) => (v as int)).toList(),)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithComplexProperty with EquatableMixin {
return null;
}
return EntityWithComplexProperty(
property: safeParseClass(EntityWithComplexPropertyProperty.fromJson(json['property']))!,
property: safeParseObj(EntityWithComplexPropertyProperty.fromJson(json['property']),)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithComplexPropertyWithDefaultValue with EquatableMixin {
return null;
}
return EntityWithComplexPropertyWithDefaultValue(
property: safeParseClass(EntityWithComplexPropertyWithDefaultValueProperty.fromJson(json['property'])) ?? const EntityWithComplexPropertyWithDefaultValueProperty(value: Expression.value("Default text"),),
property: safeParseObj(EntityWithComplexPropertyWithDefaultValueProperty.fromJson(json['property']), fallback: const EntityWithComplexPropertyWithDefaultValueProperty(value: Expression.value("Default text"),),)!,
);
}
}
@@ -46,7 +46,7 @@ class EntityWithComplexPropertyWithDefaultValueProperty with EquatableMixin {
return null;
}
return EntityWithComplexPropertyWithDefaultValueProperty(
value: safeParseStrExpr(json['value']?.toString())!,
value: safeParseStrExpr(json['value']?.toString(),)!,
);
}
}
@@ -25,7 +25,7 @@ class EntityWithEntityProperty with EquatableMixin {
return null;
}
return EntityWithEntityProperty(
entity: safeParseClass(Entity.fromJson(json['entity'])) ?? const Entity(const EntityWithStringEnumProperty(property: Expression.value(EntityWithStringEnumPropertyProperty.second),)),
entity: safeParseObj(Entity.fromJson(json['entity']), fallback: const Entity(const EntityWithStringEnumProperty(property: Expression.value(EntityWithStringEnumPropertyProperty.second),)),)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithJsonProperty with EquatableMixin {
return null;
}
return EntityWithJsonProperty(
jsonProperty: safeParseClass(json) ?? None,
jsonProperty: safeParseObj(json, fallback: None,)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithOptionalComplexProperty with EquatableMixin {
return null;
}
return EntityWithOptionalComplexProperty(
property: safeParseClass(EntityWithOptionalComplexPropertyProperty.fromJson(json['property'])),
property: safeParseObj(EntityWithOptionalComplexPropertyProperty.fromJson(json['property']),),
);
}
}
@@ -23,7 +23,7 @@ class EntityWithOptionalProperty with EquatableMixin {
return null;
}
return EntityWithOptionalProperty(
property: safeParseStrExpr(json['property']?.toString()),
property: safeParseStrExpr(json['property']?.toString(),),
);
}
}
@@ -23,7 +23,7 @@ class EntityWithOptionalStringEnumProperty with EquatableMixin {
return null;
}
return EntityWithOptionalStringEnumProperty(
property: safeParseClassExpr(EntityWithOptionalStringEnumPropertyProperty.fromJson(json['property'])),
property: safeParseStrEnumExpr(json['property'], parse: EntityWithOptionalStringEnumPropertyProperty.fromJson,),
);
}
}
@@ -31,9 +31,9 @@ class EntityWithPropertyWithDefaultValue with EquatableMixin {
return null;
}
return EntityWithPropertyWithDefaultValue(
iNum: safeParseIntExpr(json['iNum'], fallback: 0) ?? const Expression.value(0),
nested: safeParseClass(EntityWithPropertyWithDefaultValueNested.fromJson(json['nested'])),
url: safeParseUriExpr(json['url']) ?? const Expression.value(const Uri.parse("https://yandex.ru")),
iNum: safeParseIntExpr(json['iNum'], fallback: 0,)!,
nested: safeParseObj(EntityWithPropertyWithDefaultValueNested.fromJson(json['nested']),),
url: safeParseUriExpr(json['url'])!,
);
}
}
@@ -64,9 +64,9 @@ class EntityWithPropertyWithDefaultValueNested with EquatableMixin {
return null;
}
return EntityWithPropertyWithDefaultValueNested(
iNum: safeParseIntExpr(json['iNum'], fallback: 0) ?? const Expression.value(0),
nonOptional: safeParseStrExpr(json['non_optional']?.toString())!,
url: safeParseUriExpr(json['url']) ?? const Expression.value(const Uri.parse("https://yandex.ru")),
iNum: safeParseIntExpr(json['iNum'], fallback: 0,)!,
nonOptional: safeParseStrExpr(json['non_optional']?.toString(),)!,
url: safeParseUriExpr(json['url'])!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithRawArray with EquatableMixin {
return null;
}
return EntityWithRawArray(
array: safeParseClassExpr(json['array'])!,
array: safeParseObjExpr(json['array'],)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithRequiredProperty with EquatableMixin {
return null;
}
return EntityWithRequiredProperty(
property: safeParseStrExpr(json['property']?.toString())!,
property: safeParseStrExpr(json['property']?.toString(),)!,
);
}
}
@@ -55,14 +55,14 @@ class EntityWithSimpleProperties with EquatableMixin {
return null;
}
return EntityWithSimpleProperties(
boolean: safeParseBoolExpr(json['boolean']),
booleanInt: safeParseBoolExpr(json['boolean_int']),
color: safeParseColorExpr(json['color']),
dNum: safeParseDoubleExpr(json['dNum']),
id: safeParseInt(json['id']) ?? 0,
integer: safeParseIntExpr(json['integer'], fallback: 0) ?? const Expression.value(0),
positiveInteger: safeParseIntExpr(json['positive_integer']),
string: safeParseStrExpr(json['string']?.toString()),
boolean: safeParseBoolExpr(json['boolean'],),
booleanInt: safeParseBoolExpr(json['boolean_int'],),
color: safeParseColorExpr(json['color'],),
dNum: safeParseDoubleExpr(json['dNum'],),
id: safeParseInt(json['id'], fallback: 0,)!,
integer: safeParseIntExpr(json['integer'], fallback: 0,)!,
positiveInteger: safeParseIntExpr(json['positive_integer'],),
string: safeParseStrExpr(json['string']?.toString(),),
url: safeParseUriExpr(json['url']),
);
}
@@ -23,7 +23,7 @@ class EntityWithStringArrayProperty with EquatableMixin {
return null;
}
return EntityWithStringArrayProperty(
array: safeParseClassExpr((json['array'] as List<dynamic>).map((v) => (v as String)).toList())!,
array: safeParseObjExpr((json['array'] as List<dynamic>).map((v) => (v as String)).toList(),)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithStringEnumProperty with EquatableMixin {
return null;
}
return EntityWithStringEnumProperty(
property: safeParseClassExpr(EntityWithStringEnumPropertyProperty.fromJson(json['property']))!,
property: safeParseStrEnumExpr(json['property'], parse: EntityWithStringEnumPropertyProperty.fromJson,)!,
);
}
}
@@ -23,7 +23,7 @@ class EntityWithStringEnumPropertyWithDefaultValue with EquatableMixin {
return null;
}
return EntityWithStringEnumPropertyWithDefaultValue(
value: safeParseClassExpr(EntityWithStringEnumPropertyWithDefaultValueValue.fromJson(json['value']), fallback: EntityWithStringEnumPropertyWithDefaultValueValue.second) ?? const Expression.value(EntityWithStringEnumPropertyWithDefaultValueValue.second),
value: safeParseStrEnumExpr(json['value'], parse: EntityWithStringEnumPropertyWithDefaultValueValue.fromJson, fallback: EntityWithStringEnumPropertyWithDefaultValueValue.second,)!,
);
}
}