Skip to content

Commit 8a38759

Browse files
committed
#135: adde simple enum assignment
1 parent 31954c0 commit 8a38759

File tree

8 files changed

+159
-11
lines changed

8 files changed

+159
-11
lines changed

lib/config/yml_generator_config.dart

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,47 @@ class YmlGeneratorConfig {
163163
));
164164
});
165165

166-
final values = value['values'] as YamlMap?;
166+
final values = value['values'];
167167
if (values == null) {
168168
throw Exception('Values can not be null. model: $key');
169169
}
170170

171+
ItemType? simpleType;
172+
171173
values.forEach((key, value) {
172-
final properties = value?['properties'] as YamlMap?;
173-
final description = value?['description'];
174174
final enumValues = <EnumValue>[];
175+
String? description;
176+
if (value is YamlMap) {
177+
final properties = value['properties'] as YamlMap?;
178+
description = value['description'];
179+
180+
properties?.forEach((key, value) {
181+
enumValues.add(
182+
EnumValue(
183+
value: value.toString(),
184+
propertyName: key,
185+
),
186+
);
187+
});
188+
} else if (value != null) {
189+
final valueType = switch (value.runtimeType) {
190+
String => StringType(),
191+
int => IntegerType(),
192+
double => DoubleType(),
193+
_ => StringType(),
194+
};
195+
if (simpleType == null) {
196+
simpleType = valueType;
197+
} else if (simpleType?.name != valueType.name) {
198+
throw Exception(
199+
'All values in a simple enum declaration should have the same value type, value ${simpleType?.name} is not ${valueType.name}. enum value: $key');
200+
}
201+
enumValues.add(EnumValue(
202+
value: value.toString(),
203+
propertyName: 'jsonValue',
204+
));
205+
}
175206

176-
properties?.forEach((key, value) {
177-
enumValues.add(
178-
EnumValue(
179-
value: value.toString(),
180-
propertyName: key,
181-
),
182-
);
183-
});
184207
fields.add(EnumField(
185208
name: uppercaseEnums ? key.toUpperCase() : CaseUtil(key).camelCase,
186209
rawName: key,
@@ -189,6 +212,21 @@ class YmlGeneratorConfig {
189212
));
190213
});
191214

215+
if (simpleType != null) {
216+
if (enumProperties.isNotEmpty) {
217+
throw Exception(
218+
'Simple enum declaration only works if no properties are defined, model: $key');
219+
}
220+
enumProperties.add(
221+
EnumProperty(
222+
name: 'jsonValue',
223+
type: simpleType!,
224+
isOptional: false,
225+
isJsonvalue: true,
226+
),
227+
);
228+
}
229+
192230
final enumModel = EnumModel(
193231
addJsonValueToProperties: value['use_default_json_value'] ?? true,
194232
generateExtension: value['generate_extension'] == true,

test/writer/enum_model_reader_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,38 @@ Gender:
356356
FEMALE:
357357
properties:
358358
isMale: hello
359+
""",
360+
));
361+
test(
362+
'Simple enum declaration only works with empty properties',
363+
() => testEnumError(
364+
expectedError:
365+
'Exception: Simple enum declaration only works if no properties are defined, model: Gender',
366+
enumYml: """
367+
Gender:
368+
path: user/person/
369+
type: enum
370+
properties:
371+
isMale:
372+
is_json_key: true
373+
type: String
374+
values:
375+
MALE: hello
376+
FEMALE: hello
377+
""",
378+
));
379+
test(
380+
'Simple enum declaration only works with String, int and double',
381+
() => testEnumError(
382+
expectedError:
383+
'Exception: All values in a simple enum declaration should have the same value type, value int is not String. enum value: FEMALE',
384+
enumYml: """
385+
Gender:
386+
path: user/person/
387+
type: enum
388+
values:
389+
MALE: 1
390+
FEMALE: female
359391
""",
360392
));
361393
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MyEnumModel:
2+
path: test/enum/
3+
type: enum
4+
values:
5+
MY_VALUE_1: 1.2
6+
MY_VALUE_2: 2.2
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// GENERATED CODE - DO NOT MODIFY BY HAND
2+
3+
import 'package:json_annotation/json_annotation.dart';
4+
5+
enum MyEnumModel {
6+
@JsonValue(1.2)
7+
myValue1(
8+
jsonValue: 1.2,
9+
),
10+
@JsonValue(2.2)
11+
myValue2(
12+
jsonValue: 2.2,
13+
);
14+
15+
final double jsonValue;
16+
17+
const MyEnumModel({
18+
required this.jsonValue,
19+
});
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MyEnumModel:
2+
path: test/enum/
3+
type: enum
4+
values:
5+
MY_VALUE_1: 1
6+
MY_VALUE_2: 2
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// GENERATED CODE - DO NOT MODIFY BY HAND
2+
3+
import 'package:json_annotation/json_annotation.dart';
4+
5+
enum MyEnumModel {
6+
@JsonValue(1)
7+
myValue1(
8+
jsonValue: 1,
9+
),
10+
@JsonValue(2)
11+
myValue2(
12+
jsonValue: 2,
13+
);
14+
15+
final int jsonValue;
16+
17+
const MyEnumModel({
18+
required this.jsonValue,
19+
});
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MyEnumModel:
2+
path: test/enum/
3+
type: enum
4+
values:
5+
MY_VALUE_1: my_value1
6+
MY_VALUE_2: my_value2
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// GENERATED CODE - DO NOT MODIFY BY HAND
2+
3+
import 'package:json_annotation/json_annotation.dart';
4+
5+
enum MyEnumModel {
6+
@JsonValue('my_value1')
7+
myValue1(
8+
jsonValue: 'my_value1',
9+
),
10+
@JsonValue('my_value2')
11+
myValue2(
12+
jsonValue: 'my_value2',
13+
);
14+
15+
final String jsonValue;
16+
17+
const MyEnumModel({
18+
required this.jsonValue,
19+
});
20+
}

0 commit comments

Comments
 (0)