summaryrefslogtreecommitdiff
path: root/SDL_Core/src/components/smart_objects/src/object_optional_schema_item.cc
blob: 7765a0e3a77220b501a3b5141357d3bd9b77e1b0 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
 * @file object_optional_schema_item.cc
 * @brief Schema item of object with optional parameters.
 */
// Copyright (c) 2013, Ford Motor Company
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with the
// distribution.
//
// Neither the name of the Ford Motor Company nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "smart_objects/object_optional_schema_item.h"
#include "smart_objects/always_false_schema_item.h"
#include "smart_objects/smart_object.h"

namespace NsSmartDeviceLink {
namespace NsSmartObjects {

namespace smart_objects = NsSmartDeviceLink::NsSmartObjects;

const char* smart_objects::ObjectOptionalSchemaItem::kOptionalGenericFieldName
  = "<-- @@@@$$$&&& uniqe1 uancdm,vn 991188 --->";


//----------------------------------------------------------------------------

utils::SharedPtr<smart_objects::ObjectOptionalSchemaItem>
  smart_objects::ObjectOptionalSchemaItem::create(
    const std::map<std::string,
    smart_objects::CObjectSchemaItem::SMember> & members) {
  return new ObjectOptionalSchemaItem(members);
}

//----------------------------------------------------------------------------

smart_objects::Errors::eType smart_objects::ObjectOptionalSchemaItem::validate(
    const smart_objects::SmartObject & object) {
  smart_objects::Errors::eType result = smart_objects::Errors::ERROR;

  if (NsSmartDeviceLink::NsSmartObjects::SmartType_Map == object.getType()) {
    result = smart_objects::Errors::OK;
    const std::set<std::string> object_keys = object.enumerate();

    for (std::map<std::string, CObjectSchemaItem::SMember>
          ::const_iterator i = mMembers.begin(); i != mMembers.end(); ++i) {
      if (object_keys.end() != object_keys.find(i->first)) {
          result = i->second.mSchemaItem->validate(object.getElement(i->first));
      } else if (true == i->second.mIsMandatory
          && false == IsOptionalName(i->first)) {
        result = smart_objects::Errors::MISSING_MANDATORY_PARAMETER;
      }
      if (smart_objects::Errors::OK != result) {
        break;
      }
    }

    bool have_optional_parameter = false;
    std::map<std::string, CObjectSchemaItem::SMember>::const_iterator
      optional_iterator = mMembers.find(kOptionalGenericFieldName);
    if (smart_objects::Errors::OK == result) {
      for (std::set<std::string>::const_iterator k = object_keys.begin();
            k != object_keys.end(); ++k) {
        if (mMembers.end() == mMembers.find(*k)) {
          // this key is supposed as optional
          if (mMembers.end() != optional_iterator) {
            // validate optional (having any name) object member
            have_optional_parameter = true;
            result = optional_iterator->
                     second.mSchemaItem->validate(object.getElement(*k));
          } else {
            result = smart_objects::Errors::UNEXPECTED_PARAMETER;
            break;
          }
          if (smart_objects::Errors::OK != result) {
            break;
          }
        }
      }
      if (false == have_optional_parameter
          && mMembers.end() != optional_iterator
          && true == optional_iterator->second.mIsMandatory) {
        result = smart_objects::Errors::MISSING_MANDATORY_UNTITLED_PARAMETER;
      }
    }
  } else {
    result = smart_objects::Errors::INVALID_VALUE;
  }

  return result;
}

//----------------------------------------------------------------------------

bool smart_objects::ObjectOptionalSchemaItem::IsOptionalName(
    const std::string& name) {
  return (0 == name.compare(kOptionalGenericFieldName));
}

//----------------------------------------------------------------------------

smart_objects::ObjectOptionalSchemaItem::ObjectOptionalSchemaItem(
    const std::map<std::string,
                   smart_objects::CObjectSchemaItem::SMember> & members):
  CObjectSchemaItem(members) {
}

//----------------------------------------------------------------------------

std::set<std::string>
ObjectOptionalSchemaItem::GetOptionalObjectKeys(const SmartObject& root_obj) {
  std::set<std::string> optional_objects;

  const std::set<std::string> object_keys = root_obj.enumerate();
  typedef std::set<std::string>::const_iterator Iter;
  for (Iter k = object_keys.begin(); k != object_keys.end(); ++k) {
    // if an object is not among the schema items then it is optional
    if (mMembers.end() == mMembers.find(*k)) {
      optional_objects.insert(*k);
    }
  }

  return optional_objects;
}

//----------------------------------------------------------------------------

void ObjectOptionalSchemaItem::IterateOverOptionalItems(SmartObject* object,
  void (ISchemaItem::* action)(SmartObject&) ) {
  if (0 == mMembers.count(kOptionalGenericFieldName)) {
    return;             // There are no optional items
  }

  utils::SharedPtr<ISchemaItem> schema =
    mMembers.at(kOptionalGenericFieldName).mSchemaItem;

  // Then apply schema for all the optional objects
  std::set<std::string> optionals = GetOptionalObjectKeys(*object);
  typedef std::set<std::string>::const_iterator Iter;
  for (Iter key = optionals.begin(); key != optionals.end(); ++key) {
    (schema.get()->*action)((*object)[*key]);
  }
}

//----------------------------------------------------------------------------

void ObjectOptionalSchemaItem::applySchema(SmartObject & object) {
  // At first apply schema for the regular objects
  CObjectSchemaItem::applySchema(object);

  IterateOverOptionalItems(&object, &ISchemaItem::applySchema);
}

//----------------------------------------------------------------------------

void ObjectOptionalSchemaItem::unapplySchema(SmartObject & object) {
  CObjectSchemaItem::unapplySchema(object);

  IterateOverOptionalItems(&object, &ISchemaItem::unapplySchema);
}

}  // namespace NsSmartObjects
}  // namespace NsSmartDeviceLink