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

Modifies DataModel and Language implementations for code generator #81

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions src/bin/ion/commands/beta/generate/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,43 @@ use std::fmt::{Display, Formatter};

/// Represents a context that will be used for code generation
pub struct CodeGenContext {
// Initially the data_model field is set to None.
// Once an ISL type definition is mapped to a data model this will have Some value.
pub(crate) data_model: Option<DataModel>,
// Initially the abstract_data_type field is set to None.
// Once an ISL type definition is mapped to an abstract data type this will have Some value.
pub(crate) abstract_data_type: Option<AbstractDataType>,
}

impl CodeGenContext {
pub fn new() -> Self {
Self { data_model: None }
Self {
abstract_data_type: None,
}
}

pub fn with_data_model(&mut self, data_model: DataModel) {
self.data_model = Some(data_model);
pub fn with_abstract_data_type(&mut self, abstract_data_type: AbstractDataType) {
self.abstract_data_type = Some(abstract_data_type);
}
}

/// Represents a data model type that can be used to determine which templates can be used for code generation.
/// Represents an abstract data type type that can be used to determine which templates can be used for code generation.
desaikd marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum DataModel {
Value, // a struct with a scalar value (used for `type` constraint)
// TODO: Make Sequence parameterized over data type.
// add a data type for sequence here that can be used to read elements for that data type.
Sequence, // a struct with a sequence/collection value (used for `element` constraint)
pub enum AbstractDataType {
Copy link
Contributor Author

@desaikd desaikd Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(🗺️ PR tour) Renames DataModel to AbstractDataType. Also modifies Sequence enum variant to store information about sequence data type. (#69)

// a struct with a scalar value (used for `type` constraint)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments and the Display impl below include a lot of ISL-specific details. The ADT is the middle ground between ISL and a target language, so I think ideally it wouldn't know about or mention either.

pub enum AbstractDataType {
  // A scalar value (e.g. a string or integer)
  Value,
  // A series of zero or more values whose type is described by the nested `String` (e.g. a list)
  Sequence(String)
  // A collection of field name/value pairs (e.g. a map)
  Struct
}

impl Display for AbstractDataType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                AbstractDataType::Value => "scalar value",
                AbstractDataType::Sequence(_) => "sequence",
                AbstractDataType::Struct => "struct",
            }
        )
    }
}

Which ADT variant would be used for a user-defined data type that looked like this?

struct Point2D {
  x: f64,
  y: f64,
}

If it's Value, then we shouldn't describe it as "scalar". If it's Struct, then it would be good to mention "user defined types" as a use case for Struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. This specific example would be struct but if we had a type definition like below:

type::{
  name: "id", 
  type: int
}

with following POJO representation:

struct Id {
    value: i64,
}

Then this would be Value. All the structs with a single field that represents the data type(e.g. integer) for that type definition(e.g. Id) will be under Value.

Value,
// a struct with a sequence/collection value (used for `element` constraint)
// the parameter string represents the data type of the sequence
Sequence(String),
Struct,
}

impl Display for DataModel {
impl Display for AbstractDataType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
DataModel::Value => "single value struct",
DataModel::Sequence => "sequence value struct",
DataModel::Struct => "struct",
AbstractDataType::Value => "single value struct",
AbstractDataType::Sequence(_) => "sequence value struct",
AbstractDataType::Struct => "struct",
}
)
}
Expand Down
Loading
Loading