Skip to content

Commit

Permalink
Make InexactObject(Vec<Prop>) and ExactObject(Vec<Prop>) objects with…
Browse files Browse the repository at this point in the history
… private constructors

Summary:
* Add some type safety - wrap Vec<Prop> in two newtypes, InexactObject and ExactObject
* These structs have a single private field. This allows us to force developers to use a constructor (InexactObject::new), which (in a followup diff) will sort the props. However, they implement Deref and DerefMut, so we conversion **out** is not a problem. (We never need to consume them, so From is not necessary.)

Thanks Maarten Staa!

Reviewed By: captbaritone

Differential Revision: D34468586

fbshipit-source-id: e39822d3289af25f6c658649bc2499f84aa0c7b3
  • Loading branch information
rbalicki2 authored and facebook-github-bot committed Mar 11, 2022
1 parent a844636 commit 8beb0cb
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 97 deletions.
46 changes: 23 additions & 23 deletions compiler/crates/relay-typegen/src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl FlowPrinter {

#[cfg(test)]
mod tests {
use crate::writer::KeyValuePairProp;
use crate::writer::{ExactObject, InexactObject, KeyValuePairProp};

use super::*;
use intern::string_key::Intern;
Expand Down Expand Up @@ -378,26 +378,26 @@ mod tests {
#[test]
fn exact_object() {
assert_eq!(
print_type(&AST::ExactObject(Vec::new())),
print_type(&AST::ExactObject(ExactObject::new(Vec::new()))),
r"{||}".to_string()
);

assert_eq!(
print_type(&AST::ExactObject(vec![Prop::KeyValuePair(
KeyValuePairProp {
print_type(&AST::ExactObject(ExactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "single".intern(),
optional: false,
read_only: false,
value: AST::String,
}
),])),
}),
]))),
r"{|
single: string,
|}"
.to_string()
);
assert_eq!(
print_type(&AST::ExactObject(vec![
print_type(&AST::ExactObject(ExactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "foo".intern(),
optional: true,
Expand All @@ -410,7 +410,7 @@ mod tests {
read_only: true,
value: AST::Number,
}),
])),
]))),
r"{|
foo?: string,
+bar: number,
Expand All @@ -422,12 +422,12 @@ mod tests {
#[test]
fn nested_object() {
assert_eq!(
print_type(&AST::ExactObject(vec![
print_type(&AST::ExactObject(ExactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "foo".intern(),
optional: true,
read_only: false,
value: AST::ExactObject(vec![
value: AST::ExactObject(ExactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "nested_foo".intern(),
optional: true,
Expand All @@ -440,15 +440,15 @@ mod tests {
read_only: true,
value: AST::Number,
}),
]),
])),
}),
Prop::KeyValuePair(KeyValuePairProp {
key: "bar".intern(),
optional: false,
read_only: true,
value: AST::Number,
}),
])),
]))),
r"{|
foo?: {|
nested_foo?: string,
Expand All @@ -463,22 +463,22 @@ mod tests {
#[test]
fn inexact_object() {
assert_eq!(
print_type(&AST::InexactObject(Vec::new())),
print_type(&AST::InexactObject(InexactObject::new(Vec::new()))),
r"{
...
}"
.to_string()
);

assert_eq!(
print_type(&AST::InexactObject(vec![Prop::KeyValuePair(
KeyValuePairProp {
print_type(&AST::InexactObject(InexactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "single".intern(),
optional: false,
read_only: false,
value: AST::String,
}
),])),
}),
]))),
r"{
single: string,
...
Expand All @@ -487,7 +487,7 @@ mod tests {
);

assert_eq!(
print_type(&AST::InexactObject(vec![
print_type(&AST::InexactObject(InexactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "foo".intern(),
optional: false,
Expand All @@ -500,7 +500,7 @@ mod tests {
read_only: true,
value: AST::Number,
})
])),
]))),
r"{
foo: string,
+bar?: number,
Expand All @@ -513,14 +513,14 @@ mod tests {
#[test]
fn other_comment() {
assert_eq!(
print_type(&AST::ExactObject(vec![Prop::KeyValuePair(
KeyValuePairProp {
print_type(&AST::ExactObject(ExactObject::new(vec![
Prop::KeyValuePair(KeyValuePairProp {
key: "with_comment".intern(),
optional: false,
read_only: false,
value: AST::OtherTypename,
}
),])),
}),
]))),
r#"{|
// This will never be '%other', but we need some
// value in case none of the concrete values match.
Expand Down
Loading

0 comments on commit 8beb0cb

Please sign in to comment.