From 4790a0dc58ce235b8efc65efe861b53364aa350b Mon Sep 17 00:00:00 2001 From: Jordy de Jonghe Date: Thu, 7 Sep 2023 15:42:59 +0200 Subject: [PATCH] #130: use jsonValue as default --- lib/writer/enum_model_writer.dart | 3 +- .../use_default_json_value/config.txt | 14 ++++++++++ .../use_default_json_value/output.txt | 28 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/writer/enum_model_writer/use_default_json_value/config.txt create mode 100644 test/writer/enum_model_writer/use_default_json_value/output.txt diff --git a/lib/writer/enum_model_writer.dart b/lib/writer/enum_model_writer.dart index d3455cf..77a43d9 100644 --- a/lib/writer/enum_model_writer.dart +++ b/lib/writer/enum_model_writer.dart @@ -57,7 +57,8 @@ class EnumModelWriter { var value = enumValue?.value ?? property.defaultValue; sb.write(' ${property.name}: '); - if (property.type is StringType && value != null) { + if (property.type is StringType && (value != null || property.isJsonvalue)) { + if (value == null && property.isJsonvalue) value = jsonValue; sb.writeln('\'$value\','); } else { sb.writeln('$value,'); diff --git a/test/writer/enum_model_writer/use_default_json_value/config.txt b/test/writer/enum_model_writer/use_default_json_value/config.txt new file mode 100644 index 0000000..9d2eee4 --- /dev/null +++ b/test/writer/enum_model_writer/use_default_json_value/config.txt @@ -0,0 +1,14 @@ +DoubleStatus: + path: status + type: enum + properties: + value: + type: String + is_json_value: true + values: + status_0: + properties: + value: customValue + status_1: + status_2: + status_3: diff --git a/test/writer/enum_model_writer/use_default_json_value/output.txt b/test/writer/enum_model_writer/use_default_json_value/output.txt new file mode 100644 index 0000000..77a6c9f --- /dev/null +++ b/test/writer/enum_model_writer/use_default_json_value/output.txt @@ -0,0 +1,28 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +import 'package:json_annotation/json_annotation.dart'; + +enum DoubleStatus { + @JsonValue('customValue') + STATUS_0( + value: 'customValue', + ), + @JsonValue('status_1') + STATUS_1( + value: 'status_1', + ), + @JsonValue('status_2') + STATUS_2( + value: 'status_2', + ), + @JsonValue('status_3') + STATUS_3( + value: 'status_3', + ); + + final String value; + + const DoubleStatus({ + required this.value, + }); +}