Repository: schultek/dart_mappable

Issue:
MappableField with key as raw String
I need to use a raw string for one key because one of the API I'm using (appwrite) is storing the id of a record in the '$id' field.
However, the generated file does not take into account the 'r' before the string and it creates an error and I need to update manually the generated file. I have also tried to escape the $ by adding a \ in front of it but id doesn't not work.

Am I missing something or is there a workaround ?
Thank you

```
@MappableClass()
class TranslatorModel extends TranslatorEntity with TranslatorModelMappable {
  /// Constructs a [TranslatorModel] instance.
  TranslatorModel({
    @MappableField(key: r'$id') required super.id,
    required super.surname,
    required super.firstname,
    required super.languages,
  });
}
```

From TranslatorModelMapper : 

```
static const Field<TranslatorModel, String> _f$id =
      Field('id', _$id, key: '$id');
```



Discussion: For information, I have implemented the following workaround using a hook on the MappableClass: 

```
class TranslatorMappingHook extends MappingHook {
  const TranslatorMappingHook();
  @override
  Object? beforeDecode(Object? value) {
    if (value is Map<String, dynamic>) {
      // Set 'id' key with r'$id' value
      if (value.containsKey(r'$id')) {
        value['id'] = value[r'$id'];
      }
    }
    return value;
  }
}

```
Oh yes seems like a bug, will take a look


Fix patch:
diff --git a/packages/dart_mappable_builder/lib/src/elements/field/class_mapper_field_element.dart b/packages/dart_mappable_builder/lib/src/elements/field/class_mapper_field_element.dart
index d43f762a..66442ed8 100644
--- a/packages/dart_mappable_builder/lib/src/elements/field/class_mapper_field_element.dart
+++ b/packages/dart_mappable_builder/lib/src/elements/field/class_mapper_field_element.dart
@@ -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 '';
     }
diff --git a/packages/dart_mappable_builder/lib/src/elements/field/record_mapper_field_element.dart b/packages/dart_mappable_builder/lib/src/elements/field/record_mapper_field_element.dart
index 71ae834e..f7bb6f86 100644
--- a/packages/dart_mappable_builder/lib/src/elements/field/record_mapper_field_element.dart
+++ b/packages/dart_mappable_builder/lib/src/elements/field/record_mapper_field_element.dart
@@ -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 '';
     }


Test patch:
diff --git a/packages/dart_mappable_builder/test/simple_model_test.dart b/packages/dart_mappable_builder/test/simple_model_test.dart
index c74d45ff..84fafab3 100644
--- a/packages/dart_mappable_builder/test/simple_model_test.dart
+++ b/packages/dart_mappable_builder/test/simple_model_test.dart
@@ -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'}
+          }),
+        },
+      );
+    });
   });
 }
diff --git a/packages/dart_mappable_builder/test/utils/temp_asset_writer.dart b/packages/dart_mappable_builder/test/utils/temp_asset_writer.dart
index 4e0a02a5..e1e8c546 100644
--- a/packages/dart_mappable_builder/test/utils/temp_asset_writer.dart
+++ b/packages/dart_mappable_builder/test/utils/temp_asset_writer.dart
@@ -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 {