Skip to content

Commit

Permalink
Use json_comments crate to skip config file comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ppca committed Jan 23, 2023
1 parent 5dbca4c commit 74e5c09
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ indicatif = { version = "0.15.0", features = ["with_rayon"] }
insta = { version = "1.26.0", features = ["json", "yaml"] }
itertools = "0.10.0"
itoa = "1.0"
json_comments = "0.2.1"
libc = "0.2.81"
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
log = "0.4"
Expand Down
1 change: 1 addition & 0 deletions core/chain-configs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ description = "This crate provides typed interfaces to the NEAR Genesis and Chai
anyhow.workspace = true
chrono.workspace = true
derive_more.workspace = true
json_comments.workspace = true
num-rational.workspace = true
once_cell.workspace = true
serde.workspace = true
Expand Down
5 changes: 4 additions & 1 deletion core/chain-configs/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use near_primitives::{
},
version::ProtocolVersion,
};
use json_comments::StripComments;

const MAX_GAS_PRICE: Balance = 10_000_000_000_000_000_000_000;

Expand Down Expand Up @@ -271,7 +272,9 @@ impl GenesisConfig {
pub fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
let file = File::open(path).with_context(|| "Could not open genesis config file.")?;
let reader = BufReader::new(file);
let genesis_config: GenesisConfig = serde_json::from_reader(reader)
// Strip the comments from the input (use `as_bytes()` to get a `Read`).
let stripped = StripComments::new(reader);
let genesis_config: GenesisConfig = serde_json::from_reader(stripped)
.with_context(|| "Failed to deserialize the genesis records.")?;
Ok(genesis_config)
}
Expand Down
1 change: 1 addition & 0 deletions nearcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ futures.workspace = true
hyper-tls.workspace = true
hyper.workspace = true
indicatif.workspace = true
json_comments.workspace = true
num-rational.workspace = true
once_cell.workspace = true
rand.workspace = true
Expand Down
5 changes: 4 additions & 1 deletion nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use near_primitives::test_utils::create_test_signer;
use near_primitives::time::Clock;
use num_rational::Rational32;
use serde::{Deserialize, Serialize};
use json_comments::StripComments;
#[cfg(test)]
use tempfile::tempdir;
use tracing::{info, warn};
Expand Down Expand Up @@ -391,8 +392,10 @@ impl Config {
let contents = std::fs::read_to_string(path)
.with_context(|| format!("Failed to read config from {}", path.display()))?;
let mut unrecognised_fields = Vec::new();
let mut contents_without_comments = String::new();
StripComments::new(contents.as_bytes()).read_to_string(&mut contents_without_comments).unwrap();
let config: Config = serde_ignored::deserialize(
&mut serde_json::Deserializer::from_str(&contents),
&mut serde_json::Deserializer::from_str(&contents_without_comments),
|field| {
let field = field.to_string();
// TODO(mina86): Remove this deprecation notice some time by the
Expand Down
18 changes: 12 additions & 6 deletions nearcore/src/dyn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use near_dyn_configs::{UpdateableConfigLoaderError, UpdateableConfigs};
use near_o11y::log_config::LogConfig;
use serde::Deserialize;
use std::path::{Path, PathBuf};
use json_comments::StripComments;

const LOG_CONFIG_FILENAME: &str = "log_config.json";

Expand Down Expand Up @@ -59,13 +60,18 @@ where
for<'a> T: Deserialize<'a>,
{
match std::fs::read_to_string(path) {
Ok(config_str) => match serde_json::from_str::<T>(&config_str) {
Ok(config) => {
tracing::info!(target: "neard", config=?config, "Changing the config {path:?}.");
return Ok(Some(config));
Ok(config_str) => {
// Strip the comments from the input (use `as_bytes()` to get a `Read`).
let stripped = StripComments::new(config_str.as_bytes());

match serde_json::from_reader(stripped) {
Ok(config) => {
tracing::info!(target: "neard", config=?config, "Changing the config {path:?}.");
return Ok(Some(config));
}
Err(err) => Err(UpdateableConfigLoaderError::Parse { file: path.to_path_buf(), err }),
}
Err(err) => Err(UpdateableConfigLoaderError::Parse { file: path.to_path_buf(), err }),
},
},
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => {
tracing::info!(target: "neard", ?err, "Reset the config {path:?} because the config file doesn't exist.");
Expand Down

0 comments on commit 74e5c09

Please sign in to comment.