summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Cherry2017-12-11 03:40:07 -0600
committerTom Cherry2018-01-16 16:44:40 -0600
commit927c5d5fdc46eb60c28e569a922659733d927ada (patch)
treef24924d114a787afda709db0dee2039d3a0bb7ee /property_service
parent4ba3ebbc2f8820741003a36b7ca6c545333f1627 (diff)
downloadplatform-system-core-927c5d5fdc46eb60c28e569a922659733d927ada.tar.gz
platform-system-core-927c5d5fdc46eb60c28e569a922659733d927ada.tar.xz
platform-system-core-927c5d5fdc46eb60c28e569a922659733d927ada.zip
Introduce property types
Properties right now can take any format, but that makes it hard to specify an API for these properties as Treble intends to do. Therefore this change introduces the idea of property types, described below. 1) 'string' this is the default type and allows any property to be set. 2) 'bool' this allows only boolean values (true|false|1|0) 3) 'int' and 'uint' these allow signed and unsigned integer values respectively. 4) 'double' this allows floating point numbers with double precision. 5) 'size' this allows for strings matching [0-9]+[gkm]. 6) 'enum' this allows only a specific set of space deliminated values to be set, e.g. 'enum allow these strings' only allows one of 'allow', 'these', or 'strings' to be set. Bug: 70858511 Test: unit tests, test that properties are only set if their type matches Change-Id: I7a6b00fb43ec630d1f56c9e9a1f1b61d3914f603
Diffstat (limited to 'property_service')
-rw-r--r--property_service/libpropertyinfoserializer/property_info_file.cpp65
1 files changed, 62 insertions, 3 deletions
diff --git a/property_service/libpropertyinfoserializer/property_info_file.cpp b/property_service/libpropertyinfoserializer/property_info_file.cpp
index bf96d88bf..2cdc62dce 100644
--- a/property_service/libpropertyinfoserializer/property_info_file.cpp
+++ b/property_service/libpropertyinfoserializer/property_info_file.cpp
@@ -1,9 +1,26 @@
1//
2// Copyright (C) 2017 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
1#include <property_info_serializer/property_info_serializer.h> 17#include <property_info_serializer/property_info_serializer.h>
2 18
3#include <android-base/strings.h> 19#include <android-base/strings.h>
4 20
5#include "space_tokenizer.h" 21#include "space_tokenizer.h"
6 22
23using android::base::Join;
7using android::base::Split; 24using android::base::Split;
8using android::base::StartsWith; 25using android::base::StartsWith;
9using android::base::Trim; 26using android::base::Trim;
@@ -11,6 +28,34 @@ using android::base::Trim;
11namespace android { 28namespace android {
12namespace properties { 29namespace properties {
13 30
31namespace {
32
33bool IsTypeValid(const std::vector<std::string>& type_strings) {
34 if (type_strings.empty()) {
35 return false;
36 }
37
38 // There must be at least one string following 'enum'
39 if (type_strings[0] == "enum") {
40 return type_strings.size() > 1;
41 }
42
43 // There should not be any string following any other types.
44 if (type_strings.size() != 1) {
45 return false;
46 }
47
48 // Check that the type matches one of remaining valid types.
49 static const char* const no_parameter_types[] = {"string", "bool", "int",
50 "uint", "double", "size"};
51 for (const auto& type : no_parameter_types) {
52 if (type_strings[0] == type) {
53 return true;
54 }
55 }
56 return false;
57}
58
14bool ParsePropertyInfoLine(const std::string& line, PropertyInfoEntry* out, std::string* error) { 59bool ParsePropertyInfoLine(const std::string& line, PropertyInfoEntry* out, std::string* error) {
15 auto tokenizer = SpaceTokenizer(line); 60 auto tokenizer = SpaceTokenizer(line);
16 61
@@ -26,14 +71,28 @@ bool ParsePropertyInfoLine(const std::string& line, PropertyInfoEntry* out, std:
26 return false; 71 return false;
27 } 72 }
28 73
29 // It is not an error to not find these, as older files will not contain them. 74 // It is not an error to not find exact_match or a type, as older files will not contain them.
30 auto exact_match = tokenizer.GetNext(); 75 auto exact_match = tokenizer.GetNext();
31 auto type = tokenizer.GetRemaining(); 76 // We reformat type to be space deliminated regardless of the input whitespace for easier storage
77 // and subsequent parsing.
78 auto type_strings = std::vector<std::string>{};
79 auto type = tokenizer.GetNext();
80 while (!type.empty()) {
81 type_strings.emplace_back(type);
82 type = tokenizer.GetNext();
83 }
32 84
33 *out = {property, context, type, exact_match == "exact"}; 85 if (!type_strings.empty() && !IsTypeValid(type_strings)) {
86 *error = "Type '" + Join(type_strings, " ") + "' is not valid";
87 return false;
88 }
89
90 *out = {property, context, Join(type_strings, " "), exact_match == "exact"};
34 return true; 91 return true;
35} 92}
36 93
94} // namespace
95
37void ParsePropertyInfoFile(const std::string& file_contents, 96void ParsePropertyInfoFile(const std::string& file_contents,
38 std::vector<PropertyInfoEntry>* property_infos, 97 std::vector<PropertyInfoEntry>* property_infos,
39 std::vector<std::string>* errors) { 98 std::vector<std::string>* errors) {