-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for enums in Dart. (#6516)
* Added support for enums in Dart. * Pick non-private names for enum values. The _ prefix denotes a private member in Dart, so avoid generating enum values starting with this character. * Properly encode enum values into query paramters. * Various cleanups. * Add support for x-enum-values extension. Use class instead of enum for better ergonomy. Better generated enum names. * Fixed test. * Support enum descriptions.
- Loading branch information
1 parent
8b70f24
commit 1050aa9
Showing
20 changed files
with
234 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
modules/swagger-codegen/src/main/resources/dart/class.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@Entity() | ||
class {{classname}} { | ||
{{#vars}}{{#description}}/* {{{description}}} */{{/description}} | ||
@Property(name: '{{baseName}}') | ||
{{{datatype}}} {{name}} = {{{defaultValue}}}; | ||
{{#allowableValues}}{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{{/allowableValues}} | ||
{{/vars}} | ||
{{classname}}(); | ||
|
||
@override | ||
String toString() { | ||
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]'; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
modules/swagger-codegen/src/main/resources/dart/enum.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@Entity() | ||
class {{classname}} { | ||
/// The underlying value of this enum member. | ||
final {{dataType}} value; | ||
|
||
const {{classname}}._internal(this.value); | ||
|
||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{#description}} | ||
/// {{description}} | ||
{{/description}} | ||
static const {{classname}} {{name}} = const {{classname}}._internal({{value}}); | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
} | ||
|
||
class {{classname}}TypeTransformer extends TypeTransformer<{{classname}}> { | ||
@override | ||
dynamic encode({{classname}} data) { | ||
return data.value; | ||
} | ||
|
||
@override | ||
{{classname}} decode(dynamic data) { | ||
switch (data) { | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
case {{value}}: return {{classname}}.{{name}}; | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
default: throw('Unknown enum value to decode: $data'); | ||
} | ||
} | ||
} |
22 changes: 5 additions & 17 deletions
22
modules/swagger-codegen/src/main/resources/dart/model.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,7 @@ | ||
part of {{pubName}}.api; | ||
|
||
{{#models}}{{#model}} | ||
@Entity() | ||
class {{classname}} { | ||
{{#vars}}{{#description}}/* {{{description}}} */{{/description}} | ||
@Property(name: '{{baseName}}') | ||
{{{datatype}}} {{name}} = {{{defaultValue}}}; | ||
{{#allowableValues}}{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{{/allowableValues}} | ||
{{/vars}} | ||
{{classname}}(); | ||
|
||
@override | ||
String toString() { | ||
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]'; | ||
} | ||
|
||
} | ||
{{/model}}{{/models}} | ||
{{#models}} | ||
{{#model}} | ||
{{#isEnum}}{{>enum}}{{/isEnum}}{{^isEnum}}{{>class}}{{/isEnum}} | ||
{{/model}} | ||
{{/models}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.