manifest_parse_util.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_JSON_SCHEMA_COMPILER_MANIFEST_PARSE_UTIL_H_
  5. #define TOOLS_JSON_SCHEMA_COMPILER_MANIFEST_PARSE_UTIL_H_
  6. #include <vector>
  7. #include "base/strings/string16.h"
  8. #include "base/strings/string_piece_forward.h"
  9. #include "base/values.h"
  10. #include "tools/json_schema_compiler/util.h"
  11. namespace json_schema_compiler {
  12. namespace manifest_parse_util {
  13. // This file contains helpers used by auto-generated manifest parsing code.
  14. // Populates |error| and |error_path_reversed| denoting array parse error at the
  15. // given |key|. Note |error| should already contain the specific parse error for
  16. // the array item.
  17. void PopulateArrayParseError(
  18. base::StringPiece key,
  19. base::string16* error,
  20. std::vector<base::StringPiece>* error_path_reversed);
  21. // Populates |error| and |error_path_reversed| denoting the given invalid enum
  22. // |value| at the given |key|.
  23. void PopulateInvalidEnumValueError(
  24. base::StringPiece key,
  25. const std::string& value,
  26. base::string16* error,
  27. std::vector<base::StringPiece>* error_path_reversed);
  28. // Populates manifest parse |error| for the given path in |error_path_reversed|.
  29. void PopulateFinalError(base::string16* error,
  30. std::vector<base::StringPiece>* error_path_reversed);
  31. // Returns the value at the given |key| in |dict|, ensuring that it's of the
  32. // |expected_type|. On failure, returns false and populates |error| and
  33. // |error_path_reversed|.
  34. const base::Value* FindKeyOfType(
  35. const base::DictionaryValue& dict,
  36. base::StringPiece key,
  37. base::Value::Type expected_type,
  38. base::string16* error,
  39. std::vector<base::StringPiece>* error_path_reversed);
  40. // Parses |out| from |dict| at the given |key|. On failure, returns false and
  41. // populates |error| and |error_path_reversed|.
  42. bool ParseFromDictionary(const base::DictionaryValue& dict,
  43. base::StringPiece key,
  44. int* out,
  45. base::string16* error,
  46. std::vector<base::StringPiece>* error_path_reversed);
  47. bool ParseFromDictionary(const base::DictionaryValue& dict,
  48. base::StringPiece key,
  49. bool* out,
  50. base::string16* error,
  51. std::vector<base::StringPiece>* error_path_reversed);
  52. bool ParseFromDictionary(const base::DictionaryValue& dict,
  53. base::StringPiece key,
  54. double* out,
  55. base::string16* error,
  56. std::vector<base::StringPiece>* error_path_reversed);
  57. bool ParseFromDictionary(const base::DictionaryValue& dict,
  58. base::StringPiece key,
  59. std::string* out,
  60. base::string16* error,
  61. std::vector<base::StringPiece>* error_path_reversed);
  62. // This overload is used for lists/arrays.
  63. template <typename T>
  64. bool ParseFromDictionary(const base::DictionaryValue& dict,
  65. base::StringPiece key,
  66. std::vector<T>* out_ptr,
  67. base::string16* error,
  68. std::vector<base::StringPiece>* error_path_reversed);
  69. // This overload is used for optional values.
  70. template <typename T>
  71. bool ParseFromDictionary(const base::DictionaryValue& dict,
  72. base::StringPiece key,
  73. std::unique_ptr<T>* out_ptr,
  74. base::string16* error,
  75. std::vector<base::StringPiece>* error_path_reversed);
  76. // This overload is used for generated types.
  77. template <typename T>
  78. bool ParseFromDictionary(const base::DictionaryValue& dict,
  79. base::StringPiece key,
  80. T* out_ptr,
  81. base::string16* error,
  82. std::vector<base::StringPiece>* error_path_reversed) {
  83. return T::ParseFromDictionary(dict, key, out_ptr, error, error_path_reversed);
  84. }
  85. template <typename T>
  86. bool ParseFromDictionary(const base::DictionaryValue& dict,
  87. base::StringPiece key,
  88. std::vector<T>* out_ptr,
  89. base::string16* error,
  90. std::vector<base::StringPiece>* error_path_reversed) {
  91. const base::Value* value = FindKeyOfType(dict, key, base::Value::Type::LIST,
  92. error, error_path_reversed);
  93. if (!value)
  94. return false;
  95. bool result = json_schema_compiler::util::PopulateArrayFromList(
  96. value->AsListValue(*value), out_ptr, error);
  97. if (!result)
  98. PopulateArrayParseError(key, error, error_path_reversed);
  99. return result;
  100. }
  101. template <typename T>
  102. bool ParseFromDictionary(const base::DictionaryValue& dict,
  103. base::StringPiece key,
  104. std::unique_ptr<T>* out_ptr,
  105. base::string16* error,
  106. std::vector<base::StringPiece>* error_path_reversed) {
  107. DCHECK(out_ptr);
  108. // Ignore optional keys if they are not present without raising an error.
  109. if (!dict.FindKey(key))
  110. return true;
  111. // Parse errors for optional keys which are specified should still cause a
  112. // failure.
  113. auto result = std::make_unique<T>();
  114. if (!ParseFromDictionary(dict, key, result.get(), error, error_path_reversed))
  115. return false;
  116. *out_ptr = std::move(result);
  117. return true;
  118. }
  119. // Alias for pointer to a function which converts a string to an enum of type T.
  120. template <typename T>
  121. using StringToEnumConverter = T (*)(const std::string&);
  122. // Parses enum |out| from |dict| at the given |key|. On failure, returns false
  123. // and populates |error| and |error_path_reversed|.
  124. template <typename T>
  125. bool ParseEnumFromDictionary(
  126. const base::DictionaryValue& dict,
  127. base::StringPiece key,
  128. StringToEnumConverter<T> converter,
  129. bool is_optional_property,
  130. T none_value,
  131. T* out,
  132. base::string16* error,
  133. std::vector<base::StringPiece>* error_path_reversed) {
  134. DCHECK(out);
  135. DCHECK_EQ(none_value, *out);
  136. // Ignore optional keys if they are not present without raising an error.
  137. if (is_optional_property && !dict.FindKey(key))
  138. return true;
  139. // Parse errors for optional keys which are specified should still cause a
  140. // failure.
  141. const base::Value* value = FindKeyOfType(dict, key, base::Value::Type::STRING,
  142. error, error_path_reversed);
  143. if (!value)
  144. return false;
  145. const std::string str = value->GetString();
  146. T enum_value = converter(str);
  147. if (enum_value == none_value) {
  148. PopulateInvalidEnumValueError(key, str, error, error_path_reversed);
  149. return false;
  150. }
  151. *out = enum_value;
  152. return true;
  153. }
  154. } // namespace manifest_parse_util
  155. } // namespace json_schema_compiler
  156. #endif // TOOLS_JSON_SCHEMA_COMPILER_MANIFEST_PARSE_UTIL_H_