Skip to content

s/Hash/BTree/g in codegen to make the output deterministic #430

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

Merged
merged 2 commits into from
Aug 12, 2022
Merged
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
4 changes: 2 additions & 2 deletions graphql_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod reqwest;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Display};
use std::fmt::{self, Display, Write};

/// A convenience trait that can be used to build a GraphQL request body.
///
Expand Down Expand Up @@ -215,7 +215,7 @@ impl Display for Error {
fragments
.iter()
.fold(String::new(), |mut acc, item| {
acc.push_str(&format!("{}/", item));
let _ = write!(acc, "{}/", item);
acc
})
.trim_end_matches('/')
Expand Down
14 changes: 7 additions & 7 deletions graphql_client_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod tests;

pub use crate::codegen_options::{CodegenMode, GraphQLClientCodegenOptions};

use std::{collections::HashMap, fmt::Display, io};
use std::{collections::BTreeMap, fmt::Display, io};

#[derive(Debug)]
struct GeneralError(String);
Expand All @@ -43,7 +43,7 @@ impl Display for GeneralError {
impl std::error::Error for GeneralError {}

type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
type CacheMap<T> = std::sync::Mutex<HashMap<std::path::PathBuf, T>>;
type CacheMap<T> = std::sync::Mutex<BTreeMap<std::path::PathBuf, T>>;

lazy_static! {
static ref SCHEMA_CACHE: CacheMap<schema::Schema> = CacheMap::default();
Expand All @@ -57,7 +57,7 @@ pub fn generate_module_token_stream(
schema_path: &std::path::Path,
options: GraphQLClientCodegenOptions,
) -> Result<TokenStream, BoxError> {
use std::collections::hash_map;
use std::collections::btree_map;

let schema_extension = schema_path
.extension()
Expand All @@ -69,8 +69,8 @@ pub fn generate_module_token_stream(
let schema: schema::Schema = {
let mut lock = SCHEMA_CACHE.lock().expect("schema cache is poisoned");
match lock.entry(schema_path.to_path_buf()) {
hash_map::Entry::Occupied(o) => o.get().clone(),
hash_map::Entry::Vacant(v) => {
btree_map::Entry::Occupied(o) => o.get().clone(),
btree_map::Entry::Vacant(v) => {
schema_string = read_file(v.key())?;
let schema = match schema_extension {
"graphql" | "gql" => {
Expand All @@ -93,8 +93,8 @@ pub fn generate_module_token_stream(
let (query_string, query) = {
let mut lock = QUERY_CACHE.lock().expect("query cache is poisoned");
match lock.entry(query_path) {
hash_map::Entry::Occupied(o) => o.get().clone(),
hash_map::Entry::Vacant(v) => {
btree_map::Entry::Occupied(o) => o.get().clone(),
btree_map::Entry::Vacant(v) => {
let query_string = read_file(v.key())?;
let query = graphql_parser::parse_query(&query_string)
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?
Expand Down
12 changes: 6 additions & 6 deletions graphql_client_codegen/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
},
};
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, BTreeSet},
fmt::Display,
};

Expand All @@ -42,7 +42,7 @@ impl QueryValidationError {
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct SelectionId(u32);
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct OperationId(u32);
Expand All @@ -53,7 +53,7 @@ impl OperationId {
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ResolvedFragmentId(u32);

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -509,7 +509,7 @@ where
pub(crate) struct Query {
fragments: Vec<ResolvedFragment>,
operations: Vec<ResolvedOperation>,
selection_parent_idx: HashMap<SelectionId, SelectionParent>,
selection_parent_idx: BTreeMap<SelectionId, SelectionParent>,
selections: Vec<Selection>,
variables: Vec<ResolvedVariable>,
}
Expand Down Expand Up @@ -620,8 +620,8 @@ impl ResolvedVariable {

#[derive(Debug, Default)]
pub(crate) struct UsedTypes {
pub(crate) types: HashSet<TypeId>,
fragments: HashSet<ResolvedFragmentId>,
pub(crate) types: BTreeSet<TypeId>,
fragments: BTreeSet<ResolvedFragmentId>,
}

impl UsedTypes {
Expand Down
24 changes: 12 additions & 12 deletions graphql_client_codegen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests;

use crate::query::UsedTypes;
use crate::type_qualifiers::GraphqlTypeQualifier;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet};

pub(crate) const DEFAULT_SCALARS: &[&str] = &["ID", "String", "Int", "Float", "Boolean"];

Expand Down Expand Up @@ -44,25 +44,25 @@ pub(crate) enum StoredFieldParent {
Interface(InterfaceId),
}

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) struct ObjectId(u32);

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
pub(crate) struct ObjectFieldId(usize);

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) struct InterfaceId(usize);

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) struct ScalarId(usize);

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) struct UnionId(usize);

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) struct EnumId(usize);

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) struct InputId(u32);

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -98,7 +98,7 @@ pub(crate) struct StoredScalar {
pub(crate) name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub(crate) enum TypeId {
Object(ObjectId),
Scalar(ScalarId),
Expand Down Expand Up @@ -222,7 +222,7 @@ pub(crate) struct Schema {
stored_scalars: Vec<StoredScalar>,
stored_enums: Vec<StoredEnum>,
stored_inputs: Vec<StoredInputType>,
names: HashMap<String, TypeId>,
names: BTreeMap<String, TypeId>,

pub(crate) query_type: Option<ObjectId>,
pub(crate) mutation_type: Option<ObjectId>,
Expand All @@ -239,7 +239,7 @@ impl Schema {
stored_scalars: Vec::with_capacity(DEFAULT_SCALARS.len()),
stored_enums: Vec::new(),
stored_inputs: Vec::new(),
names: HashMap::new(),
names: BTreeMap::new(),
query_type: None,
mutation_type: None,
subscription_type: None,
Expand Down Expand Up @@ -404,7 +404,7 @@ impl StoredInputType {
&'a self,
input_id: InputId,
schema: &'a Schema,
visited_types: &mut HashSet<&'a str>,
visited_types: &mut BTreeSet<&'a str>,
) -> bool {
visited_types.insert(&self.name);
// The input type is recursive if any of its members contains it, without indirection
Expand Down Expand Up @@ -440,7 +440,7 @@ impl StoredInputType {

pub(crate) fn input_is_recursive_without_indirection(input_id: InputId, schema: &Schema) -> bool {
let input = schema.get_input(input_id);
let mut visited_types = HashSet::<&str>::new();
let mut visited_types = BTreeSet::<&str>::new();
input.contains_type_without_indirection(input_id, schema, &mut visited_types)
}
impl<'doc, T> std::convert::From<graphql_parser::schema::Document<'doc, T>> for Schema
Expand Down
1 change: 0 additions & 1 deletion graphql_client_codegen/src/schema/json_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub(super) fn build_schema(src: IntrospectionResponse) -> Schema {

fn build_names_map(src: &mut JsonSchema, schema: &mut Schema) {
let names = &mut schema.names;
names.reserve(types_mut(src).count());

unions_mut(src)
.map(|u| u.name.as_ref().expect("union name"))
Expand Down