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

Generate JSON schema for compiler config #4723

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions compiler/Cargo.lock

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

6 changes: 5 additions & 1 deletion compiler/crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/facebook/relay"
license = "MIT"

[dependencies]
schemars = { version = "0.8.21", features = ["indexmap2"] }
colored = "2.1.0"
indexmap = { version = "2.2.6", features = ["arbitrary", "rayon", "serde"] }
intern = { path = "../intern" }
Expand All @@ -17,5 +18,8 @@ lsp-types = "0.94.1"
md-5 = "0.10"
rayon = "1.9.0"
serde = { version = "1.0.185", features = ["derive", "rc"] }
serde_json = { version = "1.0.100", features = ["float_roundtrip", "unbounded_depth"] }
serde_json = { version = "1.0.100", features = [
"float_roundtrip",
"unbounded_depth",
] }
typetag = "0.2.15"
5 changes: 3 additions & 2 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use std::fmt::Result as FmtResult;
use indexmap::IndexSet;
use intern::string_key::StringKey;
use intern::Lookup;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use crate::Rollout;

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct FeatureFlags {
#[serde(default)]
Expand Down Expand Up @@ -133,7 +134,7 @@ fn default_as_true() -> bool {
true
}

#[derive(Debug, Deserialize, Clone, Serialize, Default)]
#[derive(Debug, Deserialize, Clone, Serialize, Default, JsonSchema)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum FeatureFlag {
/// Fully disabled: developers may not use this feature
Expand Down
7 changes: 5 additions & 2 deletions compiler/crates/common/src/named_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use intern::impl_lookup;
use intern::string_key::Intern;
use intern::string_key::StringKey;
use intern::Lookup;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -45,7 +46,8 @@ pub trait Named {
Ord,
PartialEq,
PartialOrd,
Serialize
Serialize,
JsonSchema
)]
pub struct DirectiveName(pub StringKey);

Expand Down Expand Up @@ -73,7 +75,8 @@ impl_lookup!(DirectiveName);
Ord,
PartialOrd,
Hash,
Serialize
Serialize,
JsonSchema
)]
pub struct ArgumentName(pub StringKey);

Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/common/src/rollout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

use md5::Digest;
use md5::Md5;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

/// A utility to enable gradual rollout of large codegen changes.
/// Can be constructed as the Default which passes or a percentage between 0 and
/// 100.
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, JsonSchema)]
pub struct Rollout(Option<u8>);

impl Rollout {
Expand Down
12 changes: 8 additions & 4 deletions compiler/crates/fixture-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,20 @@ pub async fn test_fixture<T, U, V>(
};

let actual = format!("{}\n", actual.trim_end());
let expected_file_path = workspace_root()
.join(source_file_path)
.with_file_name(expected_file_name);
assert_file_contains(&actual, expected_file_path, expected)
}

pub fn assert_file_contains(actual: &str, expected_file_path: PathBuf, expected: &str) {
if actual != expected {
{
let _guard = LOCK.lock();
print_diff::print_diff(expected, &actual);
}

if env::var_os("UPDATE_SNAPSHOTS").is_some() {
let expected_file_path = workspace_root()
.join(source_file_path)
.with_file_name(expected_file_name);
File::create(&expected_file_path)
.unwrap_or_else(|e| {
panic!(
Expand All @@ -183,7 +187,7 @@ pub async fn test_fixture<T, U, V>(
}
}

fn workspace_root() -> PathBuf {
pub fn workspace_root() -> PathBuf {
if let Ok(cargo) = std::env::var("CARGO") {
let stdout = Command::new(cargo)
.args(["locate-project", "--workspace", "--message-format=plain"])
Expand Down
6 changes: 5 additions & 1 deletion compiler/crates/intern/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/facebook/relay"
license = "MIT"

[dependencies]
schemars = { version = "0.8.21", features = ["indexmap2"] }
fnv = "1.0"
hashbrown = { version = "0.14.3", features = ["raw", "serde"] }
indexmap = { version = "2.2.6", features = ["arbitrary", "rayon", "serde"] }
Expand All @@ -23,4 +24,7 @@ smallvec = { version = "1.6.1", features = ["serde", "union"] }
[dev-dependencies]
bincode = "1.3.3"
rand = { version = "0.8", features = ["small_rng"] }
serde_json = { version = "1.0.100", features = ["float_roundtrip", "unbounded_depth"] }
serde_json = { version = "1.0.100", features = [
"float_roundtrip",
"unbounded_depth",
] }
21 changes: 21 additions & 0 deletions compiler/crates/intern/src/string_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use std::fmt::Formatter;
use std::str::FromStr;

use indexmap::IndexMap;
use schemars::gen::SchemaGenerator;
use schemars::schema::InstanceType;
use schemars::schema::Schema;
use schemars::schema::SchemaObject;
use schemars::schema::SingleOrVec;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
Expand All @@ -29,6 +35,21 @@ pub use crate::Lookup;
#[repr(transparent)]
pub struct StringKey(StringId);

impl JsonSchema for StringKey {
fn schema_name() -> std::string::String {
String::from("StringKey")
}

fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
format: None,
..Default::default()
}
.into()
}
}

pub type StringKeyMap<V> = HashMap<StringKey, V, BuildIdHasher<u32>>;
pub type StringKeySet = HashSet<StringKey, BuildIdHasher<u32>>;
pub type StringKeyIndexMap<V> = IndexMap<StringKey, V, BuildIdHasher<u32>>;
Expand Down
9 changes: 8 additions & 1 deletion compiler/crates/relay-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ path = "tests/compile_relay_artifacts_with_custom_id_test.rs"
[[test]]
name = "relay_compiler_relay_compiler_integration_test"
path = "tests/relay_compiler_integration_test.rs"
[[test]]
name = "relay_config_schema_json_test"
path = "tests/relay_config_schema_json_test.rs"

[dependencies]
schemars = { version = "0.8.21", features = ["indexmap2"] }
async-trait = "0.1.71"
bincode = "1.3.3"
common = { path = "../common" }
Expand Down Expand Up @@ -63,7 +67,10 @@ schema-diff = { path = "../schema-diff" }
schema-validate-lib = { path = "../schema-validate" }
serde = { version = "1.0.185", features = ["derive", "rc"] }
serde_bser = "0.3"
serde_json = { version = "1.0.100", features = ["float_roundtrip", "unbounded_depth"] }
serde_json = { version = "1.0.100", features = [
"float_roundtrip",
"unbounded_depth",
] }
sha1 = "0.10.5"
sha2 = "0.10.6"
signedsource = { path = "../signedsource" }
Expand Down
5 changes: 3 additions & 2 deletions compiler/crates/relay-compiler/src/compiler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use schema::SDLSchema;
use schema_diff::check::SchemaChangeSafety;
use schema_diff::definitions::SchemaChange;
use schema_diff::detect_changes;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use zstd::stream::read::Decoder as ZstdDecoder;
Expand All @@ -58,7 +59,7 @@ use crate::file_source::LocatedJavascriptSourceFeatures;
use crate::file_source::SourceControlUpdateStatus;

/// Set of project names.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, JsonSchema)]
#[serde(from = "DeserializableProjectSet")]
pub struct ProjectSet(Vec<ProjectName>);

Expand Down Expand Up @@ -119,7 +120,7 @@ impl fmt::Display for ProjectSet {
// want our actual `ProjectSet` object to be modeled as a single Vec internally.
// So, we provide this enum to use sede's polymorphic deserialization and then
// tell `ProjectSet` to deserialize via this enum using `From`.
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, JsonSchema)]
#[serde(untagged)]
pub enum DeserializableProjectSet {
ProjectName(ProjectName),
Expand Down
Loading
Loading