Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | use anyhow::Result; |
| 18 | use serde::Serialize; |
| 19 | use tinytemplate::TinyTemplate; |
| 20 | |
| 21 | use crate::aconfig::{FlagState, Permission}; |
| 22 | use crate::cache::{Cache, Item}; |
| 23 | use crate::commands::OutputFile; |
| 24 | |
| 25 | pub fn generate_cpp_code(cache: &Cache) -> Result<OutputFile> { |
| 26 | let class_elements: Vec<ClassElement> = cache.iter().map(create_class_element).collect(); |
| 27 | let readwrite = class_elements.iter().any(|item| item.readwrite); |
| 28 | let namespace = cache.namespace().to_lowercase(); |
| 29 | let context = Context { namespace: namespace.clone(), readwrite, class_elements }; |
| 30 | let mut template = TinyTemplate::new(); |
| 31 | template.add_template("cpp_code_gen", include_str!("../templates/cpp.template"))?; |
| 32 | let contents = template.render("cpp_code_gen", &context)?; |
| 33 | let path = ["aconfig", &(namespace + ".h")].iter().collect(); |
| 34 | Ok(OutputFile { contents: contents.into(), path }) |
| 35 | } |
| 36 | |
| 37 | #[derive(Serialize)] |
| 38 | struct Context { |
| 39 | pub namespace: String, |
| 40 | pub readwrite: bool, |
| 41 | pub class_elements: Vec<ClassElement>, |
| 42 | } |
| 43 | |
| 44 | #[derive(Serialize)] |
| 45 | struct ClassElement { |
| 46 | pub readwrite: bool, |
| 47 | pub default_value: String, |
| 48 | pub flag_name: String, |
| 49 | } |
| 50 | |
| 51 | fn create_class_element(item: &Item) -> ClassElement { |
| 52 | ClassElement { |
| 53 | readwrite: item.permission == Permission::ReadWrite, |
| 54 | default_value: if item.state == FlagState::Enabled { |
| 55 | "true".to_string() |
| 56 | } else { |
| 57 | "false".to_string() |
| 58 | }, |
| 59 | flag_name: item.name.clone(), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[cfg(test)] |
| 64 | mod tests { |
| 65 | use super::*; |
| 66 | use crate::aconfig::{FlagDeclaration, FlagState, FlagValue, Permission}; |
| 67 | use crate::commands::Source; |
| 68 | |
| 69 | #[test] |
| 70 | fn test_cpp_codegen_build_time_flag_only() { |
| 71 | let namespace = "my_namespace"; |
| 72 | let mut cache = Cache::new(namespace.to_string()).unwrap(); |
| 73 | cache |
| 74 | .add_flag_declaration( |
| 75 | Source::File("aconfig_one.txt".to_string()), |
| 76 | FlagDeclaration { |
| 77 | name: "my_flag_one".to_string(), |
| 78 | description: "buildtime disable".to_string(), |
| 79 | }, |
| 80 | ) |
| 81 | .unwrap(); |
| 82 | cache |
| 83 | .add_flag_value( |
| 84 | Source::Memory, |
| 85 | FlagValue { |
| 86 | namespace: namespace.to_string(), |
| 87 | name: "my_flag_one".to_string(), |
| 88 | state: FlagState::Disabled, |
| 89 | permission: Permission::ReadOnly, |
| 90 | }, |
| 91 | ) |
| 92 | .unwrap(); |
| 93 | cache |
| 94 | .add_flag_declaration( |
| 95 | Source::File("aconfig_two.txt".to_string()), |
| 96 | FlagDeclaration { |
| 97 | name: "my_flag_two".to_string(), |
| 98 | description: "buildtime enable".to_string(), |
| 99 | }, |
| 100 | ) |
| 101 | .unwrap(); |
| 102 | cache |
| 103 | .add_flag_value( |
| 104 | Source::Memory, |
| 105 | FlagValue { |
| 106 | namespace: namespace.to_string(), |
| 107 | name: "my_flag_two".to_string(), |
| 108 | state: FlagState::Enabled, |
| 109 | permission: Permission::ReadOnly, |
| 110 | }, |
| 111 | ) |
| 112 | .unwrap(); |
| 113 | let expect_content = r#"#ifndef my_namespace_HEADER_H |
| 114 | #define my_namespace_HEADER_H |
| 115 | #include "my_namespace.h" |
| 116 | |
| 117 | namespace my_namespace { |
| 118 | |
| 119 | class my_flag_one { |
| 120 | public: |
| 121 | virtual const bool value() { |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | class my_flag_two { |
| 127 | public: |
| 128 | virtual const bool value() { |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |
| 134 | #endif |
| 135 | "#; |
| 136 | let file = generate_cpp_code(&cache).unwrap(); |
| 137 | assert_eq!("aconfig/my_namespace.h", file.path.to_str().unwrap()); |
| 138 | assert_eq!( |
| 139 | expect_content.replace(' ', ""), |
| 140 | String::from_utf8(file.contents).unwrap().replace(' ', "") |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn test_cpp_codegen_runtime_flag() { |
| 146 | let namespace = "my_namespace"; |
| 147 | let mut cache = Cache::new(namespace.to_string()).unwrap(); |
| 148 | cache |
| 149 | .add_flag_declaration( |
| 150 | Source::File("aconfig_one.txt".to_string()), |
| 151 | FlagDeclaration { |
| 152 | name: "my_flag_one".to_string(), |
| 153 | description: "buildtime disable".to_string(), |
| 154 | }, |
| 155 | ) |
| 156 | .unwrap(); |
| 157 | cache |
| 158 | .add_flag_declaration( |
| 159 | Source::File("aconfig_two.txt".to_string()), |
| 160 | FlagDeclaration { |
| 161 | name: "my_flag_two".to_string(), |
| 162 | description: "runtime enable".to_string(), |
| 163 | }, |
| 164 | ) |
| 165 | .unwrap(); |
| 166 | cache |
| 167 | .add_flag_value( |
| 168 | Source::Memory, |
| 169 | FlagValue { |
| 170 | namespace: namespace.to_string(), |
| 171 | name: "my_flag_two".to_string(), |
| 172 | state: FlagState::Enabled, |
| 173 | permission: Permission::ReadWrite, |
| 174 | }, |
| 175 | ) |
| 176 | .unwrap(); |
| 177 | let expect_content = r#"#ifndef my_namespace_HEADER_H |
| 178 | #define my_namespace_HEADER_H |
| 179 | #include "my_namespace.h" |
| 180 | |
| 181 | #include <server_configurable_flags/get_flags.h> |
| 182 | using namespace server_configurable_flags; |
| 183 | |
| 184 | namespace my_namespace { |
| 185 | |
| 186 | class my_flag_one { |
| 187 | public: |
| 188 | virtual const bool value() { |
| 189 | return GetServerConfigurableFlag( |
| 190 | "my_namespace", |
| 191 | "my_flag_one", |
| 192 | "false") == "true"; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | class my_flag_two { |
| 197 | public: |
| 198 | virtual const bool value() { |
| 199 | return GetServerConfigurableFlag( |
| 200 | "my_namespace", |
| 201 | "my_flag_two", |
| 202 | "true") == "true"; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | #endif |
| 208 | "#; |
| 209 | let file = generate_cpp_code(&cache).unwrap(); |
| 210 | assert_eq!("aconfig/my_namespace.h", file.path.to_str().unwrap()); |
| 211 | assert_eq!( |
| 212 | expect_content.replace(' ', ""), |
| 213 | String::from_utf8(file.contents).unwrap().replace(' ', "") |
| 214 | ); |
| 215 | } |
| 216 | } |