Skip to content

Commit

Permalink
Merge pull request #252 from MBulli/main
Browse files Browse the repository at this point in the history
Fix for issue #207
  • Loading branch information
schultek authored Jan 17, 2025
2 parents 5bda858 + 7cc20ef commit ed7e32a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ClassMapperFieldElement extends MapperFieldElement {
_keyFor(field?.getter) ??
parent.caseStyle.transform(name);
if (key != name) {
return ", key: '$key'";
return ", key: r'$key'";
} else {
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RecordMapperFieldElement extends MapperFieldElement {
String key = param.key ?? parent.caseStyle?.transform(param.name) ?? name;

if (key != name) {
return ", key: '$key'";
return ", key: r'$key'";
} else {
return '';
}
Expand Down
39 changes: 39 additions & 0 deletions packages/dart_mappable_builder/test/simple_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,44 @@ void main() {
},
);
});

// Test case for https://github.com/schultek/dart_mappable/issues/207
test('raw string as key for field', () async {
await testMappable(
{
'model': '''
import 'package:dart_mappable/dart_mappable.dart';
part 'model.mapper.dart';
@MappableRecord()
typedef FullName = (@MappableField(key: r'\$firstName') String, String);
@MappableClass()
class Rawkeymodel with RawkeymodelMappable {
final String a;
final FullName? b;
Rawkeymodel({
@MappableField(key: r'\$key') required this.a,
this.b,
});
}
''',
},
expect: {
"Rawkeymodel(a: 'hello', b: ('Max', 'Mustermann')).toMap()": equals({
r'$key': 'hello',
'b': {r'$firstName': 'Max', r'$2': 'Mustermann'}
}),
"RawkeymodelMapper.fromMap({r'\$key': 'hi', 'b': ('Max', 'Mustermann')}).toMap()":
equals({
r'$key': 'hi',
'b': {r'$firstName': 'Max', r'$2': 'Mustermann'}
}),
},
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class TempAssetWriter implements RecordingAssetWriter {

TempAssetWriter() {
var id = Random().nextInt(1e6.round());
dir = Directory.fromUri(
Uri.parse(path.join(Directory.systemTemp.path, 'mappable_$id')));
dir.createSync();
dir = Directory.systemTemp.createTempSync('mappable_$id');
}

Future<dynamic> run(String code) async {
Expand Down

0 comments on commit ed7e32a

Please sign in to comment.