Skip to content

Commit

Permalink
Merge pull request #133 from icapps/fix-enum-map-and-extension
Browse files Browse the repository at this point in the history
Fix enum map and extension
  • Loading branch information
vanlooverenkoen committed Mar 8, 2023
2 parents aa15fda + 32d9b5b commit 95c36ad
Show file tree
Hide file tree
Showing 17 changed files with 364 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## [6.2.1] - 2023-03-07
- Updated the enum model generator to correctly generate the (reverse)Mapping based on the item_type

## [6.2.0] - 2023-02-23
- Better enum support. Right now, String, int, double can be used to map a value to an enum.

Expand Down
1 change: 0 additions & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ linter:
- file_names
- hash_and_equals
- implementation_imports
- invariant_booleans
- iterable_contains_unrelated_type
- join_return_with_assignment
- library_names
Expand Down
36 changes: 36 additions & 0 deletions example/lib/model/status/double_status.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions example/lib/model/status/status.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions example/model_generator/enums.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ Gender:
value:
GENDER_lap:
value: GENDER_lap

Status:
path: status
type: enum
item_type: int
generate_map: true
generate_extensions: true
properties:
status_0:
value: 0
Expand All @@ -31,3 +34,19 @@ Status:
value: 2
status_3:
value: 3

DoubleStatus:
path: status
type: enum
item_type: double
generate_map: true
generate_extensions: true
properties:
status_0:
value: 0.0
status_1:
value: 1.0
status_2:
value: 2.0
status_3:
value: 3.0
18 changes: 11 additions & 7 deletions lib/util/case_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ class CaseUtil {
return words;
}

String get snakeCase => _getSnakeCase();

String get camelCase => _getCamelCase();

String _getSnakeCase({String separator = '_'}) =>
_words.map((word) => word.toLowerCase()).toList().join(separator);
String get pascalCase => _getPascalCase();

String get snakeCase => _getSnakeCase();

String _getCamelCase({String separator = ''}) {
final words = _words.map(_upperCaseFirstLetter).toList();
Expand All @@ -55,7 +54,12 @@ class CaseUtil {
return words.join(separator);
}

static String _upperCaseFirstLetter(String word) {
return '${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';
}
String _getPascalCase({String separator = ''}) =>
_words.map(_upperCaseFirstLetter).toList().join(separator);

String _getSnakeCase({String separator = '_'}) =>
_words.map((word) => word.toLowerCase()).toList().join(separator);

String _upperCaseFirstLetter(String word) =>
'${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';
}
58 changes: 42 additions & 16 deletions lib/writer/enum_model_writer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:model_generator/model/item_type/double_type.dart';
import 'package:model_generator/model/item_type/string_type.dart';
import 'package:model_generator/model/model/enum_model.dart';
import 'package:model_generator/util/case_util.dart';
import 'package:model_generator/writer/object_model_writer.dart';

class EnumModelWriter {
Expand All @@ -19,7 +21,10 @@ class EnumModelWriter {
sb.writeln("///$modelDescription");
}

sb.writeln('enum ${jsonModel.name} {');
final jsonModelName = CaseUtil(jsonModel.name);
final itemTypeName = CaseUtil(jsonModel.itemType.name);

sb.writeln('enum ${jsonModelName.pascalCase} {');
jsonModel.fields?.forEach((key) {
final jsonValue = key.value == null || key.value?.isEmpty == null
? key.serializedName
Expand All @@ -29,9 +34,12 @@ class EnumModelWriter {
sb.writeln(' ///$description');
}
if (jsonModel.itemType is StringType) {
sb.writeln(" @JsonValue('$jsonValue')");
sb.writeln(' @JsonValue(\'$jsonValue\')');
} else if (jsonModel.itemType is DoubleType && jsonValue != null) {
final doubleValue = double.tryParse(jsonValue);
sb.writeln(' @JsonValue($doubleValue)');
} else {
sb.writeln(" @JsonValue($jsonValue)");
sb.writeln(' @JsonValue($jsonValue)');
}
sb.writeln(' ${key.name},');
});
Expand All @@ -40,29 +48,46 @@ class EnumModelWriter {
if (jsonModel.generateMap) {
sb
..writeln()
..writeln('const ${jsonModel.name}Mapping = {');
..writeln('const ${jsonModelName.camelCase}Mapping = {');

jsonModel.fields?.forEach((key) {
final jsonValue = key.value == null || key.value?.isEmpty == null
? key.serializedName
: key.value;
sb
..write(' ${jsonModel.name}.${key.name}: ')
..writeln('\'$jsonValue\',');
sb.write(' ${jsonModelName.pascalCase}.${key.name}: ');
if (jsonModel.itemType is StringType) {
sb.writeln('\'$jsonValue\',');
} else if (jsonModel.itemType is DoubleType && jsonValue != null) {
final doubleValue = double.tryParse(jsonValue);
sb.writeln('$doubleValue,');
} else {
sb.writeln('$jsonValue,');
}
});

sb
..writeln('};')
..writeln()
..writeln('const reverse${jsonModel.name}Mapping = {');
..writeln();

if (jsonModel.itemType is DoubleType) {
sb.writeln('final reverse${jsonModelName.pascalCase}Mapping = {');
} else {
sb.writeln('const reverse${jsonModelName.pascalCase}Mapping = {');
}

jsonModel.fields?.forEach((key) {
final jsonValue = key.value == null || key.value?.isEmpty == null
? key.serializedName
: key.value;
sb
..write(' \'$jsonValue\': ')
..writeln('${jsonModel.name}.${key.name},');
if (jsonModel.itemType is StringType) {
sb.write(' \'$jsonValue\': ');
} else if (jsonModel.itemType is DoubleType && jsonValue != null) {
final doubleValue = double.tryParse(jsonValue);
sb.write(' $doubleValue: ');
} else {
sb.write(' $jsonValue: ');
}
sb.writeln('${jsonModelName.pascalCase}.${key.name},');
});

sb.writeln('};');
Expand All @@ -71,14 +96,15 @@ class EnumModelWriter {
sb
..writeln()
..writeln(
'extension ${jsonModel.name}Extension on ${jsonModel.name} {')
'extension ${jsonModelName.pascalCase}Extension on ${jsonModelName.pascalCase} {')
..writeln(
' String get stringValue => ${jsonModel.name}Mapping[this]!;')
' ${itemTypeName.originalText} get ${itemTypeName.camelCase}Value => ${jsonModelName.camelCase}Mapping[this]!;')
..writeln('}')
..writeln()
..writeln('extension ${jsonModel.name}StringExtension on String {')
..writeln(
' ${jsonModel.name}? get as${jsonModel.name} => reverse${jsonModel.name}Mapping[this];')
'extension ${jsonModelName.pascalCase}${itemTypeName.pascalCase}Extension on ${itemTypeName.originalText} {')
..writeln(
' ${jsonModelName.pascalCase}? get as${jsonModelName.pascalCase} => reverse${jsonModelName.pascalCase}Mapping[this];')
..writeln('}');
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: model_generator
description: Dart tool to automaticly generate models from a yml file to speed up your development flow.
version: 6.2.0
version: 6.2.1
homepage: https://github.com/icapps/flutter-model-generator

environment:
Expand Down
27 changes: 21 additions & 6 deletions test/util/case_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,42 @@ void main() {
test('Case Util normal text all lowercase', () {
expect(CaseUtil('hallokes').originalText, 'hallokes');
expect(CaseUtil('hallokes').camelCase, 'hallokes');
expect(CaseUtil('hallokes').pascalCase, 'Hallokes');
expect(CaseUtil('hallokes').snakeCase, 'hallokes');
});
test('Case Util normal text all Uppercase', () {

test('Case Util normal text all uppercase', () {
expect(CaseUtil('HALLOKES').originalText, 'HALLOKES');
expect(CaseUtil('HALLOKES').camelCase, 'hallokes');
expect(CaseUtil('HALLOKES').pascalCase, 'Hallokes');
expect(CaseUtil('HALLOKES').snakeCase, 'hallokes');
});

test('Case Util camel case', () {
expect(CaseUtil('hiThere').originalText, 'hiThere');
expect(CaseUtil('hiThere').camelCase, 'hiThere');
expect(CaseUtil('hiThere').pascalCase, 'HiThere');
expect(CaseUtil('hiThere').snakeCase, 'hi_there');
});

test('Case Util pascal case', () {
expect(CaseUtil('HiThere').originalText, 'HiThere');
expect(CaseUtil('HiThere').camelCase, 'hiThere');
expect(CaseUtil('HiThere').pascalCase, 'HiThere');
expect(CaseUtil('HiThere').snakeCase, 'hi_there');
});

test('Case Util snake case', () {
expect(CaseUtil('hi_there').originalText, 'hi_there');
expect(CaseUtil('hi_there').camelCase, 'hiThere');
expect(CaseUtil('hi_there').pascalCase, 'HiThere');
expect(CaseUtil('hi_there').snakeCase, 'hi_there');
});
test('Case Util camelCase', () {
expect(CaseUtil('hiThere').originalText, 'hiThere');
expect(CaseUtil('hiThere').camelCase, 'hiThere');
expect(CaseUtil('hiThere').snakeCase, 'hi_there');
});

test('Case Util ', () {
expect(CaseUtil('hi-there').originalText, 'hi-there');
expect(CaseUtil('hi-there').camelCase, 'hiThere');
expect(CaseUtil('hi-there').pascalCase, 'HiThere');
expect(CaseUtil('hi-there').snakeCase, 'hi_there');
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/writer/enum_model_writer/custom-value-map-extension.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum MyEnumModel {
MY_VALUE_2,
}

const MyEnumModelMapping = {
const myEnumModelMapping = {
MyEnumModel.MY_VALUE_1: 'customValue',
MyEnumModel.MY_VALUE_2: 'MY_VALUE_2',
};
Expand All @@ -20,7 +20,7 @@ const reverseMyEnumModelMapping = {
};

extension MyEnumModelExtension on MyEnumModel {
String get stringValue => MyEnumModelMapping[this]!;
String get stringValue => myEnumModelMapping[this]!;
}

extension MyEnumModelStringExtension on String {
Expand Down
2 changes: 1 addition & 1 deletion test/writer/enum_model_writer/custom-value-map.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum MyEnumModel {
MY_VALUE_2,
}

const MyEnumModelMapping = {
const myEnumModelMapping = {
MyEnumModel.MY_VALUE_1: 'customValue',
MyEnumModel.MY_VALUE_2: 'MY_VALUE_2',
};
Expand Down
2 changes: 1 addition & 1 deletion test/writer/enum_model_writer/normal_with_double_type.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
///A good description of this field
@JsonValue(1)
@JsonValue(1.0)
MY_VALUE_1,
@JsonValue(2.2)
MY_VALUE_2,
Expand Down
21 changes: 21 additions & 0 deletions test/writer/enum_model_writer/normal_with_double_type_map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// GENERATED CODE - DO NOT MODIFY BY HAND

import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
///A good description of this field
@JsonValue(1.0)
MY_VALUE_1,
@JsonValue(2.2)
MY_VALUE_2,
}

const myEnumModelMapping = {
MyEnumModel.MY_VALUE_1: 1.0,
MyEnumModel.MY_VALUE_2: 2.2,
};

final reverseMyEnumModelMapping = {
1.0: MyEnumModel.MY_VALUE_1,
2.2: MyEnumModel.MY_VALUE_2,
};

0 comments on commit 95c36ad

Please sign in to comment.