Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MappableField with key as raw String #207

Open
outrelouxhe opened this issue Jun 28, 2024 · 2 comments
Open

MappableField with key as raw String #207

outrelouxhe opened this issue Jun 28, 2024 · 2 comments

Comments

@outrelouxhe
Copy link

outrelouxhe commented Jun 28, 2024

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');
@outrelouxhe
Copy link
Author

outrelouxhe commented Jun 29, 2024

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;
  }
}

@schultek
Copy link
Owner

Oh yes seems like a bug, will take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants