summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/util.cc
blob: 38e6b30674e75bb39bdae1d90314390fb1d32d94 (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
// 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/values.h"

namespace json_schema_compiler {
namespace util {

bool GetStrings(
    const base::DictionaryValue& from,
    const std::string& name,
    std::vector<std::string>* out) {
  base::ListValue* list = NULL;
  if (!from.GetListWithoutPathExpansion(name, &list))
    return false;

  std::string string;
  for (size_t i = 0; i < list->GetSize(); ++i) {
    if (!list->GetString(i, &string))
      return false;
    out->push_back(string);
  }

  return true;
}

bool GetOptionalStrings(
    const base::DictionaryValue& from,
    const std::string& name,
    scoped_ptr<std::vector<std::string> >* out) {
  base::ListValue* list = NULL;
  {
    base::Value* maybe_list = NULL;
    // Since |name| is optional, its absence is acceptable. However, anything
    // other than a ListValue is not.
    if (!from.GetWithoutPathExpansion(name, &maybe_list))
      return true;
    if (!maybe_list->IsType(base::Value::TYPE_LIST))
      return false;
    list = static_cast<base::ListValue*>(maybe_list);
  }

  out->reset(new std::vector<std::string>());
  std::string string;
  for (size_t i = 0; i < list->GetSize(); ++i) {
    if (!list->GetString(i, &string)) {
      out->reset();
      return false;
    }
    (*out)->push_back(string);
  }

  return true;
}

void SetStrings(
    const std::vector<std::string>& from,
    const std::string& name,
    base::DictionaryValue* out) {
  base::ListValue* list = new base::ListValue();
  out->SetWithoutPathExpansion(name, list);
  for (std::vector<std::string>::const_iterator it = from.begin();
      it != from.end(); ++it) {
    list->Append(base::Value::CreateStringValue(*it));
  }
}

void SetOptionalStrings(
    const scoped_ptr<std::vector<std::string> >& from,
    const std::string& name,
    base::DictionaryValue* out) {
  if (!from.get())
    return;

  SetStrings(*from, name, out);
}

}  // namespace api_util
}  // namespace extensions