Skip to content

Commit

Permalink
#130: finished property functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Sep 5, 2023
1 parent fcf0054 commit db94271
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/config/yml_generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class YmlGeneratorConfig {
});

models.add(EnumModel(
addJsonKeyToProperties: value['use_default_json_key'] ?? true,
name: key,
path: path,
baseDirectory: baseDirectory,
Expand Down
2 changes: 2 additions & 0 deletions lib/model/model/enum_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import 'package:model_generator/model/model/model.dart';
class EnumModel extends Model {
final List<EnumField> fields;
final List<EnumProperty> properties;
final bool addJsonKeyToProperties;

EnumModel({
required String name,
required this.fields,
required this.properties,
this.addJsonKeyToProperties = true,
String? path,
String? baseDirectory,
List<String>? extraImports,
Expand Down
49 changes: 40 additions & 9 deletions lib/writer/enum_model_writer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:model_generator/model/item_type/double_type.dart';
import 'package:model_generator/model/item_type/item_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';
Expand All @@ -24,10 +25,12 @@ class EnumModelWriter {

final jsonModelName = CaseUtil(jsonModel.name);
final properties = jsonModel.properties;
final keyProperty = properties.firstWhereOrNull((property) => property.isJsonKey);
final addDefaultJsonKey = keyProperty == null && jsonModel.addJsonKeyToProperties;
final addProperties = properties.isNotEmpty || addDefaultJsonKey;

sb.writeln('enum ${jsonModelName.pascalCase} {');
for (var key in jsonModel.fields) {
final keyProperty = properties.firstWhereOrNull((property) => property.isJsonKey);
final jsonValue = key.values.firstWhereOrNull((value) => value.propertyName == keyProperty?.name)?.value ?? key.serializedName;
final propertyType = keyProperty?.type;
final isLast = jsonModel.fields.indexOf(key) == (jsonModel.fields.length - 1);
Expand All @@ -41,30 +44,58 @@ class EnumModelWriter {
sb.writeln(' @JsonValue($jsonValue)');
}
sb.write(' ${key.name}');
if (properties.isNotEmpty && isLast) {
sb.writeln(';');

if (addProperties) {
sb.writeln('(');
if (addDefaultJsonKey) {
sb.writeln(' jsonValue: \'$jsonValue\',');
}
for (var value in key.values) {
final type = itemTypeForProperty(value.propertyName, properties);
sb.write(' ${value.propertyName}: ');
if (type is StringType) {
sb.writeln('\'${value.value}\',');
} else {
sb.writeln('${value.value},');
}
}
if (isLast) {
sb.writeln(' );');
} else {
sb.writeln(' ),');
}
} else {
sb.writeln(',');
}
}

if (properties.isNotEmpty) {
if (addProperties) {
sb.writeln();
}

if (addDefaultJsonKey) {
sb.writeln(' final String jsonValue;');
}
for (var property in properties) {
sb.writeln('final ${property.type.name} ${property.name};');
sb.writeln(' final ${property.type.name} ${property.name};');
}
if (properties.isNotEmpty) {
sb.write('Const ${jsonModel.name} ({');
if (addProperties) {
sb.writeln();
sb.writeln(' const ${jsonModelName.pascalCase}({');
for (var property in properties) {
sb.write('required this.${property.name}, ');
sb.writeln(' required this.${property.name},');
}
sb.writeln('})');
if (addDefaultJsonKey) {
sb.writeln(' required this.jsonValue,');
}
sb.writeln(' });');
}

sb.writeln('}');

return sb.toString();
}

ItemType itemTypeForProperty(String propertyName, List<EnumProperty> properties) =>
properties.firstWhereOrNull((property) => property.name == propertyName)?.type ?? StringType();
}
10 changes: 8 additions & 2 deletions test/writer/enum_model_writer/custom-value/config.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
MyEnumModel:
Person:
path: test/enum/
type: enum
properties:
jsonKey:
is_json_key: true
type: int
firstName: String
lastName: String
values:
MY_VALUE_1:
properties:
jsonKey: 1
firstName: firstName1
lastName: lastName1
MY_VALUE_2:
properties:
jsonKey: 2
jsonKey: 2
firstName: firstName2
lastName: lastName2
24 changes: 21 additions & 3 deletions test/writer/enum_model_writer/custom-value/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@

import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
enum Person {
@JsonValue(1)
MY_VALUE_1,
MY_VALUE_1(
jsonKey: 1,
firstName: 'firstName1',
lastName: 'lastName1',
),
@JsonValue(2)
MY_VALUE_2,
MY_VALUE_2(
jsonKey: 2,
firstName: 'firstName2',
lastName: 'lastName2',
);

final int jsonKey;
final String firstName;
final String lastName;

const Person({
required this.jsonKey,
required this.firstName,
required this.lastName,
});
}
14 changes: 12 additions & 2 deletions test/writer/enum_model_writer/normal-description/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import 'package:json_annotation/json_annotation.dart';
///A good description of this enum
enum MyEnumModel {
@JsonValue('MY_VALUE_1')
MY_VALUE_1,
MY_VALUE_1(
jsonValue: 'MY_VALUE_1',
),
@JsonValue('MY_VALUE_2')
MY_VALUE_2,
MY_VALUE_2(
jsonValue: 'MY_VALUE_2',
);

final String jsonValue;

const MyEnumModel({
required this.jsonValue,
});
}
14 changes: 12 additions & 2 deletions test/writer/enum_model_writer/normal/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
@JsonValue('MY_VALUE_1')
MY_VALUE_1,
MY_VALUE_1(
jsonValue: 'MY_VALUE_1',
),
@JsonValue('MY_VALUE_2')
MY_VALUE_2,
MY_VALUE_2(
jsonValue: 'MY_VALUE_2',
);

final String jsonValue;

const MyEnumModel({
required this.jsonValue,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MyEnumModel:
path: test/enum/
use_default_json_key: false
type: enum
values:
MY_VALUE_1:
MY_VALUE_2:
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// GENERATED CODE - DO NOT MODIFY BY HAND

import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
@JsonValue('MY_VALUE_1')
MY_VALUE_1,
@JsonValue('MY_VALUE_2')
MY_VALUE_2,
}
14 changes: 14 additions & 0 deletions test/writer/enum_model_writer/normal_with_double_type/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MyEnumModel:
path: test/enum/
type: enum
properties:
value:
type: double
is_json_key: true
values:
MY_VALUE_1:
properties:
value: 1.2
MY_VALUE_2:
properties:
value: 2.2
17 changes: 13 additions & 4 deletions test/writer/enum_model_writer/normal_with_double_type/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
///A good description of this field
@JsonValue(1.0)
MY_VALUE_1,
@JsonValue(1.2)
MY_VALUE_1(
value: 1.2,
),
@JsonValue(2.2)
MY_VALUE_2,
MY_VALUE_2(
value: 2.2,
);

final double value;

const MyEnumModel({
required this.value,
});
}
14 changes: 14 additions & 0 deletions test/writer/enum_model_writer/normal_with_int_type/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MyEnumModel:
path: test/enum/
type: enum
properties:
value:
type: int
is_json_key: true
values:
MY_VALUE_1:
properties:
value: 1
MY_VALUE_2:
properties:
value: 2
15 changes: 12 additions & 3 deletions test/writer/enum_model_writer/normal_with_int_type/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
///A good description of this field
@JsonValue(1)
MY_VALUE_1,
MY_VALUE_1(
value: 1,
),
@JsonValue(2)
MY_VALUE_2,
MY_VALUE_2(
value: 2,
);

final int value;

const MyEnumModel({
required this.value,
});
}

0 comments on commit db94271

Please sign in to comment.