-
Notifications
You must be signed in to change notification settings - Fork 33
Add ability to seal enum classes in Dart #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jerel
wants to merge
2
commits into
zefchain:main
Choose a base branch
from
jerel:dart-sealed-classes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 +1,7 @@ | ||
| // Copyright (c) Facebook, Inc. and its affiliates | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
|
||
| use std::collections::{BTreeMap, BTreeSet}; | ||
| use std::collections::{BTreeMap, BTreeSet, HashMap}; | ||
|
|
||
| /// Code generation options meant to be supported by all languages. | ||
| #[derive(Clone, Debug)] | ||
|
|
@@ -12,7 +12,7 @@ pub struct CodeGeneratorConfig { | |
| pub external_definitions: ExternalDefinitions, | ||
| pub comments: DocComments, | ||
| pub custom_code: CustomCode, | ||
| pub c_style_enums: bool, | ||
| pub enums: EnumConfig, | ||
| pub package_manifest: bool, | ||
| } | ||
|
|
||
|
|
@@ -36,6 +36,18 @@ pub type CustomCode = std::collections::BTreeMap< | |
| /* custom code */ String, | ||
| >; | ||
|
|
||
| /// Configure the generation style of enums. | ||
| #[derive(Clone, Debug)] | ||
| pub struct EnumConfig { | ||
| // Generate [enum] if `true` or classes if `false` | ||
| pub c_style: bool, | ||
| // Generate sealed class if `true` or abstract class if `false` | ||
| pub sealed: bool, | ||
| // If `sealed_enums` is true then the listed names will be abstract, | ||
| // if `sealed_enums` is false then the listed names will be sealed. | ||
| pub output_type: HashMap<&'static str, &'static str>, | ||
| } | ||
|
|
||
| /// How to copy generated source code and available runtimes for a given language. | ||
| pub trait SourceInstaller { | ||
| type Error; | ||
|
|
@@ -67,7 +79,11 @@ impl CodeGeneratorConfig { | |
| external_definitions: BTreeMap::new(), | ||
| comments: BTreeMap::new(), | ||
| custom_code: BTreeMap::new(), | ||
| c_style_enums: false, | ||
| enums: EnumConfig { | ||
| c_style: false, | ||
| sealed: false, | ||
| output_type: HashMap::new(), | ||
| }, | ||
| package_manifest: true, | ||
| } | ||
| } | ||
|
|
@@ -116,7 +132,23 @@ impl CodeGeneratorConfig { | |
| /// Generate C-style enums (without variant data) as the target language | ||
| /// native enum type in supported languages. | ||
| pub fn with_c_style_enums(mut self, c_style_enums: bool) -> Self { | ||
| self.c_style_enums = c_style_enums; | ||
| self.enums.c_style = c_style_enums; | ||
| self | ||
| } | ||
|
|
||
| /// For complex enums generate sealed enum classes instead of abstract classes | ||
| pub fn with_sealed_enums(mut self, sealed: bool) -> Self { | ||
| self.enums.sealed = sealed; | ||
| self | ||
| } | ||
|
|
||
| /// Generate abstract or sealed classes for data enums based on `with_sealed_enums` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: typo: extra space "enums based" |
||
| /// but allow item by item overrides. | ||
| pub fn with_enum_type_overrides( | ||
| mut self, | ||
| overrides: HashMap<&'static str, &'static str>, | ||
| ) -> Self { | ||
| self.enums.output_type = overrides; | ||
| self | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -192,7 +192,7 @@ where | |
| // in Dart enums cannot have a static method added to them | ||
| // yet so we must call the extension class instead | ||
| fn get_class(&self, name: &str) -> String { | ||
| if self.generator.config.c_style_enums { | ||
| if self.generator.config.enums.c_style { | ||
| use ContainerFormat::Enum; | ||
| match self.get_field_container_type(name) { | ||
| // if we have an enum AND all of that enum's members are Unit | ||
|
|
@@ -583,12 +583,19 @@ return obj; | |
| Struct(fields) => fields.clone(), | ||
| Enum(variants) => { | ||
| // When we find an enum with all Unit variants, we ser/de as a regular Dart enum. | ||
| if self.generator.config.c_style_enums | ||
| if ((self.generator.config.enums.c_style | ||
| && !self.generator.config.enums.output_type.contains_key(name)) | ||
| || self.generator.config.enums.output_type.get(name) == Some(&"enum")) | ||
| && variants.values().all(|f| f.value == VariantFormat::Unit) | ||
| { | ||
| self.output_enum_container(name, variants)?; | ||
| } else if (self.generator.config.enums.sealed | ||
| && !self.generator.config.enums.output_type.contains_key(name)) | ||
| || self.generator.config.enums.output_type.get(name) == Some(&"sealed") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we're not using |
||
| { | ||
| self.output_enum_class_container(name, variants, "sealed")?; | ||
| } else { | ||
| self.output_enum_class_container(name, variants)?; | ||
| self.output_enum_class_container(name, variants, "abstract")?; | ||
| } | ||
| return Ok(()); | ||
| } | ||
|
|
@@ -1052,12 +1059,14 @@ switch (this) {{"#, | |
| &mut self, | ||
| name: &str, | ||
| variants: &BTreeMap<u32, Named<VariantFormat>>, | ||
| class_type: &str, | ||
| ) -> Result<()> { | ||
| writeln!(self.out)?; | ||
| self.output_comment(name)?; | ||
| writeln!( | ||
| self.out, | ||
| "abstract class {} {{", | ||
| "{} class {} {{", | ||
| class_type, | ||
| self.quote_qualified_name(name) | ||
| )?; | ||
| self.enter_class(name); | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean "If
sealedis true" etc?