aconfig: restrict valid namespace and flag names
The namespace and flag names will be used as identifiers in the
auto-generated code. Place restrictions on what constitutes a valid
name.
Valid identifiers are those that match /[a-z][a-z0-9_]/. aconfig
explicitly does not implement any automatic translation to make names
valid identifiers: this sidesteps potential conflicts such as "foo.bar"
and "foo_bar" mapping to the same name if dots were translated to
underscores.
Bug: b/284252015
Test: atest aconfig.test
Change-Id: I38d005a74311e5829e540063404d1565071e6e96
diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs
index 733b1c5..98288e7 100644
--- a/tools/aconfig/src/codegen_java.rs
+++ b/tools/aconfig/src/codegen_java.rs
@@ -31,7 +31,7 @@
let mut template = TinyTemplate::new();
template.add_template("java_code_gen", include_str!("../templates/java.template"))?;
let contents = template.render("java_code_gen", &context)?;
- let mut path: PathBuf = namespace.split('.').collect();
+ let mut path: PathBuf = ["aconfig", namespace].iter().collect();
// TODO: Allow customization of the java class name
path.push("Flags.java");
Ok(OutputFile { contents: contents.into(), path })
@@ -76,7 +76,7 @@
#[test]
fn test_generate_java_code() {
- let namespace = "com.example";
+ let namespace = "example";
let mut builder = CacheBuilder::new(namespace.to_string()).unwrap();
builder
.add_flag_declaration(
@@ -106,7 +106,7 @@
)
.unwrap();
let cache = builder.build();
- let expect_content = r#"package com.example;
+ let expect_content = r#"package aconfig.example;
import android.provider.DeviceConfig;
@@ -118,7 +118,7 @@
public static boolean test2() {
return DeviceConfig.getBoolean(
- "com.example",
+ "example",
"test2__test2",
false
);
@@ -127,7 +127,7 @@
}
"#;
let file = generate_java_code(&cache).unwrap();
- assert_eq!("com/example/Flags.java", file.path.to_str().unwrap());
+ assert_eq!("aconfig/example/Flags.java", file.path.to_str().unwrap());
assert_eq!(
expect_content.replace(' ', ""),
String::from_utf8(file.contents).unwrap().replace(' ', "")