All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dart2.serialization.native.native_enum_inline.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{#description}}/// {{{.}}}{{/description}}
class {{{enumName}}} {
  /// Instantiate a new enum with the provided [value].
  const {{{enumName}}}._(this.value);

  /// The underlying value of this enum member.
  final {{{dataType}}} value;

  @override
  String toString() => {{#isString}}value{{/isString}}{{^isString}}value.toString(){{/isString}};

  {{{dataType}}} toJson() => value;

  {{#allowableValues}}
    {{#enumVars}}
  static const {{{name}}} = {{{enumName}}}._({{#isString}}r{{/isString}}{{{value}}});
    {{/enumVars}}
  {{/allowableValues}}

  /// List of all possible values in this [enum][{{{enumName}}}].
  static const values = <{{{enumName}}}>[
  {{#allowableValues}}
    {{#enumVars}}
    {{{name}}},
    {{/enumVars}}
  {{/allowableValues}}
  ];

  static {{{enumName}}}? fromJson(dynamic value) => {{{enumName}}}TypeTransformer().decode(value);

  static List<{{{enumName}}}> listFromJson(dynamic json, {bool growable = false,}) {
    final result = <{{{enumName}}}>[];
    if (json is List && json.isNotEmpty) {
      for (final row in json) {
        final value = {{{enumName}}}.fromJson(row);
        if (value != null) {
          result.add(value);
        }
      }
    }
    return result.toList(growable: growable);
  }
}

/// Transformation class that can [encode] an instance of [{{{enumName}}}] to {{{dataType}}},
/// and [decode] dynamic data back to [{{{enumName}}}].
class {{{enumName}}}TypeTransformer {
  factory {{{enumName}}}TypeTransformer() => _instance ??= const {{{enumName}}}TypeTransformer._();

  const {{{enumName}}}TypeTransformer._();

  {{{dataType}}} encode({{{enumName}}} data) => data.value;

  /// Decodes a [dynamic value][data] to a {{{enumName}}}.
  ///
  /// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
  /// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
  /// cannot be decoded successfully, then an [UnimplementedError] is thrown.
  ///
  /// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
  /// and users are still using an old app with the old code.
  {{{enumName}}}? decode(dynamic data, {bool allowNull = true}) {
    if (data != null) {
      switch (data) {
        {{#allowableValues}}
          {{#enumVars}}
        case {{#isString}}r{{/isString}}{{{value}}}: return {{{enumName}}}.{{{name}}};
          {{/enumVars}}
        {{/allowableValues}}
        default:
          if (!allowNull) {
            throw ArgumentError('Unknown enum value to decode: $data');
          }
      }
    }
    return null;
  }

  /// Singleton [{{{enumName}}}TypeTransformer] instance.
  static {{{enumName}}}TypeTransformer? _instance;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy