Skip to content

Commit df3ca40

Browse files
kstepanovdevarmyhaylenko
authored andcommitted
Replace the identical methods with parse_json (#428)
1 parent 769ebc4 commit df3ca40

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

nft_ingester/src/bin/slot_persister/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use interface::{
1010
use metrics_utils::{utils::start_metrics, MetricState, MetricsTrait};
1111
use nft_ingester::{
1212
backfiller::BackfillSource,
13-
config::{parse_json_to_big_table_config, BigTableConfig},
13+
config::{parse_json, BigTableConfig},
1414
inmemory_slots_dumper::InMemorySlotsDumper,
1515
};
1616
use rocks_db::{column::TypedColumn, SlotStorage};
@@ -55,7 +55,7 @@ struct Args {
5555
start_slot: Option<u64>,
5656

5757
/// Big table config (best passed from env)
58-
#[arg(short, long, env, value_parser = parse_json_to_big_table_config)]
58+
#[arg(short, long, env, value_parser = parse_json::<Result<BigTableConfig, String>>)]
5959
big_table_config: Option<BigTableConfig>,
6060

6161
/// Metrics port

nft_ingester/src/config.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct IngesterClapArgs {
4646
#[clap(long, env = "INGESTER_PG_MAX_QUERY_TIMEOUT_SECS", default_value = "120")]
4747
pub pg_max_query_statement_timeout_secs: u32,
4848

49-
#[clap(short('r'), long, env, help="example: {redis_connection_str=\"redis://127.0.0.1:6379/0\"}", value_parser = parse_json_to_dict)]
49+
#[clap(short('r'), long, env, help="example: {\"redis_connection_str\": \"redis://127.0.0.1:6379/0\"}", value_parser = parse_json::<Dict>)]
5050
pub redis_connection_config: Dict,
5151

5252
#[clap(long, env, default_value = "5")]
@@ -106,7 +106,7 @@ pub struct IngesterClapArgs {
106106
)]
107107
pub run_profiling: bool,
108108

109-
#[clap(long, env, value_parser = parse_json_to_json_middleware_config, help = "Example: {'is_enabled':true, 'max_urls_to_parse':10} ",)]
109+
#[clap(long, env, value_parser = parse_json::<JsonMiddlewareConfig>, help = "Example: {'is_enabled':true, 'max_urls_to_parse':10} ",)]
110110
pub json_middleware_config: Option<JsonMiddlewareConfig>,
111111

112112
// Group: Rocks DB Configuration
@@ -221,7 +221,7 @@ pub struct IngesterClapArgs {
221221
pub backfill_rpc_address: Option<String>,
222222
#[clap(long, env, default_value = "rpc", help = "#backfiller Backfill source mode.")]
223223
pub backfiller_source_mode: BackfillerSourceMode,
224-
#[clap(long, env, value_parser = parse_json_to_big_table_config, help ="#backfiller Big table config")]
224+
#[clap(long, env, value_parser = parse_json::<Result<BigTableConfig, String>>, help ="#backfiller Big table config")]
225225
pub big_table_config: Option<BigTableConfig>,
226226

227227
#[clap(
@@ -510,7 +510,7 @@ pub struct ApiClapArgs {
510510
#[clap(long, env, help = "#api Storage service base url")]
511511
pub storage_service_base_url: Option<String>,
512512

513-
#[clap(long, env, value_parser = parse_json_to_json_middleware_config, help = "Example: {'is_enabled':true, 'max_urls_to_parse':10} ",)]
513+
#[clap(long, env, value_parser = parse_json::<Result<JsonMiddlewareConfig, String>>, help = "Example: {'is_enabled':true, 'max_urls_to_parse':10} ",)]
514514
pub json_middleware_config: Option<JsonMiddlewareConfig>,
515515
#[clap(long, env, default_value = "100")]
516516
pub parallel_json_downloaders: i32,
@@ -525,19 +525,7 @@ pub struct ApiClapArgs {
525525
pub log_level: String,
526526
}
527527

528-
fn parse_json_to_dict(s: &str) -> Result<Dict, String> {
529-
parse_json(s)
530-
}
531-
532-
fn parse_json_to_json_middleware_config(s: &str) -> Result<JsonMiddlewareConfig, String> {
533-
parse_json(s)
534-
}
535-
536-
pub fn parse_json_to_big_table_config(s: &str) -> Result<BigTableConfig, String> {
537-
parse_json(s)
538-
}
539-
540-
fn parse_json<T: serde::de::DeserializeOwned>(s: &str) -> Result<T, String> {
528+
pub fn parse_json<T: serde::de::DeserializeOwned>(s: &str) -> Result<T, String> {
541529
serde_json::from_str(s).map_err(|e| format!("Failed to parse JSON: {}", e))
542530
}
543531

@@ -568,7 +556,7 @@ pub const DATABASE_URL_KEY: &str = "url";
568556
pub const MAX_POSTGRES_CONNECTIONS: &str = "max_postgres_connections";
569557

570558
#[derive(Deserialize, PartialEq, Debug, Clone)]
571-
pub struct BigTableConfig(Dict);
559+
pub struct BigTableConfig(serde_json::Value);
572560

573561
pub const BIG_TABLE_CREDS_KEY: &str = "creds";
574562
pub const BIG_TABLE_TIMEOUT_KEY: &str = "timeout";
@@ -586,7 +574,7 @@ impl BigTableConfig {
586574
}
587575

588576
pub fn get_big_table_timeout_key(&self) -> Result<u32, IngesterError> {
589-
Ok(self.0.get(BIG_TABLE_TIMEOUT_KEY).and_then(|v| v.to_u128()).ok_or(
577+
Ok(self.0.get(BIG_TABLE_TIMEOUT_KEY).and_then(|v| v.as_u64()).ok_or(
590578
IngesterError::ConfigurationError { msg: "BIG_TABLE_TIMEOUT_KEY missing".to_string() },
591579
)? as u32)
592580
}
@@ -612,7 +600,7 @@ mod tests {
612600
"--rpc-host",
613601
"https://mainnet-aura.metaplex.com",
614602
"--redis-connection-config",
615-
"{}",
603+
r#"{"redis_connection_str": "foo"}"#,
616604
]);
617605

618606
assert_eq!(args.rocks_db_path, "./my_rocksdb");

0 commit comments

Comments
 (0)