blob: d133b1a2e6d3e50126aec8928ad4b37c648834c0 [file] [log] [blame]
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -07001// 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 Vakulenko5f472062014-08-14 17:54:04 -070012#include <chromeos/error.h>
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070013
14namespace base {
15class Value;
16class DictionaryValue;
17} // namespace base
18
19namespace buffet {
20
21class 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.
28class 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 Vakulenko66ec2922014-06-17 15:30:22 -070035 // Declaring default and copy constructors to document the copyable
36 // nature of this class. Using the default implementation for them though.
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070037 ObjectSchema() = default;
Alex Vakulenko66ec2922014-06-17 15:30:22 -070038 ObjectSchema(const ObjectSchema& rhs) = default;
39 ObjectSchema& operator=(const ObjectSchema& rhs) = default;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070040
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 Vakulenko66ec2922014-06-17 15:30:22 -070048 // 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 Vakulenkoe439a0f2014-05-21 12:26:47 -070058 // 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 Vakulenko5f472062014-08-14 17:54:04 -070061 std::unique_ptr<base::DictionaryValue> ToJson(
62 bool full_schema, chromeos::ErrorPtr* error) const;
Alex Vakulenko66ec2922014-06-17 15:30:22 -070063 // Loads the object schema from JSON. If |object_schema| is not nullptr, it is
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070064 // used as a base schema to inherit omitted properties and constraints from.
Alex Vakulenko66ec2922014-06-17 15:30:22 -070065 bool FromJson(const base::DictionaryValue* value,
Alex Vakulenko5f472062014-08-14 17:54:04 -070066 const ObjectSchema* object_schema, chromeos::ErrorPtr* error);
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070067
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 Vakulenko66ec2922014-06-17 15:30:22 -070072 const PropType* base_schema,
Alex Vakulenko5f472062014-08-14 17:54:04 -070073 Properties* properties, chromeos::ErrorPtr* error) const;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070074 // 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 Vakulenko66ec2922014-06-17 15:30:22 -070078 const PropType* base_schema,
Alex Vakulenko5f472062014-08-14 17:54:04 -070079 Properties* properties,
80 chromeos::ErrorPtr* error) const;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070081 // 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 Vakulenko66ec2922014-06-17 15:30:22 -070085 const PropType* base_schema,
Alex Vakulenko5f472062014-08-14 17:54:04 -070086 Properties* properties,
87 chromeos::ErrorPtr* error) const;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070088 // 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 Vakulenko66ec2922014-06-17 15:30:22 -070092 const PropType* base_schema,
Alex Vakulenko5f472062014-08-14 17:54:04 -070093 Properties* properties,
94 chromeos::ErrorPtr* error) const;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070095
96 // Internal parameter type definition map.
97 Properties properties_;
Alex Vakulenko66ec2922014-06-17 15:30:22 -070098 bool extra_properties_allowed_{false};
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070099};
100
101} // namespace buffet
102
103#endif // BUFFET_COMMANDS_OBJECT_SCHEMA_H_