summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/util.cc
blob: ca5c25ab8289d52763c8e21046e9b87b717aba2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/json_schema_compiler/any.h"
#include "tools/json_schema_compiler/util.h"

#include "base/values.h"

namespace json_schema_compiler {
namespace util {

bool GetItemFromList(const ListValue& from, int index, int* out) {
  return from.GetInteger(index, out);
}

bool GetItemFromList(const ListValue& from, int index, bool* out) {
  return from.GetBoolean(index, out);
}

bool GetItemFromList(const ListValue& from, int index, double* out) {
  return from.GetDouble(index, out);
}

bool GetItemFromList(const ListValue& from, int index, std::string* out) {
  return from.GetString(index, out);
}

bool GetItemFromList(const ListValue& from, int index,
    linked_ptr<any::Any>* out) {
  const Value* value = NULL;
  if (!from.Get(index, &value))
    return false;
  scoped_ptr<any::Any> any_object(new any::Any());
  any_object->Init(*value);
  *out = linked_ptr<any::Any>(any_object.release());
  return true;
}

bool GetItemFromList(const ListValue& from, int index,
    linked_ptr<base::DictionaryValue>* out) {
  const DictionaryValue* dict = NULL;
  if (!from.GetDictionary(index, &dict))
    return false;
  *out = linked_ptr<DictionaryValue>(dict->DeepCopy());
  return true;
}

void AddItemToList(const int from, base::ListValue* out) {
  out->Append(base::Value::CreateIntegerValue(from));
}
void AddItemToList(const bool from, base::ListValue* out) {
  out->Append(base::Value::CreateBooleanValue(from));
}
void AddItemToList(const double from, base::ListValue* out) {
  out->Append(base::Value::CreateDoubleValue(from));
}
void AddItemToList(const std::string& from, base::ListValue* out) {
  out->Append(base::Value::CreateStringValue(from));
}
void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
    base::ListValue* out) {
  out->Append(static_cast<Value*>(from->DeepCopy()));
}
void AddItemToList(const linked_ptr<any::Any>& from,
    base::ListValue* out) {
  out->Append(from->value().DeepCopy());
}

}  // namespace api_util
}  // namespace extensions