summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/util.cc
blob: 3e36cfc7a6957c7226fa0c5b2b10e78a7a6a104e (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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/util.h"

#include "base/stl_util.h"
#include "base/values.h"

namespace json_schema_compiler {
namespace util {

bool PopulateItem(const base::Value& from, int* out) {
  return from.GetAsInteger(out);
}

bool PopulateItem(const base::Value& from, bool* out) {
  return from.GetAsBoolean(out);
}

bool PopulateItem(const base::Value& from, double* out) {
  return from.GetAsDouble(out);
}

bool PopulateItem(const base::Value& from, std::string* out) {
  return from.GetAsString(out);
}

bool PopulateItem(const base::Value& from, std::vector<char>* out) {
  const base::BinaryValue* binary = nullptr;
  if (!from.GetAsBinary(&binary))
    return false;
  out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize());
  return true;
}

bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out) {
  *out = make_linked_ptr(from.DeepCopy());
  return true;
}

bool PopulateItem(const base::Value& from,
                  linked_ptr<base::DictionaryValue>* out) {
  const base::DictionaryValue* dict = nullptr;
  if (!from.GetAsDictionary(&dict))
    return false;
  *out = make_linked_ptr(dict->DeepCopy());
  return true;
}

void AddItemToList(const int from, base::ListValue* out) {
  out->Append(new base::FundamentalValue(from));
}

void AddItemToList(const bool from, base::ListValue* out) {
  out->Append(new base::FundamentalValue(from));
}

void AddItemToList(const double from, base::ListValue* out) {
  out->Append(new base::FundamentalValue(from));
}

void AddItemToList(const std::string& from, base::ListValue* out) {
  out->Append(new base::StringValue(from));
}

void AddItemToList(const std::vector<char>& from, base::ListValue* out) {
  out->Append(base::BinaryValue::CreateWithCopiedBuffer(vector_as_array(&from),
                                                        from.size()));
}

void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out) {
  out->Append(from->DeepCopy());
}

void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
                   base::ListValue* out) {
  out->Append(static_cast<base::Value*>(from->DeepCopy()));
}

std::string ValueTypeToString(base::Value::Type type) {
  switch (type) {
    case base::Value::TYPE_NULL:
      return "null";
    case base::Value::TYPE_BOOLEAN:
      return "boolean";
    case base::Value::TYPE_INTEGER:
      return "integer";
    case base::Value::TYPE_DOUBLE:
      return "number";
    case base::Value::TYPE_STRING:
      return "string";
    case base::Value::TYPE_BINARY:
      return "binary";
    case base::Value::TYPE_DICTIONARY:
      return "dictionary";
    case base::Value::TYPE_LIST:
      return "list";
  }
  NOTREACHED();
  return "";
}

}  // namespace util
}  // namespace json_schema_compiler