Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | |
| 5 | #ifndef BUFFET_COMMANDS_OBJECT_SCHEMA_H_ |
| 6 | #define BUFFET_COMMANDS_OBJECT_SCHEMA_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 12 | #include <chromeos/error.h> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 13 | |
| 14 | namespace base { |
| 15 | class Value; |
| 16 | class DictionaryValue; |
| 17 | } // namespace base |
| 18 | |
| 19 | namespace buffet { |
| 20 | |
| 21 | class PropType; |
| 22 | |
| 23 | // ObjectSchema is a class representing an object definition in GCD command |
| 24 | // schema. This could represent a GCD command definition, but also it can be |
| 25 | // used when defining custom object types for command properties such as |
| 26 | // output media type (paper) for print command. The schema definition for |
| 27 | // these type of object description is the same. |
| 28 | class ObjectSchema final { |
| 29 | public: |
| 30 | // Properties is a string-to-PropType map representing a list of |
| 31 | // properties defined for a command/object. The key is the parameter |
| 32 | // name and the value is the parameter type definition object. |
| 33 | using Properties = std::map<std::string, std::shared_ptr<PropType>>; |
| 34 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 35 | // Declaring default and copy constructors to document the copyable |
| 36 | // nature of this class. Using the default implementation for them though. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 37 | ObjectSchema() = default; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 38 | ObjectSchema(const ObjectSchema& rhs) = default; |
| 39 | ObjectSchema& operator=(const ObjectSchema& rhs) = default; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 40 | |
| 41 | // Add a new parameter definition. |
| 42 | void AddProp(const std::string& name, std::shared_ptr<PropType> prop); |
| 43 | // Finds parameter type definition by name. Returns nullptr if not found. |
| 44 | const PropType* GetProp(const std::string& name) const; |
| 45 | // Gets the list of all the properties defined. |
| 46 | const Properties& GetProps() const { return properties_; } |
| 47 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 48 | // Specify whether extra properties are allowed on objects described by |
| 49 | // this schema. When validating a value of an object type, we can |
| 50 | // make sure that the value has only the properties explicitly defined by |
| 51 | // the schema and no other (custom) properties are allowed. |
| 52 | // This is to support JSON Schema's "additionalProperties" specification. |
| 53 | bool GetExtraPropertiesAllowed() const { return extra_properties_allowed_; } |
| 54 | void SetExtraPropertiesAllowed(bool allowed) { |
| 55 | extra_properties_allowed_ = allowed; |
| 56 | } |
| 57 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 58 | // Saves the object schema to JSON. When |full_schema| is set to true, |
| 59 | // then all properties and constraints are saved, otherwise, only |
| 60 | // the overridden (not inherited) ones are saved. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 61 | std::unique_ptr<base::DictionaryValue> ToJson( |
| 62 | bool full_schema, chromeos::ErrorPtr* error) const; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 63 | // Loads the object schema from JSON. If |object_schema| is not nullptr, it is |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 64 | // used as a base schema to inherit omitted properties and constraints from. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 65 | bool FromJson(const base::DictionaryValue* value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 66 | const ObjectSchema* object_schema, chromeos::ErrorPtr* error); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | // Internal helper method to load individual parameter type definitions. |
| 70 | bool PropFromJson(const std::string& prop_name, |
| 71 | const base::Value& value, |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 72 | const PropType* base_schema, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 73 | Properties* properties, chromeos::ErrorPtr* error) const; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 74 | // Helper function in case the parameter is defined as JSON string like this: |
| 75 | // "prop":"..." |
| 76 | bool PropFromJsonString(const std::string& prop_name, |
| 77 | const base::Value& value, |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 78 | const PropType* base_schema, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 79 | Properties* properties, |
| 80 | chromeos::ErrorPtr* error) const; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 81 | // Helper function in case the parameter is defined as JSON array like this: |
| 82 | // "prop":[...] |
| 83 | bool PropFromJsonArray(const std::string& prop_name, |
| 84 | const base::Value& value, |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 85 | const PropType* base_schema, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 86 | Properties* properties, |
| 87 | chromeos::ErrorPtr* error) const; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 88 | // Helper function in case the parameter is defined as JSON object like this: |
| 89 | // "prop":{...} |
| 90 | bool PropFromJsonObject(const std::string& prop_name, |
| 91 | const base::Value& value, |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 92 | const PropType* base_schema, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 93 | Properties* properties, |
| 94 | chromeos::ErrorPtr* error) const; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 95 | |
| 96 | // Internal parameter type definition map. |
| 97 | Properties properties_; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 98 | bool extra_properties_allowed_{false}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace buffet |
| 102 | |
| 103 | #endif // BUFFET_COMMANDS_OBJECT_SCHEMA_H_ |