Skip to content
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

Adds support for Java enum generation #158

Merged
merged 10 commits into from
Oct 24, 2024
20 changes: 16 additions & 4 deletions src/bin/ion/commands/generate/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ impl<'a, L: Language + 'static> CodeGenerator<'a, L> {
// * The sequence type for `Sequence` will be stored based on `type` constraint with either `list` or `sexp`.
// * If given list of constraints has any `type` constraint except `type: list`, `type: struct` and `type: sexp`, then `AbstractDataType::Scalar` needs to be constructed.
// * The `base_type` for `Scalar` will be stored based on `type` constraint.
// * If given list of constraints has any `valida_values` constraint with symbol values, then `AbstractDataType::Enum` needs to be constructed.
desaikd marked this conversation as resolved.
Show resolved Hide resolved
// * All the other constraints except the above ones are not yet supported by code generator.
let abstract_data_type = if constraints
.iter()
Expand All @@ -460,10 +461,7 @@ impl<'a, L: Language + 'static> CodeGenerator<'a, L> {
isl_type,
)?
}
} else if constraints
.iter()
.any(|it| matches!(it.constraint(), IslConstraintValue::ValidValues(_)))
{
} else if Self::contains_enum_constraints(constraints) {
self.build_enum_from_constraints(constraints, code_gen_context, isl_type)?
} else if Self::contains_scalar_constraints(constraints) {
if is_nested_type {
Expand Down Expand Up @@ -510,6 +508,20 @@ impl<'a, L: Language + 'static> CodeGenerator<'a, L> {
&& isl_type_ref.name().as_str() != "struct"))
}

/// Verifies if the given constraints contain a `valid_values` constraint with only symbol values.
fn contains_enum_constraints(constraints: &[IslConstraint]) -> bool {
constraints.iter().any(|it| {
if let IslConstraintValue::ValidValues(valid_values) = it.constraint() {
valid_values
.values()
.iter()
.all(|val| matches!(val, ValidValue::Element(Value::Symbol(_))))
} else {
false
}
})
}

fn render_generated_code(
&mut self,
type_name: &str,
Expand Down
1 change: 0 additions & 1 deletion src/bin/ion/commands/generate/templates/java/enum.templ
desaikd marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public {% if is_nested == true %} static {% endif %} enum {{ model.name }} {
* The caller is responsible for closing the stream associated with the writer.
*/
public void writeTo(IonWriter writer) throws IOException {
{# Writes the enum variant as a string or symbol #}
writer.writeSymbol(this.textValue);
}

Expand Down
Loading