realm-dart icon indicating copy to clipboard operation
realm-dart copied to clipboard

Private properties are ignored on Realm model generation

Open dotjon0 opened this issue 1 year ago • 5 comments

What happened?

So we have the below code. When we run dart run realm generate the _type property is not generated in the Realm .g.dart part file.... There is no warning message. Ideally it would be amazing if the Realm team could support private properties - use case below (can think of many more, please ask):

The outcome we are looking to achieve is to make 'enums' (stored in the database as a String) and instance of the enum via getters. So when you access Car.type it outputs a value from CarType enum.

@RealmModel()
class $Car {
  
  /// Car Type
  @MapTo("type")
  late String _type;

  @Ignored()
  CarType get type {
    return CarType.values.byName(_type);
  }
}

enum CarType { fastback, saloon }

Repro steps

See above

Version

^1.0.3

What Atlas Services are you using?

Atlas Device Sync

What type of application is this?

Flutter Application

Client OS and version

macOS 13.4.1

Code snippets

See above

Stacktrace of the exception/crash you're getting

None

Relevant log output

None

dotjon0 avatar Aug 02 '23 12:08 dotjon0

We hear you. For now perhaps rename _type to typeAsString and allow it to be public.

nielsenko avatar Aug 02 '23 12:08 nielsenko

You don't need the @Ignored() on type as calculated props are implicitly ignored.

nielsenko avatar Aug 02 '23 12:08 nielsenko

Thanks @nielsenko. Yes the typeAsString is a good suggestion, however we also need other custom getters to convert properties to different types in far more complex scenarios...so the property naming in these cases could get very unmaintainable and confusing... Is private properties something that you would consider as this would unlock a load of possibilities ?

dotjon0 avatar Aug 02 '23 13:08 dotjon0

We will definitely consider. Just have to think it through, but I don't see any immediate reason as why not to allow them.

Perhaps hidden behind a configuration flag.

nielsenko avatar Aug 02 '23 14:08 nielsenko

Any updates on this please ? We have a huge number of enums (stored as strings in MongoDB Atlas) and the proposed approach above would be much appreciated to streamline the development and strictly typed nature - i.e. we can map 'strings' in Realm data base models to Dart enums.

dotjon0 avatar Nov 07 '23 22:11 dotjon0

It would be awesome to take this a step further and add support for enums directly and avoid the boiler-plate.

Annotations such as

@Enumerated(EnumType.ordinal32)

or

@Enumerated(EnumType.name)

Could be used the specify how to store the enum.

derrickgw avatar Apr 17 '24 03:04 derrickgw

@dotjon0 This is now supported with 2.1.0.

@derrickgw I appreciate your suggestion, but one problem for us is that enums behave very differently across the various SDK languages. There are multiple issues requesting the same:

  • https://github.com/realm/realm-core/issues/6159
  • https://github.com/realm/realm-dart/issues/681
  • https://github.com/realm/realm-dotnet/issues/549
  • https://github.com/realm/realm-java/issues/776
  • https://github.com/realm/realm-js/issues/5260
  • https://github.com/realm/realm-kotlin/issues/829
  • https://github.com/realm/realm-swift/issues/921

It would be fairly trivial to add for the Dart SDK, but we exists in a larger eco-system. I will bring it up internally.

nielsenko avatar Apr 17 '24 17:04 nielsenko

Amazing @nielsenko thank you soooo much! really appreciated! Will transform enums in Flutter Realm.

dotjon0 avatar Apr 17 '24 18:04 dotjon0

@nielsenko Thanks. I recognize that adding enum support at the file level would require a lot of effort and coordination. But in the meantime storing them as string or ints is essentially what we do now. Adding a dart-only annotation to automate that would be great, and might even make it easier to migrate later if/when the file format supports enums.

derrickgw avatar Apr 17 '24 21:04 derrickgw

@derrickgw if you build your Realm models automatically, you can then automatically create the required setters and getters for enums, so likely the result you're after i.e.

@RealmModel()
class $Car {
  
  /// Car Type
  @MapTo("type")
  late String _type;

  @Ignored()
  CarType get type {
    return CarType.values.byName(_type);
  }
}

enum CarType { fastback, saloon }

dotjon0 avatar Apr 17 '24 22:04 dotjon0

@dotjon0 That is essentially what I have now, but the variable is statusInt and the getter/setter is for status because private variables weren't supported yet. I am just saying that it would be awesome to have this boiler plate in the generated code instead of me writing it. It isn't a huge deal, just a nice to have. And it would allow the generated boilerplate to change if/when the database file supports enums natively.

derrickgw avatar Apr 18 '24 16:04 derrickgw

@derrickgw yes in an ideal world for sure. Note there is an open feature request for this https://github.com/realm/realm-dart/issues/681

dotjon0 avatar Apr 18 '24 17:04 dotjon0