Coder Social home page Coder Social logo

Support final attributes about hive HOT 6 CLOSED

isar avatar isar commented on August 23, 2024
Support final attributes

from hive.

Comments (6)

simc avatar simc commented on August 23, 2024

Yes it is possible but as you said, Hive would need to understand the constructors.

I don't think this is a very hard thing to implement but the generator may need some changes. This is definitely a planned feature but I'm currently working on other features and bugfixes.

Another important thing that is missing are unit tests for the generator. I'm not sure how to do that ^^

I would appreciate your help if you want to to implement the constructor feature ;)

from hive.

MarcelGarus avatar MarcelGarus commented on August 23, 2024

Hmm, the more I think about this, the more complex this feature seems.

Like, theoretically, the classes can be complex, mixing final and non-final fields and altering them in the constructor.

For example, consider the following class:

@HiveType()
class Label {
  @HiveField(0)
  final String name;

  @HiveField(1)
  String author;

  Label();

  Label.withLowercaseName(String name) : this.name = name.toLowercase();

  Label.withName(this.name);
}

Theoretically, hive could be clever enough to come up with would be something of the likes:

return SampleClass.withName(name)
    ..author = author;

But that seems complex, especially considering that attributes can be named, positioned or required.
There's just a lot of variation going on in constructors.
So I'm afraid the code generator would become very complex.


Another option would be to limit the constructors that are accepted, maybe even requiring a fromHive constructor for classes that contain final fields, something like this:

@HiveType()
class Label {
  ...

  Label.fromHive({
    this.name,
    this.author
  });
}

Any ideas on how to proceed?
What do you think about requiring a special constructor for classes with final fields that still want to be stored in hive?

from hive.

MarcelGarus avatar MarcelGarus commented on August 23, 2024

Quick sidenote: For my use case I just created a package data_classes that can convert a mutable class from and to an immutable class pendant.
So you could define a mutable class in a few lines of code and annotate it with @HiveType. An immutable class gets generated that can be used everywhere else in the app. When you want to save such a class to hive, you can convert it to the mutable class and save this to hive. Vice versa for retrieving objects from hive.

from hive.

simc avatar simc commented on August 23, 2024

I think it is not that complex to make it work with named and positioned parameters. The only thing I would require is that the constructor uses the this.myAttribute initialization.

A constructor may look like this:

class MyClass {
  String name;
  int age;

  MyClass(this.age, {this.name});

The data_classes package is really cool...

from hive.

MarcelGarus avatar MarcelGarus commented on August 23, 2024

Okay, so we use the unnamed constructor and set only those fields that are assigned with this.field.
Just to be sure, let's say we have a class

@HiveType()
class MyClass {
  @HiveField(0)
  final String name;

  @HiveField(1)
  int age;

  @HiveField(2)
  int fieldThatIsNotInTheConstructor;

  MyClass(this.age, {this.name});
}

I'll try to implement the generator so that the generated code could then look like this:

MyClass read(BinaryReader reader) {
  var numOfFields = reader.readByte();
  var fields = <int, dynamic>{
    for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
  };
  return MyClass(
    fields[1] as int,
    name: fields[0] as String,
  )..fieldThatIsNotInTheConstructor = fields[2] as int;
}

from hive.

simc avatar simc commented on August 23, 2024

Yes that looks good πŸ‘

from hive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.