Skip to content

Commit

Permalink
Improve decoding of basic types (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Jul 18, 2024
1 parent ecb3dd5 commit 7c1b92a
Show file tree
Hide file tree
Showing 316 changed files with 43,079 additions and 109,301 deletions.
24 changes: 8 additions & 16 deletions _test/expected_nonidentical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -962,9 +962,7 @@ class NestedResponse {

NestedResponse.fromJson(core.Map json_)
: this(
nestedResult: json_.containsKey('nestedResult')
? json_['nestedResult'] as core.String
: null,
nestedResult: json_['nestedResult'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -981,7 +979,7 @@ class ToyAgeRequest {

ToyAgeRequest.fromJson(core.Map json_)
: this(
age: json_.containsKey('age') ? json_['age'] as core.int : null,
age: json_['age'] as core.int?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand Down Expand Up @@ -1051,10 +1049,8 @@ class ToyMapResponse {
),
)
: null,
result: json_.containsKey('result')
? json_['result'] as core.String
: null,
v: json_.containsKey('v') ? json_['v'] : null,
result: json_['result'] as core.String?,
v: json_['v'],
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -1078,8 +1074,8 @@ class ToyRequest {

ToyRequest.fromJson(core.Map json_)
: this(
age: json_.containsKey('age') ? json_['age'] as core.int : null,
name: json_.containsKey('name') ? json_['name'] as core.String : null,
age: json_['age'] as core.int?,
name: json_['name'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -1097,9 +1093,7 @@ class ToyResourceResponse {

ToyResourceResponse.fromJson(core.Map json_)
: this(
result: json_.containsKey('result')
? json_['result'] as core.String
: null,
result: json_['result'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -1116,9 +1110,7 @@ class ToyResponse {

ToyResponse.fromJson(core.Map json_)
: this(
result: json_.containsKey('result')
? json_['result'] as core.String
: null,
result: json_['result'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand Down
8 changes: 3 additions & 5 deletions _test/wrapapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class WrapRequest {

WrapRequest.fromJson(core.Map json_)
: this(
age: json_.containsKey('age') ? json_['age'] as core.int : null,
name: json_.containsKey('name') ? json_['name'] as core.String : null,
age: json_['age'] as core.int?,
name: json_['name'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -108,9 +108,7 @@ class WrapResponse {

WrapResponse.fromJson(core.Map json_)
: this(
result: json_.containsKey('result')
? json_['result'] as core.String
: null,
result: json_['result'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand Down
4 changes: 1 addition & 3 deletions _test_package/lib/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class $Response {

$Response.fromJson(core.Map json_)
: this(
result: json_.containsKey('result')
? json_['result'] as core.String
: null,
result: json_['result'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand Down
16 changes: 6 additions & 10 deletions _test_package/lib/toyapi/0_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -959,9 +959,7 @@ class NestedResponse {

NestedResponse.fromJson(core.Map json_)
: this(
nestedResult: json_.containsKey('nestedResult')
? json_['nestedResult'] as core.String
: null,
nestedResult: json_['nestedResult'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -978,7 +976,7 @@ class ToyAgeRequest {

ToyAgeRequest.fromJson(core.Map json_)
: this(
age: json_.containsKey('age') ? json_['age'] as core.int : null,
age: json_['age'] as core.int?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand Down Expand Up @@ -1048,10 +1046,8 @@ class ToyMapResponse {
),
)
: null,
result: json_.containsKey('result')
? json_['result'] as core.String
: null,
v: json_.containsKey('v') ? json_['v'] : null,
result: json_['result'] as core.String?,
v: json_['v'],
);

core.Map<core.String, core.dynamic> toJson() => {
Expand All @@ -1075,8 +1071,8 @@ class ToyRequest {

ToyRequest.fromJson(core.Map json_)
: this(
age: json_.containsKey('age') ? json_['age'] as core.int : null,
name: json_.containsKey('name') ? json_['name'] as core.String : null,
age: json_['age'] as core.int?,
name: json_['name'] as core.String?,
);

core.Map<core.String, core.dynamic> toJson() => {
Expand Down
1 change: 1 addition & 0 deletions discoveryapis_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Package version must be provided or default is to set `publish_to: none`.
- Added `Function` to the set of reserved names.
- Remove extra parenthesis from date encoding.
- Generate much smaller code for trivial types: `String`, `bool`, `int`, `double`.

## 1.0.0

Expand Down
26 changes: 25 additions & 1 deletion discoveryapis_generator/lib/src/dart_schema_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ class BooleanType extends PrimitiveDartSchemaType {

@override
String get declaration => '${imports.core.ref()}bool';

@override
String decodeFromMap(String jsonName) =>
"json_['${escapeString(jsonName)}'] as $declaration?";
}

class IntegerType extends PrimitiveDartSchemaType {
Expand All @@ -367,6 +371,10 @@ class IntegerType extends PrimitiveDartSchemaType {

@override
String get declaration => '${imports.core.ref()}int';

@override
String decodeFromMap(String jsonName) =>
"json_['${escapeString(jsonName)}'] as $declaration?";
}

class StringIntegerType extends PrimitiveDartSchemaType {
Expand Down Expand Up @@ -398,6 +406,11 @@ class DoubleType extends PrimitiveDartSchemaType {
@override
String jsonDecode(String json, {String? importName}) =>
'($json as ${imports.core.ref()}num).toDouble()';

@override
String decodeFromMap(String jsonName) =>
"(json_['${escapeString(jsonName)}'] as ${imports.core.ref()}num?)"
'?.toDouble()';
}

class StringType extends PrimitiveDartSchemaType {
Expand All @@ -411,6 +424,14 @@ class StringType extends PrimitiveDartSchemaType {

@override
String get declaration => '${imports.core.ref()}String';

@override
String decodeFromMap(String jsonName) {
if (runtimeType == StringType) {
return "json_['${escapeString(jsonName)}'] as $declaration?";
}
return super.decodeFromMap(jsonName);
}
}

/// Here to support the fix for https://github.com/google/googleapis.dart/issues/211
Expand Down Expand Up @@ -456,7 +477,7 @@ class EnumType extends StringType {
"'NULL_VALUE' : null";
}

return super.decodeFromMap(jsonName);
return "json_['${escapeString(jsonName)}'] as $declaration?";
}
}

Expand Down Expand Up @@ -514,6 +535,9 @@ class AnyType extends PrimitiveDartSchemaType {

@override
String jsonDecode(String json, {String? importName}) => json;

@override
String decodeFromMap(String jsonName) => "json_['${escapeString(jsonName)}']";
}

/// Represents an unnamed List<T> type with a given `T`.
Expand Down
Loading

0 comments on commit 7c1b92a

Please sign in to comment.