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

Add minimal documentation to in ir in trustfall_core #139

Merged
merged 6 commits into from
Feb 10, 2023
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
34 changes: 32 additions & 2 deletions trustfall_core/src/ir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Trustfall internal representation (IR)
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
#![allow(dead_code)]

pub mod indexed;
Expand Down Expand Up @@ -26,17 +27,21 @@ lazy_static! {
pub(crate) static ref TYPENAME_META_FIELD_ARC: Arc<str> = Arc::from(TYPENAME_META_FIELD);
}

/// Vertex ID
#[doc(alias("vertex", "node"))]
Copy link
Owner

Choose a reason for hiding this comment

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

Very nice! 💯

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Vid(pub(crate) NonZeroUsize); // vertex ID
pub struct Vid(pub(crate) NonZeroUsize);

impl Vid {
pub fn new(id: NonZeroUsize) -> Vid {
Vid(id)
}
}

/// Edge ID
#[doc(alias = "edge")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Eid(pub(crate) NonZeroUsize); // edge ID
pub struct Eid(pub(crate) NonZeroUsize);

impl Eid {
pub fn new(id: NonZeroUsize) -> Eid {
Expand All @@ -49,8 +54,12 @@ pub struct EdgeParameters(
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub BTreeMap<Arc<str>, FieldValue>,
);

/// IR of components of a query, containing information about the vertex ID
Copy link
Owner

Choose a reason for hiding this comment

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

Do you think it's worth trying to stick to always saying Vid throughout the docs, even linking to it as needed? It's a core concept and will keep coming up all over the place in the IR, so I'm worried that mixing between vertex ID and Vid may be more confusing than just sticking to the unambiguous term that the reader has to learn.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it might be worth linking it every time (so it's always easy to find the definition), but yes it should be the same. I'll replace them!

/// of the root of the query, as well as well as maps of all vertices, edges,
/// folds, and outputs of the query.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IRQueryComponent {
/// The [Vid] of the root, or entry point, of the query.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
pub root: Vid,

#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
Expand All @@ -66,6 +75,7 @@ pub struct IRQueryComponent {
pub outputs: BTreeMap<Arc<str>, ContextField>,
}

/// Intermediate representation of a query
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IRQuery {
pub root_name: Arc<str>,
Expand Down Expand Up @@ -94,6 +104,9 @@ pub struct IREdge {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parameters: Option<Arc<EdgeParameters>>,

/// Indicating if this edge is optional.
///
/// This would correspond to `@optional` in GraphQL.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
#[serde(default = "default_optional", skip_serializing_if = "is_false")]
pub optional: bool,

Expand Down Expand Up @@ -123,9 +136,13 @@ impl Recursive {
}
}

/// Representation of a vertex (node) in the Trustfall intermediate
/// representation (IR).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IRVertex {
pub vid: Vid,

/// The name of the type of the vertex as a string.
pub type_name: Arc<str>,

#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -282,6 +299,15 @@ impl Argument {
}
}

/// Operations that can be made in the graph.
///
/// In GraphQL, this can correspond to the `op` argument in `@filter`,
/// for example in the following:
/// ```graphql
/// query Student {
/// name @filter(op: "has_substring", values: ["John"])
/// }
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
/// ```
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Operation<LeftT, RightT>
Expand Down Expand Up @@ -365,6 +391,10 @@ where
}
}

/// The operation name as a `str`
///
/// Note that these are the same as would be given to a GraphQL `op`
/// argumetn.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn operation_name(&self) -> &'static str {
match self {
Operation::IsNull(..) => "is_null",
Expand Down
16 changes: 13 additions & 3 deletions trustfall_core/src/ir/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/// IR of the values of GraphQL fields.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
use async_graphql_value::{ConstValue, Number, Value};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Values of fields in GraphQL types.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
///
/// For version that is serialized as an untagged enum, see [TransparentValue].
Copy link
Owner

Choose a reason for hiding this comment

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

awesome!

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FieldValue {
// Order may matter here! Deserialization, if ever configured for untagged serialization,
Expand All @@ -10,17 +14,21 @@ pub enum FieldValue {
// This is because we want to prioritize the standard Integer GraphQL type over our custom u64,
// and prioritize exact integers over lossy floats.
Null,
Int64(i64), // AKA Integer
/// AKA integer
Int64(i64),
Uint64(u64),
Float64(f64), // AKA Float, and also not allowed to be NaN
/// AKA Float, and also not allowed to be NaN
Float64(f64),
String(String),
Boolean(bool),
DateTimeUtc(DateTime<Utc>),
Enum(String),
List(Vec<FieldValue>),
}

/// Same as FieldValue, but serialized as an untagged enum,
/// Values of fields in GraphQL types.
///
/// Same as [FieldValue], but serialized as an untagged enum,
/// which may be more suitable e.g. when serializing to JSON.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down Expand Up @@ -190,6 +198,7 @@ impl From<bool> for FieldValue {
}
}

/// Represents a finite (non-infinite, not-NaN) [f64] value
pub struct FiniteF64(f64);
impl From<FiniteF64> for FieldValue {
fn from(f: FiniteF64) -> FieldValue {
Expand Down Expand Up @@ -320,6 +329,7 @@ impl<T: Clone + Into<FieldValue>> From<&[T]> for FieldValue {
}
}

/// Converts a JSON number to a [FieldValue]
fn convert_number_to_field_value(n: &Number) -> Result<FieldValue, String> {
// The order here matters!
// Int64 must be before Uint64, which must be before Float64.
Expand Down