aconfig: Cache flag values for rust codegen
Bug: b/307336730 Test: atest aconfig.test Change-Id: I01741a4205cbe4e9b007f43b043505bcbcf05cd8
This commit is contained in:
@@ -32,10 +32,12 @@ where
|
|||||||
{
|
{
|
||||||
let template_flags: Vec<TemplateParsedFlag> =
|
let template_flags: Vec<TemplateParsedFlag> =
|
||||||
parsed_flags_iter.map(|pf| TemplateParsedFlag::new(package, pf)).collect();
|
parsed_flags_iter.map(|pf| TemplateParsedFlag::new(package, pf)).collect();
|
||||||
|
let has_readwrite = template_flags.iter().any(|item| item.readwrite);
|
||||||
let context = TemplateContext {
|
let context = TemplateContext {
|
||||||
package: package.to_string(),
|
package: package.to_string(),
|
||||||
template_flags,
|
template_flags,
|
||||||
modules: package.split('.').map(|s| s.to_string()).collect::<Vec<_>>(),
|
modules: package.split('.').map(|s| s.to_string()).collect::<Vec<_>>(),
|
||||||
|
has_readwrite,
|
||||||
};
|
};
|
||||||
let mut template = TinyTemplate::new();
|
let mut template = TinyTemplate::new();
|
||||||
template.add_template(
|
template.add_template(
|
||||||
@@ -55,6 +57,7 @@ struct TemplateContext {
|
|||||||
pub package: String,
|
pub package: String,
|
||||||
pub template_flags: Vec<TemplateParsedFlag>,
|
pub template_flags: Vec<TemplateParsedFlag>,
|
||||||
pub modules: Vec<String>,
|
pub modules: Vec<String>,
|
||||||
|
pub has_readwrite: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -94,6 +97,20 @@ mod tests {
|
|||||||
/// flag provider
|
/// flag provider
|
||||||
pub struct FlagProvider;
|
pub struct FlagProvider;
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
/// flag value cache for disabled_rw
|
||||||
|
static ref CACHED_disabled_rw: bool = flags_rust::GetServerConfigurableFlag(
|
||||||
|
"aconfig_flags.aconfig_test",
|
||||||
|
"com.android.aconfig.test.disabled_rw",
|
||||||
|
"false") == "true";
|
||||||
|
|
||||||
|
/// flag value cache for enabled_rw
|
||||||
|
static ref CACHED_enabled_rw: bool = flags_rust::GetServerConfigurableFlag(
|
||||||
|
"aconfig_flags.aconfig_test",
|
||||||
|
"com.android.aconfig.test.enabled_rw",
|
||||||
|
"true") == "true";
|
||||||
|
}
|
||||||
|
|
||||||
impl FlagProvider {
|
impl FlagProvider {
|
||||||
/// query flag disabled_ro
|
/// query flag disabled_ro
|
||||||
pub fn disabled_ro(&self) -> bool {
|
pub fn disabled_ro(&self) -> bool {
|
||||||
@@ -102,10 +119,7 @@ impl FlagProvider {
|
|||||||
|
|
||||||
/// query flag disabled_rw
|
/// query flag disabled_rw
|
||||||
pub fn disabled_rw(&self) -> bool {
|
pub fn disabled_rw(&self) -> bool {
|
||||||
flags_rust::GetServerConfigurableFlag(
|
*CACHED_disabled_rw
|
||||||
"aconfig_flags.aconfig_test",
|
|
||||||
"com.android.aconfig.test.disabled_rw",
|
|
||||||
"false") == "true"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// query flag enabled_fixed_ro
|
/// query flag enabled_fixed_ro
|
||||||
@@ -120,10 +134,7 @@ impl FlagProvider {
|
|||||||
|
|
||||||
/// query flag enabled_rw
|
/// query flag enabled_rw
|
||||||
pub fn enabled_rw(&self) -> bool {
|
pub fn enabled_rw(&self) -> bool {
|
||||||
flags_rust::GetServerConfigurableFlag(
|
*CACHED_enabled_rw
|
||||||
"aconfig_flags.aconfig_test",
|
|
||||||
"com.android.aconfig.test.enabled_rw",
|
|
||||||
"true") == "true"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,16 +3,27 @@
|
|||||||
/// flag provider
|
/// flag provider
|
||||||
pub struct FlagProvider;
|
pub struct FlagProvider;
|
||||||
|
|
||||||
|
{{ if has_readwrite - }}
|
||||||
|
lazy_static::lazy_static! \{
|
||||||
|
{{ for flag in template_flags }}
|
||||||
|
{{ if flag.readwrite -}}
|
||||||
|
/// flag value cache for {flag.name}
|
||||||
|
static ref CACHED_{flag.name}: bool = flags_rust::GetServerConfigurableFlag(
|
||||||
|
"aconfig_flags.{flag.device_config_namespace}",
|
||||||
|
"{flag.device_config_flag}",
|
||||||
|
"{flag.default_value}") == "true";
|
||||||
|
{{ -endif }}
|
||||||
|
{{ endfor }}
|
||||||
|
}
|
||||||
|
{{ -endif }}
|
||||||
|
|
||||||
impl FlagProvider \{
|
impl FlagProvider \{
|
||||||
|
|
||||||
{{ for flag in template_flags }}
|
{{ for flag in template_flags }}
|
||||||
/// query flag {flag.name}
|
/// query flag {flag.name}
|
||||||
pub fn {flag.name}(&self) -> bool \{
|
pub fn {flag.name}(&self) -> bool \{
|
||||||
{{ if flag.readwrite -}}
|
{{ if flag.readwrite -}}
|
||||||
flags_rust::GetServerConfigurableFlag(
|
*CACHED_{flag.name}
|
||||||
"aconfig_flags.{flag.device_config_namespace}",
|
|
||||||
"{flag.device_config_flag}",
|
|
||||||
"{flag.default_value}") == "true"
|
|
||||||
{{ -else- }}
|
{{ -else- }}
|
||||||
{flag.default_value}
|
{flag.default_value}
|
||||||
{{ -endif }}
|
{{ -endif }}
|
||||||
|
Reference in New Issue
Block a user