Changeset View
Changeset View
Standalone View
Standalone View
services/feature-flags/src/database.rs
use crate::constants::{ | |||||
FEATURE_FLAGS_CONFIG_FIELD, FEATURE_FLAGS_FEATURE_FIELD, | |||||
FEATURE_FLAGS_NON_STAFF_FIELD, FEATURE_FLAGS_STAFF_FIELD, | |||||
}; | |||||
use aws_sdk_dynamodb::{model::AttributeValue, Error as DynamoDBError}; | use aws_sdk_dynamodb::{model::AttributeValue, Error as DynamoDBError}; | ||||
use std::collections::HashMap; | use std::collections::HashMap; | ||||
use std::fmt::{Display, Formatter}; | use std::fmt::{Display, Formatter}; | ||||
use std::num::ParseIntError; | use std::num::ParseIntError; | ||||
#[derive( | #[derive( | ||||
Debug, derive_more::Display, derive_more::From, derive_more::Error, | Debug, derive_more::Display, derive_more::From, derive_more::Error, | ||||
)] | )] | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | let result = attribute_value.parse::<i32>().map_err(|e| { | ||||
DBItemError::new( | DBItemError::new( | ||||
attribute_name, | attribute_name, | ||||
Value::String(attribute_value.to_string()), | Value::String(attribute_value.to_string()), | ||||
DBItemAttributeError::InvalidNumberFormat(e), | DBItemAttributeError::InvalidNumberFormat(e), | ||||
) | ) | ||||
})?; | })?; | ||||
Ok(result) | Ok(result) | ||||
} | } | ||||
#[derive(Debug)] | |||||
pub struct CodeVersionSpecificFeatureConfig { | |||||
pub staff: bool, | |||||
pub non_staff: bool, | |||||
} | |||||
fn _parse_code_version_specific_feature_config( | |||||
value: Option<AttributeValue>, | |||||
) -> Result<CodeVersionSpecificFeatureConfig, DBItemError> { | |||||
let mut code_version_config_map = | |||||
_parse_map_attribute(FEATURE_FLAGS_CONFIG_FIELD, value)?; | |||||
bartek: IMO the underscore renames for `_parse_xxx_attribute` should be done in this diff because… | |||||
bartekUnsubmitted Not Done Inline ActionsNever mind, I forgot that Rust displays warnings recursively for unused functions bartek: Never mind, I forgot that Rust displays warnings recursively for unused functions | |||||
tomekAuthorUnsubmitted Done Inline ActionsThat's correct - all the underscores were necessary. After introducing the shared lib, number of underscores was decreased, though. tomek: That's correct - all the underscores were necessary. After introducing the shared lib, number… | |||||
let staff = _parse_bool_attribute( | |||||
FEATURE_FLAGS_STAFF_FIELD, | |||||
code_version_config_map.remove(FEATURE_FLAGS_STAFF_FIELD), | |||||
)?; | |||||
let non_staff = _parse_bool_attribute( | |||||
FEATURE_FLAGS_NON_STAFF_FIELD, | |||||
code_version_config_map.remove(FEATURE_FLAGS_NON_STAFF_FIELD), | |||||
)?; | |||||
Ok(CodeVersionSpecificFeatureConfig { staff, non_staff }) | |||||
} | |||||
#[derive(Debug)] | |||||
pub struct FeatureConfig { | |||||
pub name: String, | |||||
pub config: HashMap<i32, CodeVersionSpecificFeatureConfig>, | |||||
} | |||||
fn _parse_feature_config( | |||||
mut attribute_value: HashMap<String, AttributeValue>, | |||||
) -> Result<FeatureConfig, DBItemError> { | |||||
let feature_name = _parse_string_attribute( | |||||
FEATURE_FLAGS_FEATURE_FIELD, | |||||
attribute_value.remove(FEATURE_FLAGS_FEATURE_FIELD), | |||||
)?; | |||||
let config_map = _parse_map_attribute( | |||||
FEATURE_FLAGS_CONFIG_FIELD, | |||||
attribute_value.remove(FEATURE_FLAGS_CONFIG_FIELD), | |||||
)?; | |||||
let mut config = HashMap::new(); | |||||
for (code_version_string, code_version_config) in config_map { | |||||
let code_version: i32 = | |||||
_parse_number("code_version", code_version_string.as_str())?; | |||||
let version_config = | |||||
_parse_code_version_specific_feature_config(Some(code_version_config))?; | |||||
config.insert(code_version, version_config); | |||||
} | |||||
Ok(FeatureConfig { | |||||
name: feature_name, | |||||
config, | |||||
}) | |||||
} |
IMO the underscore renames for _parse_xxx_attribute should be done in this diff because they're no longer unused. My motivation is to keep the number of underscores as minimal as possible for each diff.
Anyway I see they're renamed in the following diff and I'm OK with that too