aconfig: use proto struct directly
Remove the hand-crafted wrappers around the structures auto-generated from protos/aconfig.proto, and use the auto-generated structs directly intead. This gets rid of a lot of manual repetition, and its inherent risk. Also unify how individual fields read from text proto are verified (e.g. is the flag.name field a valid identifier). Also change the intermediate cache format from JSON to binary protobuf. The concept of a 'cache' as an intermediate internal format to represent parsed input stays. The command line interface still refers to caches. At the moment a cache file is identical to a parsed_file protbuf, and the code exploits this internally. A couple of points regarding the auto-generated structs: - Vectors are named in the singular (e.g. parsed_flags.parsed_flag is a Vec<ProtoParsedFlag>) because this improves ergonomics for all devs working with aconfig input files - The auto-generated structs have fields that are of type Option<T> and convenience methods (named the same as the fields) to access T Test: atest aconfig.test aconfig.test.java Bug: 283910447 Change-Id: I512820cc4bc6c543dea9f6a4356f863120a10be3
This commit is contained in:
@@ -19,20 +19,18 @@ use serde::Serialize;
|
||||
use std::path::PathBuf;
|
||||
use tinytemplate::TinyTemplate;
|
||||
|
||||
use crate::aconfig::{FlagState, Permission};
|
||||
use crate::cache::{Cache, Item};
|
||||
use crate::codegen;
|
||||
use crate::commands::OutputFile;
|
||||
use crate::protos::{ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag};
|
||||
|
||||
pub fn generate_java_code(cache: &Cache) -> Result<Vec<OutputFile>> {
|
||||
let package = cache.package();
|
||||
pub fn generate_java_code<'a, I>(package: &str, parsed_flags_iter: I) -> Result<Vec<OutputFile>>
|
||||
where
|
||||
I: Iterator<Item = &'a ProtoParsedFlag>,
|
||||
{
|
||||
let class_elements: Vec<ClassElement> =
|
||||
cache.iter().map(|item| create_class_element(package, item)).collect();
|
||||
let is_read_write = class_elements.iter().any(|item| item.is_read_write);
|
||||
parsed_flags_iter.map(|pf| create_class_element(package, pf)).collect();
|
||||
let is_read_write = class_elements.iter().any(|elem| elem.is_read_write);
|
||||
let context = Context { package_name: package.to_string(), is_read_write, class_elements };
|
||||
|
||||
let java_files = vec!["Flags.java", "FeatureFlagsImpl.java", "FeatureFlags.java"];
|
||||
|
||||
let mut template = TinyTemplate::new();
|
||||
template.add_template("Flags.java", include_str!("../templates/Flags.java.template"))?;
|
||||
template.add_template(
|
||||
@@ -45,7 +43,7 @@ pub fn generate_java_code(cache: &Cache) -> Result<Vec<OutputFile>> {
|
||||
)?;
|
||||
|
||||
let path: PathBuf = package.split('.').collect();
|
||||
java_files
|
||||
["Flags.java", "FeatureFlagsImpl.java", "FeatureFlags.java"]
|
||||
.iter()
|
||||
.map(|file| {
|
||||
Ok(OutputFile {
|
||||
@@ -68,25 +66,23 @@ struct ClassElement {
|
||||
pub default_value: String,
|
||||
pub device_config_namespace: String,
|
||||
pub device_config_flag: String,
|
||||
pub flag_name_constant_suffix: String,
|
||||
pub is_read_write: bool,
|
||||
pub method_name: String,
|
||||
}
|
||||
|
||||
fn create_class_element(package: &str, item: &Item) -> ClassElement {
|
||||
let device_config_flag = codegen::create_device_config_ident(package, &item.name)
|
||||
.expect("values checked at cache creation time");
|
||||
fn create_class_element(package: &str, pf: &ProtoParsedFlag) -> ClassElement {
|
||||
let device_config_flag = codegen::create_device_config_ident(package, pf.name())
|
||||
.expect("values checked at flag parse time");
|
||||
ClassElement {
|
||||
default_value: if item.state == FlagState::Enabled {
|
||||
default_value: if pf.state() == ProtoFlagState::ENABLED {
|
||||
"true".to_string()
|
||||
} else {
|
||||
"false".to_string()
|
||||
},
|
||||
device_config_namespace: item.namespace.clone(),
|
||||
device_config_namespace: pf.namespace().to_string(),
|
||||
device_config_flag,
|
||||
flag_name_constant_suffix: item.name.to_ascii_uppercase(),
|
||||
is_read_write: item.permission == Permission::ReadWrite,
|
||||
method_name: format_java_method_name(&item.name),
|
||||
is_read_write: pf.permission() == ProtoFlagPermission::READ_WRITE,
|
||||
method_name: format_java_method_name(pf.name()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,8 +109,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_generate_java_code() {
|
||||
let cache = crate::test::create_cache();
|
||||
let generated_files = generate_java_code(&cache).unwrap();
|
||||
let parsed_flags = crate::test::parse_test_flags();
|
||||
let generated_files =
|
||||
generate_java_code(crate::test::TEST_PACKAGE, parsed_flags.parsed_flag.iter()).unwrap();
|
||||
let expect_flags_content = r#"
|
||||
package com.android.aconfig.test;
|
||||
public final class Flags {
|
||||
@@ -131,7 +128,6 @@ mod tests {
|
||||
return FEATURE_FLAGS.enabledRw();
|
||||
}
|
||||
private static FeatureFlags FEATURE_FLAGS = new FeatureFlagsImpl();
|
||||
|
||||
}
|
||||
"#;
|
||||
let expected_featureflagsimpl_content = r#"
|
||||
|
Reference in New Issue
Block a user