/src/spotify-json/src/detail/field_registry.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018 Spotify AB |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | * use this file except in compliance with the License. You may obtain a copy of |
6 | | * the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | * License for the specific language governing permissions and limitations under |
14 | | * the License. |
15 | | */ |
16 | | |
17 | | #include <spotify/json/detail/field_registry.hpp> |
18 | | |
19 | | #include <spotify/json/codec/string.hpp> |
20 | | #include <spotify/json/encode_context.hpp> |
21 | | |
22 | | namespace spotify { |
23 | | namespace json { |
24 | | namespace detail { |
25 | | namespace { |
26 | | |
27 | 2.33k | std::string escape_key(const std::string &key) { |
28 | 2.33k | encode_context context; |
29 | 2.33k | codec::string().encode(context, key); |
30 | 2.33k | context.append(':'); |
31 | 2.33k | return std::string(context.data(), context.size()); |
32 | 2.33k | } |
33 | | |
34 | | } // namespace |
35 | | |
36 | 2.33k | field_registry::field_registry() = default; |
37 | 2.33k | field_registry::~field_registry() = default; |
38 | 0 | field_registry::field_registry(const field_registry &) = default; |
39 | 0 | field_registry::field_registry(field_registry &&) = default; |
40 | | |
41 | | void field_registry::save(const std::string &name, bool required, |
42 | 2.33k | const std::shared_ptr<field> &f) { |
43 | 2.33k | const auto was_saved = |
44 | 2.33k | _fields.insert(typename field_map::value_type(name, f)).second; |
45 | 2.33k | if (was_saved) { |
46 | 2.33k | _field_list.push_back(std::make_pair(escape_key(name), f)); |
47 | 2.33k | _num_required_fields += required ? 1 : 0; |
48 | 2.33k | } |
49 | 2.33k | } |
50 | | |
51 | 18.5k | const field *field_registry::find(const std::string &name) const noexcept { |
52 | 18.5k | const auto field_it = _fields.find(name); |
53 | 18.5k | if (json_likely(field_it != _fields.end())) { |
54 | 1.44k | return (*field_it).second.get(); |
55 | 17.0k | } else { |
56 | 17.0k | return nullptr; |
57 | 17.0k | } |
58 | 18.5k | } |
59 | | |
60 | | } // namespace detail |
61 | | } // namespace json |
62 | | } // namespace spotify |