Mobx.dart: mobx_codegen: allow generating toString method for Store class

Created on 10 May 2019  路  15Comments  路  Source: mobxjs/mobx.dart

enhancement help wanted good first issue

Most helpful comment

We could go even further, and make the generated class Diagnosticable (docs) if the store is marked with a @forFlutter annotation or something.

That way we'd get all sorts of debugging goodness in debuggers.

More significant project though.

All 15 comments

Curious what would be the implementation of the toString() method ?

smth simple like built_value (based on core state fields) would be good enough for me

Interesting idea.
Flutter likes to have custom toString to make debugging easier.

For example, instead of MyClass, some widgets have a toString that looks like MyClass(foo: 42, bar: 'hello world')

See DiagnosticNode for more info

that format would be ok as well, even tough personally i'd prefer built_value format (for development-time logging), because built_value formats properties on separate lines and excluded nulls :).
Ideally the format could be customized based on annotation parameter :)

If you need a custom toString(), you can always add that to the concrete Store class:

class CustomStore extends _CustomStore with _$CustomStore {
  @override
  toString() { /* ... */ }
}

That said, we can add this to the backlog for generating a toString() implementation with the observables and computeds. Would that work ?

@pavanpodila,

If you need a custom toString(), you can always add that to the concrete Store class:

yes, that's the manual approach I've used so far :)

we can add this to the backlog for generating a toString() implementation with the observables and computeds. Would that work ?

Yes, absolutely. I'd probably prefer just observables (core state) to be included in generated toString, but I see that some might prefer to include computeds as well. Ideally inclusion of computeds could be determined based on annotation parameter.

@atsu85, do you want to take a stab at it? All changes are in the mobx_codegen package. I can help you tackle it and you will get to add your name to the esteemed list of contributors 馃殌

We could go even further, and make the generated class Diagnosticable (docs) if the store is marked with a @forFlutter annotation or something.

That way we'd get all sorts of debugging goodness in debuggers.

More significant project though.

Wanted to try this "good first issue" but.. is this enhancement still needed ?
I see code has changed a lot since May and it looks like dispose method (#177) has been removed from Store (#334) .

I think it's still worth having a useful toString(), so go all for it 馃憤. Yes, a few things have changed, but you can certainly add the toString() to the mixin.

Maybe as an opt-in via the annotation?

@Store(generateToString: true)

Ya let's make that a config object with that as the optional member, defaulting to false

With no prior experience of mobx and its dependencies I have some questions :)
1/ Is it the right way to implement this ?
I've done a basic case on observables without annotation so far in order to figure how to do things:
Created a "ToStrings" template

import 'package:mobx_codegen/src/template/store.dart';

class ToStringTemplate {
  StoreTemplate storeTemplate;
  String name;

  @override
  String toString() => '_toString += \'$name: \${$name.toString()}, \';';
}

in store.dart I added a new Rows and added it to the storeBody

  final Rows<ObservableStreamTemplate> observableStreams = Rows();
  final Rows<ToStringTemplate> toStrings = Rows();

  String _actionControllerName;

  $actionControllerField

  $actions

  @override
  String toString() {
    var _toString = '';
    $toStrings
    return '{\$_toString}';
  }''';

In store_class_visitor.dart I add the new row during visitFieldElement

    _storeTemplate.observables.add(template);

    final toString = ToStringTemplate()
      ..storeTemplate = _storeTemplate
      ..name = element.name;

    _storeTemplate.toStrings.add(toString);

2/ What is the correct way to regenerate the _test.g.dart files because
flutter packages pub run build_runner build
messed-up something when dealing with the content of test/data

3/ which annotation ?
should we use a global annotation like
@Store(generateToString: true)
or let dev subscribe for the individual observables and computed he wants
@observable(generateToString: true) @computed(generateToString: true)

Hello @hawkbee1, good try on your first attempt 馃憤

You won't need to use a generator for this part of the code. It will sit as part of the Store template that writes out the fields in a convenient way. You will have to generate the code for the toString method within this template

Closing as its now merged with PR #414

Was this page helpful?
0 / 5 - 0 ratings