Skip to content

Commit 45faf4a

Browse files
authored
Merge pull request #430 from bolinfest/deterministic-derive
s/Hash/BTree/g in codegen to make the output deterministic
2 parents d147828 + 1dc6d20 commit 45faf4a

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

Diff for: graphql_client/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub mod reqwest;
3232

3333
use serde::{Deserialize, Serialize};
3434
use std::collections::HashMap;
35-
use std::fmt::{self, Display};
35+
use std::fmt::{self, Display, Write};
3636

3737
/// A convenience trait that can be used to build a GraphQL request body.
3838
///
@@ -215,7 +215,7 @@ impl Display for Error {
215215
fragments
216216
.iter()
217217
.fold(String::new(), |mut acc, item| {
218-
acc.push_str(&format!("{}/", item));
218+
let _ = write!(acc, "{}/", item);
219219
acc
220220
})
221221
.trim_end_matches('/')

Diff for: graphql_client_codegen/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod tests;
2929

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

32-
use std::{collections::HashMap, fmt::Display, io};
32+
use std::{collections::BTreeMap, fmt::Display, io};
3333

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

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

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

6262
let schema_extension = schema_path
6363
.extension()
@@ -69,8 +69,8 @@ pub fn generate_module_token_stream(
6969
let schema: schema::Schema = {
7070
let mut lock = SCHEMA_CACHE.lock().expect("schema cache is poisoned");
7171
match lock.entry(schema_path.to_path_buf()) {
72-
hash_map::Entry::Occupied(o) => o.get().clone(),
73-
hash_map::Entry::Vacant(v) => {
72+
btree_map::Entry::Occupied(o) => o.get().clone(),
73+
btree_map::Entry::Vacant(v) => {
7474
schema_string = read_file(v.key())?;
7575
let schema = match schema_extension {
7676
"graphql" | "gql" => {
@@ -93,8 +93,8 @@ pub fn generate_module_token_stream(
9393
let (query_string, query) = {
9494
let mut lock = QUERY_CACHE.lock().expect("query cache is poisoned");
9595
match lock.entry(query_path) {
96-
hash_map::Entry::Occupied(o) => o.get().clone(),
97-
hash_map::Entry::Vacant(v) => {
96+
btree_map::Entry::Occupied(o) => o.get().clone(),
97+
btree_map::Entry::Vacant(v) => {
9898
let query_string = read_file(v.key())?;
9999
let query = graphql_parser::parse_query(&query_string)
100100
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?

Diff for: graphql_client_codegen/src/query.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
},
2020
};
2121
use std::{
22-
collections::{HashMap, HashSet},
22+
collections::{BTreeMap, BTreeSet},
2323
fmt::Display,
2424
};
2525

@@ -42,7 +42,7 @@ impl QueryValidationError {
4242
}
4343
}
4444

45-
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
45+
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
4646
pub(crate) struct SelectionId(u32);
4747
#[derive(Debug, Clone, Copy, PartialEq)]
4848
pub(crate) struct OperationId(u32);
@@ -53,7 +53,7 @@ impl OperationId {
5353
}
5454
}
5555

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

5959
#[derive(Debug, Clone, Copy)]
@@ -509,7 +509,7 @@ where
509509
pub(crate) struct Query {
510510
fragments: Vec<ResolvedFragment>,
511511
operations: Vec<ResolvedOperation>,
512-
selection_parent_idx: HashMap<SelectionId, SelectionParent>,
512+
selection_parent_idx: BTreeMap<SelectionId, SelectionParent>,
513513
selections: Vec<Selection>,
514514
variables: Vec<ResolvedVariable>,
515515
}
@@ -620,8 +620,8 @@ impl ResolvedVariable {
620620

621621
#[derive(Debug, Default)]
622622
pub(crate) struct UsedTypes {
623-
pub(crate) types: HashSet<TypeId>,
624-
fragments: HashSet<ResolvedFragmentId>,
623+
pub(crate) types: BTreeSet<TypeId>,
624+
fragments: BTreeSet<ResolvedFragmentId>,
625625
}
626626

627627
impl UsedTypes {

Diff for: graphql_client_codegen/src/schema.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod tests;
66

77
use crate::query::UsedTypes;
88
use crate::type_qualifiers::GraphqlTypeQualifier;
9-
use std::collections::{HashMap, HashSet};
9+
use std::collections::{BTreeMap, BTreeSet};
1010

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

@@ -44,25 +44,25 @@ pub(crate) enum StoredFieldParent {
4444
Interface(InterfaceId),
4545
}
4646

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

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

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

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

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

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

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

6868
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -98,7 +98,7 @@ pub(crate) struct StoredScalar {
9898
pub(crate) name: String,
9999
}
100100

101-
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
101+
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, PartialOrd, Ord)]
102102
pub(crate) enum TypeId {
103103
Object(ObjectId),
104104
Scalar(ScalarId),
@@ -222,7 +222,7 @@ pub(crate) struct Schema {
222222
stored_scalars: Vec<StoredScalar>,
223223
stored_enums: Vec<StoredEnum>,
224224
stored_inputs: Vec<StoredInputType>,
225-
names: HashMap<String, TypeId>,
225+
names: BTreeMap<String, TypeId>,
226226

227227
pub(crate) query_type: Option<ObjectId>,
228228
pub(crate) mutation_type: Option<ObjectId>,
@@ -239,7 +239,7 @@ impl Schema {
239239
stored_scalars: Vec::with_capacity(DEFAULT_SCALARS.len()),
240240
stored_enums: Vec::new(),
241241
stored_inputs: Vec::new(),
242-
names: HashMap::new(),
242+
names: BTreeMap::new(),
243243
query_type: None,
244244
mutation_type: None,
245245
subscription_type: None,
@@ -404,7 +404,7 @@ impl StoredInputType {
404404
&'a self,
405405
input_id: InputId,
406406
schema: &'a Schema,
407-
visited_types: &mut HashSet<&'a str>,
407+
visited_types: &mut BTreeSet<&'a str>,
408408
) -> bool {
409409
visited_types.insert(&self.name);
410410
// The input type is recursive if any of its members contains it, without indirection
@@ -440,7 +440,7 @@ impl StoredInputType {
440440

441441
pub(crate) fn input_is_recursive_without_indirection(input_id: InputId, schema: &Schema) -> bool {
442442
let input = schema.get_input(input_id);
443-
let mut visited_types = HashSet::<&str>::new();
443+
let mut visited_types = BTreeSet::<&str>::new();
444444
input.contains_type_without_indirection(input_id, schema, &mut visited_types)
445445
}
446446
impl<'doc, T> std::convert::From<graphql_parser::schema::Document<'doc, T>> for Schema

Diff for: graphql_client_codegen/src/schema/json_conversion.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub(super) fn build_schema(src: IntrospectionResponse) -> Schema {
1414

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

1918
unions_mut(src)
2019
.map(|u| u.name.as_ref().expect("union name"))

0 commit comments

Comments
 (0)