google / google/built_value.dart

built_collection fields not suggested instead of SDK fields when generic type parameter is used

Open
#675 2 comments 1 reaction 1 assignee Claimed by @davidmorgan View on GitHub
p2 / bug
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

As result of #254, the `built_value` code generator reports an helpful error when defining a class with Dart's SDK types; `List`, `Set` and `Map` etc and suggests to use the equivalent immutable types `BuiltList` etc.

However, if the properties use those same types, but include a generic type parameter, then the error is not reported.

For example, this class definition:

```dart
abstract class Model implements Built {
List get sdkList;
Set get sdkSet;
Map get sdkMap;

Model._();
factory Model([updates(ModelBuilder b)]) = _$Model;
}
```
Successfully generates the following implementation, which includes the unsafe SDK `List`, `Set` and `Map` (builder code omitted).

```dart
class _$Model extends Model {
@override
final List sdkList;
@override
final Set sdkSet;
@override
final Map sdkMap;

factory _$Model([void Function(ModelBuilder) updates]) =>
(new ModelBuilder()..update(updates)).build();

_$Model._({this.sdkList, this.sdkSet, this.sdkMap}) : super._() {
if (sdkList == null) {
throw new BuiltValueNullFieldError('Model', 'sdkList');
}
if (sdkSet == null) {
throw new BuiltValueNullFieldError('Model', 'sdkSet');
}
if (sdkMap == null) {
throw new BuiltValueNullFieldError('Model', 'sdkMap');
}
}

@override
Model rebuild(void Function(ModelBuilder) updates) =>
(toBuilder()..update(updates)).build();

@override
ModelBuilder toBuilder() => new ModelBuilder()..replace(this);

@override
bool operator ==(Object other) {
if (identical(other, this)) return true;
return other is Model &&
sdkList == other.sdkList &&
sdkSet == other.sdkSet &&
sdkMap == other.sdkMap;
}

@override
int get hashCode {
return $jf(
$jc($jc($jc(0, sdkList.hashCode), sdkSet.hashCode), sdkMap.hashCode));
}

@override
String toString() {
return (newBuiltValueToStringHelper('Model')
..add('sdkList', sdkList)
..add('sdkSet', sdkSet)
..add('sdkMap', sdkMap))
.toString();
}
}
```
Is this behaviour by design or an oversight in the matching of the type name with those defined in `value_source_field.dart`?

```dart
const _suggestedTypes = {
'List': 'BuiltList',
'Map': 'BuiltMap',
'Set': 'BuiltSet',
'ListMultimap': 'BuiltListMultimap',
'SetMultimap': 'BuiltSetMultimap',
};
```
In the example provided, the resolved `type`String is `List` etc and so doesn't match with the `List` key in the `_suggestedTypes`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.