Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tool/generation_test/fixtures/json_basic/input.genq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class _$UserCopyWithImpl implements $UserCopyWith {
address: address == genq ? __value.address : address as Address?,
birthday: birthday == genq ? __value.birthday : birthday as DateTime?,
balance: balance == genq ? __value.balance : balance as BigInt?,
someObject: someObject == genq ? __value.someObject : someObject as Object?,
someObject: someObject == genq ? __value.someObject : someObject,
someDynamic: someDynamic == genq ? __value.someDynamic : someDynamic as dynamic,
someDoubleValue: someDoubleValue == genq ? __value.someDoubleValue : someDoubleValue as double,
someDoubleValueNullable: someDoubleValueNullable == genq ? __value.someDoubleValueNullable : someDoubleValueNullable as double?,
Expand Down
2 changes: 2 additions & 0 deletions tool/generation_test/fixtures/json_default_value/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class User with _$User {
factory User({
@JsonKey(name: 'full_name', defaultValue: 'Supername') required String name,
@JsonKey(defaultValue: 999) required int? age,
@JsonKey(defaultValue: 99.9) required double? someDoubleValueNullable,
@JsonKey(name: 'some_double_value', defaultValue: 1234567.89123456789) required double someDoubleValue,
required bool registered,
}) = _User;
}
26 changes: 25 additions & 1 deletion tool/generation_test/fixtures/json_default_value/input.genq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ part of 'input.dart';
mixin _$User {
String get name => throw UnimplementedError();
int? get age => throw UnimplementedError();
double? get someDoubleValueNullable => throw UnimplementedError();
double get someDoubleValue => throw UnimplementedError();
bool get registered => throw UnimplementedError();

$UserCopyWith get copyWith => throw UnimplementedError();
Expand All @@ -15,12 +17,20 @@ class _User implements User {
@override
final int? age;

@override
final double? someDoubleValueNullable;

@override
final double someDoubleValue;

@override
final bool registered;

_User({
required this.name,
required this.age,
required this.someDoubleValueNullable,
required this.someDoubleValue,
required this.registered,
});

Expand All @@ -29,7 +39,7 @@ class _User implements User {

@override
String toString() {
return "User(name: $name, age: $age, registered: $registered)";
return "User(name: $name, age: $age, someDoubleValueNullable: $someDoubleValueNullable, someDoubleValue: $someDoubleValue, registered: $registered)";
}

@override
Expand All @@ -38,6 +48,8 @@ class _User implements User {
if (other is! User) return false;
if (!identical(other.name, name) && other.name != name) return false;
if (!identical(other.age, age) && other.age != age) return false;
if (!identical(other.someDoubleValueNullable, someDoubleValueNullable) && other.someDoubleValueNullable != someDoubleValueNullable) return false;
if (!identical(other.someDoubleValue, someDoubleValue) && other.someDoubleValue != someDoubleValue) return false;
if (!identical(other.registered, registered) && other.registered != registered) return false;
return true;
}
Expand All @@ -48,6 +60,8 @@ class _User implements User {
runtimeType,
name,
age,
someDoubleValueNullable,
someDoubleValue,
registered,
);
}
Expand All @@ -57,6 +71,8 @@ abstract class $UserCopyWith {
User call({
String name,
int? age,
double? someDoubleValueNullable,
double someDoubleValue,
bool registered,
});
}
Expand All @@ -70,11 +86,15 @@ class _$UserCopyWithImpl implements $UserCopyWith {
User call({
Object? name = genq,
Object? age = genq,
Object? someDoubleValueNullable = genq,
Object? someDoubleValue = genq,
Object? registered = genq,
}) {
return User(
name: name == genq ? __value.name : name as String,
age: age == genq ? __value.age : age as int?,
someDoubleValueNullable: someDoubleValueNullable == genq ? __value.someDoubleValueNullable : someDoubleValueNullable as double?,
someDoubleValue: someDoubleValue == genq ? __value.someDoubleValue : someDoubleValue as double,
registered: registered == genq ? __value.registered : registered as bool,
);
}
Expand All @@ -84,6 +104,8 @@ User $UserFromJson(Map<String, dynamic> json) {
return User(
name: json['full_name'] == null ? 'Supername' : json['full_name'] as String,
age: json['age'] == null ? 999 : (json['age'] as num).toInt(),
someDoubleValueNullable: json['someDoubleValueNullable'] == null ? 99.9 : (json['someDoubleValueNullable'] as num).toDouble(),
someDoubleValue: json['some_double_value'] == null ? 1234567.89123456789 : (json['some_double_value'] as num).toDouble(),
registered: json['registered'] as bool,
);
}
Expand All @@ -92,6 +114,8 @@ Map<String, dynamic> $UserToJson(User obj) {
return {
'full_name': obj.name,
'age': obj.age == null ? null : obj.age!,
'someDoubleValueNullable': obj.someDoubleValueNullable == null ? null : obj.someDoubleValueNullable!,
'some_double_value': obj.someDoubleValue,
'registered': obj.registered,
};
}
1 change: 1 addition & 0 deletions tool/generation_test/fixtures/json_enum/input.genq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Object $UserStatusToJson(UserStatus value) {
return 0;
case UserStatus.blocked:
return 1;
// ignore: unreachable_switch_default
default:
throw UnsupportedError('Could not map $value to a JSON value');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Object $AccountTypeToJson(AccountType value) {
return "vip";
case AccountType.unknown:
return "unknown";
// ignore: unreachable_switch_default
default:
throw UnsupportedError('Could not map $value to a JSON value');
}
Expand Down
24 changes: 22 additions & 2 deletions tool/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ func (p *Parser) parseArgumentList() (GenqArgumentList, *ParsingError) {
params := []GenqNamedExpression{}
if p.lookahead == TOKEN_PAREN_START {
// Annotation is an invocation
p.eat(TOKEN_PAREN_START)
_, err := p.eat(TOKEN_PAREN_START)
if err != nil {
return GenqArgumentList{}, err
}

for p.lookahead != TOKEN_PAREN_END {
p.markRestorationPoint()
v, err := p.parseNamedAssignment()

if err == nil {
p.dontRestore()

Expand All @@ -165,7 +169,7 @@ func (p *Parser) parseArgumentList() (GenqArgumentList, *ParsingError) {
}
}

_, err := p.eat(TOKEN_PAREN_END)
_, err = p.eat(TOKEN_PAREN_END)
if err != nil {
return GenqArgumentList{}, err
}
Expand All @@ -192,6 +196,10 @@ func (p *Parser) parseAnnotation() (GenqAnnotation, *ParsingError) {
if p.lookahead == TOKEN_PAREN_START {
// Annotation is an invocation
argumentList, err = p.parseArgumentList()

if err != nil {
return GenqAnnotation{}, err
}
}

return GenqAnnotation{
Expand Down Expand Up @@ -256,6 +264,18 @@ func (p *Parser) parsePrimitive() (GenqValue, *ParsingError) {
}, nil
}

if p.lookahead == TOKEN_DECIMAL_NUMBER {
v, err := p.eat(TOKEN_DECIMAL_NUMBER)
if err != nil {
return GenqValue{}, err
}

return GenqValue{
RawValue: v,
}, nil

}

if p.lookahead == TOKEN_SINGLE_STRING || p.lookahead == TOKEN_DOUBLE_STRING {
v, err := p.eat(p.lookahead)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion tool/templates/copywith.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func templateCopyWith(str []string, classDecl GenqClassDeclaration) []string {

str = append(str, indent(4, fmt.Sprintf("return %s(", classDecl.Name)))
for _, param := range classDecl.Constructor.ParamList.NamedParams {
str = append(str, indent(6, fmt.Sprintf("%s: %s == genq ? __value.%s : %s as %s,", param.Name, param.Name, param.Name, param.Name, param.ParamType.String())))
if param.ParamType.String() == "Object?" {
str = append(str, indent(6, fmt.Sprintf("%s: %s == genq ? __value.%s : %s,", param.Name, param.Name, param.Name, param.Name)))
} else {
str = append(str, indent(6, fmt.Sprintf("%s: %s == genq ? __value.%s : %s as %s,", param.Name, param.Name, param.Name, param.Name, param.ParamType.String())))
}
}
str = append(str, indent(4, fmt.Sprintf(");")))

Expand Down
1 change: 1 addition & 0 deletions tool/templates/json_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TemplateJsonEnumToJson(str []string, classDecl GenqJsonEnum) []string {
}
str = append(str, indent(6, fmt.Sprintf("return %s;", jsonValue)))
}
str = append(str, indent(4, "// ignore: unreachable_switch_default"))
str = append(str, indent(4, fmt.Sprintf("default:")))
str = append(str, indent(6, fmt.Sprintf("throw UnsupportedError('Could not map $value to a JSON value');")))
str = append(str, indent(2, fmt.Sprintf("}")))
Expand Down
Loading