From 59d6ee7eb79f976780637145667038d14903215a Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Fri, 11 Mar 2022 15:06:11 -0800 Subject: [PATCH] Sort operations directly into json map --- .../src/operation_persister/local_persister.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs b/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs index 18b3c9c9111e6..6121579de4862 100644 --- a/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs +++ b/compiler/crates/relay-compiler/src/operation_persister/local_persister.rs @@ -9,8 +9,8 @@ use async_trait::async_trait; use dashmap::DashMap; use persist_query::PersistError; use relay_config::LocalPersistConfig; +use serde_json::{Map, Value}; use sha1::{Digest, Sha1}; -use std::collections::BTreeMap; use crate::OperationPersister; @@ -54,13 +54,16 @@ impl OperationPersister for LocalPersister { } fn finalize(&self) -> Result<(), PersistError> { - let ordered: BTreeMap<_, _> = self - .query_map - .iter() - .map(|x| (x.key().clone(), x.value().clone())) - .collect(); + let mut vec = Vec::from_iter(self.query_map.iter()); + vec.sort_by(|a, b| a.key().partial_cmp(b.key()).unwrap()); - let content = serde_json::to_string_pretty(&ordered)?; + let mut json_map = Map::with_capacity(vec.len()); + + for entry in vec { + json_map.insert(entry.key().clone(), Value::String(entry.value().clone())); + } + + let content = serde_json::to_string_pretty(&json_map)?; std::fs::write(&self.config.file, content)?; Ok(())