Skip to content

Commit

Permalink
#130: added default values
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Sep 6, 2023
1 parent e450b8d commit 06c6b8b
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 22 deletions.
11 changes: 8 additions & 3 deletions lib/config/yml_generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,21 @@ class YmlGeneratorConfig {
final fields = <EnumField>[];
final enumProperties = <EnumProperty>[];
properties?.forEach((propertyKey, propertyValue) {
final ItemType itemType;
final String type;
final bool isJsonKey;
final String type;
final String? defaultValue;
final ItemType itemType;

final String name = propertyKey;

if (propertyValue is YamlMap) {
type = propertyValue['type'];
isJsonKey = propertyValue['is_json_key'] == true;
defaultValue = propertyValue['default_value'];
} else {
type = propertyValue;
isJsonKey = false;
isJsonKey = false;
defaultValue = null;
}

final optional = type.endsWith('?');
Expand All @@ -123,6 +127,7 @@ class YmlGeneratorConfig {
type: itemType,
isJsonKey: isJsonKey,
isOptional: optional,
defaultValue: defaultValue,
));
});

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 @@ -57,12 +57,14 @@ class EnumProperty {
final bool isJsonKey;
final bool isOptional;
final String name;
String? defaultValue;
ItemType type;

EnumProperty({
required this.name,
required this.type,
required this.isOptional,
this.defaultValue,
this.isJsonKey = false,
});
}
Expand Down
23 changes: 13 additions & 10 deletions lib/writer/enum_model_writer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 Down Expand Up @@ -53,13 +52,14 @@ class EnumModelWriter {
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}\',');
for (var property in properties) {
final enumValue = valueForProperty(property.name, key.values);
final value = enumValue?.value ?? property.defaultValue;
sb.write(' ${property.name}: ');
if (property.type is StringType && value != null) {
sb.writeln('\'$value\',');
} else {
sb.writeln('${value.value},');
sb.writeln('$value,');
}
}
if (isLast) {
Expand Down Expand Up @@ -94,7 +94,7 @@ class EnumModelWriter {
}
for (var property in properties) {
sb.write(' ');
if(!property.isOptional){
if (!property.isOptional) {
sb.write('required ');
}
sb.writeln('this.${property.name},');
Expand All @@ -107,6 +107,9 @@ class EnumModelWriter {
return sb.toString();
}

ItemType itemTypeForProperty(String propertyName, List<EnumProperty> properties) =>
properties.firstWhereOrNull((property) => property.name == propertyName)?.type ?? StringType();
EnumValue? valueForProperty(
String propertyName,
List<EnumValue> values,
) =>
values.firstWhereOrNull((value) => value.propertyName == propertyName);
}
19 changes: 19 additions & 0 deletions test/writer/enum_model_writer/default_values/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
MyEnumModel:
path: test/enum/
type: enum
properties:
firstName: String
lastName:
type: String
default_value: lastName
values:
MY_VALUE_1:
properties:
firstName: firstName
MY_VALUE_2:
properties:
firstName: firstName
MY_VALUE_3:
properties:
firstName: firstName
lastName: specifiedLastName
34 changes: 34 additions & 0 deletions test/writer/enum_model_writer/default_values/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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_1',
firstName: 'firstName',
lastName: 'lastName',
),
@JsonValue('MY_VALUE_2')
MY_VALUE_2(
jsonValue: 'MY_VALUE_2',
firstName: 'firstName',
lastName: 'lastName',
),
@JsonValue('MY_VALUE_3')
MY_VALUE_3(
jsonValue: 'MY_VALUE_3',
firstName: 'firstName',
lastName: 'specifiedLastName',
);

final String jsonValue;
final String firstName;
final String lastName;

const MyEnumModel({
required this.jsonValue,
required this.firstName,
required this.lastName,
});
}
16 changes: 11 additions & 5 deletions test/writer/enum_model_writer/full_enum/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ Person:
is_json_key: true
type: int
firstName: String
lastName: String
age: int
height: double
lastName:
type: String
default_value: lastName
age: int?
height: double?
values:
MAN:
description: enum of a man
properties:
jsonKey: 1
firstName: firstName1
lastName: lastName1
age: 12
height: 12.4
WOMAN:
description: enum of a woman
properties:
jsonKey: 2
firstName: firstName2
lastName: lastName2
age: 16
height: 22
OTHER:
description: enum of a other
properties:
jsonKey: 3
firstName: firstName3
lastName: SpecifiedLastName
28 changes: 24 additions & 4 deletions test/writer/enum_model_writer/full_enum/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,47 @@

import 'package:json_annotation/json_annotation.dart';

///This is a enum of a person
enum Person {
///enum of a man
@JsonValue(1)
MY_VALUE_1(
MAN(
jsonKey: 1,
firstName: 'firstName1',
lastName: 'lastName1',
lastName: 'lastName',
age: 12,
height: 12.4,
),
///enum of a woman
@JsonValue(2)
MY_VALUE_2(
WOMAN(
jsonKey: 2,
firstName: 'firstName2',
lastName: 'lastName2',
lastName: 'lastName',
age: 16,
height: 22,
),
///enum of a other
@JsonValue(3)
OTHER(
jsonKey: 3,
firstName: 'firstName3',
lastName: 'SpecifiedLastName',
age: null,
height: null,
);

final int jsonKey;
final String firstName;
final String lastName;
final int? age;
final double? height;

const Person({
required this.jsonKey,
required this.firstName,
required this.lastName,
this.age,
this.height,
});
}
13 changes: 13 additions & 0 deletions test/writer/enum_model_writer/optional_values_default/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
MyEnumModel:
path: test/enum/
type: enum
properties:
firstName: String
lastName: String?
values:
MY_VALUE_1:
properties:
firstName: firstName
MY_VALUE_2:
properties:
firstName: firstName
28 changes: 28 additions & 0 deletions test/writer/enum_model_writer/optional_values_default/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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_1',
firstName: 'firstName',
lastName: null,
),
@JsonValue('MY_VALUE_2')
MY_VALUE_2(
jsonValue: 'MY_VALUE_2',
firstName: 'firstName',
lastName: null,
);

final String jsonValue;
final String firstName;
final String? lastName;

const MyEnumModel({
required this.jsonValue,
required this.firstName,
this.lastName,
});
}

0 comments on commit 06c6b8b

Please sign in to comment.