https://github.com/orthros/dart-opt
https://github.com/orthros/dart-opt
dart dartlang metadata mirrors reflection
Last synced: 21 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/orthros/dart-opt
- Owner: orthros
- License: mit
- Created: 2017-06-08T14:28:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-09T01:36:40.000Z (about 9 years ago)
- Last Synced: 2025-10-25T21:58:46.142Z (9 months ago)
- Topics: dart, dartlang, metadata, mirrors, reflection
- Language: Dart
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dart_opt
This exists to serve as an option set generator for a Dart.
The general idea is that for an application there may need to be an object (or several) that you read in a set of key value pairs for and pass into a class.
## Note:
I know there are other ways of generating the constructor body for a class automatically using tools like `source_gen`, but I choose to do this the "manual" way as I find it a good exercise in a language to explore their reflection/mirrors api and other language level features like annotations/attributes
## Use
Write your class as a subclass of OptionSet and annotate the
member variables you wish to be auto populated from the Map with their appropriate type
Example (from the tests)
```dart
class MocOptionSet extends OptionSet {
@StringOption(optionName: "myStringField", defaultValue: "defaultValue")
String myString;
@NumOption(optionName: "myNumber", defaultValue: 10)
num myNum;
@BoolOption(optionName: "myBoolean")
bool myBool;
@boolOption
bool unnamedBool;
@stringOption
String unnamedString;
@numOption
num unnamedNum;
MocOptionSet(Map theMap) : super(theMap);
}
```
In this example, the to provide a value for the field `myBool` the Map would need a key: myBoolean and a `bool` value.
To pass a value for the field `unnamedBool` the Map would need a key: unnamedBool with a `bool` value.
## TODO
- [ ] Add more api-level documentation
- [ ] Add more unit tests
- [ ] Support Enum types