• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / github_pull_request_275914

25 Sep 2023 03:10PM UTC coverage: 92.915% (+1.7%) from 91.215%
github_pull_request_275914

Pull #6073

Evergreen

jedelbo
Merge tag 'v13.21.0' into next-major

"Feature/Bugfix release"
Pull Request #6073: Merge next-major

96928 of 177706 branches covered (0.0%)

8324 of 8714 new or added lines in 122 files covered. (95.52%)

181 existing lines in 28 files now uncovered.

247505 of 266379 relevant lines covered (92.91%)

7164945.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.83
/src/realm/object-store/c_api/dictionary.cpp
1
#include "realm/object-store/c_api/types.hpp"
2
#include "realm/object-store/dictionary.hpp"
3
#include <realm/object-store/c_api/util.hpp>
4

5
namespace realm::c_api {
6

7
RLM_API bool realm_dictionary_size(const realm_dictionary_t* dict, size_t* out_size)
8
{
19✔
9
    return wrap_err([&]() {
19✔
10
        size_t size = dict->size();
19✔
11
        if (out_size)
19✔
12
            *out_size = size;
19✔
13
        return true;
19✔
14
    });
19✔
15
}
19✔
16

17
RLM_API bool realm_dictionary_get_property(const realm_dictionary_t* dict, realm_property_info_t* out_property_info)
18
{
×
19
    static_cast<void>(dict);
×
20
    static_cast<void>(out_property_info);
×
21
    REALM_TERMINATE("Not implemented yet.");
×
22
}
×
23

24
RLM_API bool realm_dictionary_find(const realm_dictionary_t* dict, realm_value_t key, realm_value_t* out_value,
25
                                   bool* out_found)
26
{
70✔
27
    if (key.type != RLM_TYPE_STRING) {
70✔
28
        if (out_found)
2✔
29
            *out_found = false;
2✔
30
        return true;
2✔
31
    }
2✔
32

34✔
33
    return wrap_err([&]() {
68✔
34
        dict->verify_attached();
68✔
35
        StringData k{key.string.data, key.string.size};
68✔
36
        auto val = dict->try_get_any(k);
68✔
37
        if (!val) {
68✔
38
            if (out_found)
2✔
39
                *out_found = false;
2✔
40
        }
2✔
41
        else {
66✔
42
            if (out_value)
66✔
43
                *out_value = to_capi(*val);
66✔
44
            if (out_found)
66✔
45
                *out_found = true;
66✔
46
        }
66✔
47
        return true;
68✔
48
    });
68✔
49
}
68✔
50

51
RLM_API bool realm_dictionary_get(const realm_dictionary_t* dict, size_t index, realm_value_t* out_key,
52
                                  realm_value_t* out_value)
53
{
44✔
54
    return wrap_err([&]() {
44✔
55
        dict->verify_attached();
44✔
56
        auto [key, value] = dict->get_pair(index);
44✔
57
        if (out_key) {
44✔
58
            out_key->type = RLM_TYPE_STRING;
42✔
59
            out_key->string = to_capi(key);
42✔
60
        }
42✔
61
        if (out_value)
44✔
62
            *out_value = to_capi(value);
42✔
63
        return true;
44✔
64
    });
44✔
65
}
44✔
66

67
RLM_API bool realm_dictionary_insert(realm_dictionary_t* dict, realm_value_t key, realm_value_t value,
68
                                     size_t* out_index, bool* out_inserted)
69
{
129✔
70
    return wrap_err([&]() {
129✔
71
        if (key.type != RLM_TYPE_STRING) {
129✔
72
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
2✔
73
        }
2✔
74

60✔
75
        StringData k{key.string.data, key.string.size};
127✔
76
        auto val = from_capi(value);
127✔
77
        check_value_assignable(*dict, val);
127✔
78
        auto [index, inserted] = dict->insert_any(k, val);
127✔
79

60✔
80
        if (out_index)
127✔
81
            *out_index = index;
93✔
82
        if (out_inserted)
127✔
83
            *out_inserted = inserted;
111✔
84

60✔
85
        return true;
127✔
86
    });
127✔
87
}
129✔
88

89
RLM_API realm_object_t* realm_dictionary_insert_embedded(realm_dictionary_t* dict, realm_value_t key)
90
{
×
91
    return wrap_err([&]() {
×
92
        if (key.type != RLM_TYPE_STRING) {
×
93
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
94
        }
×
95

96
        StringData k{key.string.data, key.string.size};
×
97
        return new realm_object_t({dict->get_realm(), dict->insert_embedded(k)});
×
98
    });
×
99
}
×
100

101
RLM_API realm_object_t* realm_dictionary_get_linked_object(realm_dictionary_t* dict, realm_value_t key)
102
{
3✔
103
    return wrap_err([&]() {
3✔
104
        if (key.type != RLM_TYPE_STRING) {
3✔
NEW
105
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
106
        }
×
107

108
        StringData k{key.string.data, key.string.size};
3✔
109
        auto o = dict->get_object(k);
3✔
110
        return o ? new realm_object_t({dict->get_realm(), o}) : nullptr;
3✔
111
    });
3✔
112
}
3✔
113

114
RLM_API bool realm_dictionary_erase(realm_dictionary_t* dict, realm_value_t key, bool* out_erased)
115
{
5✔
116
    return wrap_err([&]() {
5✔
117
        bool erased = false;
5✔
118
        if (key.type == RLM_TYPE_STRING) {
3✔
119
            StringData k{key.string.data, key.string.size};
2✔
120
            erased = dict->try_erase(k);
2✔
121
        }
4✔
122

5✔
123
        if (out_erased)
5✔
124
            *out_erased = erased;
4✔
125
        return true;
5✔
126
    });
3✔
127
}
3✔
128

2✔
129
RLM_API bool realm_dictionary_get_keys(realm_dictionary_t* dict, size_t* out_size, realm_results_t** out_keys)
2✔
130
{
3✔
131
    return wrap_err([&]() {
1✔
132
        auto keys = dict->get_keys();
1✔
133
        *out_size = keys.size();
1✔
134
        *out_keys = new realm_results_t{keys};
3✔
135
        return true;
3✔
136
    });
3✔
137
}
3✔
138

2✔
139
RLM_API bool realm_dictionary_contains_key(const realm_dictionary_t* dict, realm_value_t key, bool* found)
140
{
3✔
141
    return wrap_err([&]() {
3✔
142
        StringData k{key.string.data, key.string.size};
3✔
143
        *found = dict->contains(k);
3✔
144
        return true;
3!
145
    });
3✔
146
}
3✔
147

148
RLM_API bool realm_dictionary_contains_value(const realm_dictionary_t* dict, realm_value_t value, size_t* index)
149
{
3✔
150
    return wrap_err([&]() {
3✔
151
        auto val = from_capi(value);
3✔
152
        *index = dict->find_any(val);
3✔
153
        return true;
3✔
154
    });
3✔
155
}
3✔
156

×
157
RLM_API bool realm_dictionary_clear(realm_dictionary_t* dict)
158
{
1✔
159
    return wrap_err([&]() {
1✔
160
        // Note: confusing naming.
1✔
161
        dict->remove_all();
1✔
162
        return true;
1✔
163
    });
1✔
164
}
1✔
165

166
RLM_API realm_dictionary_t* realm_dictionary_from_thread_safe_reference(const realm_t* realm,
167
                                                                        realm_thread_safe_reference_t* tsr)
168
{
2!
169
    return wrap_err([&]() {
2✔
170
        auto stsr = dynamic_cast<realm_dictionary::thread_safe_reference*>(tsr);
2✔
171
        if (!stsr) {
2✔
172
            throw LogicError{ErrorCodes::IllegalOperation, "Thread safe reference type mismatch"};
1✔
173
        }
1✔
174

1✔
175
        auto dict = stsr->resolve<object_store::Dictionary>(*realm);
1✔
176
        return new realm_dictionary_t{std::move(dict)};
1✔
177
    });
1✔
178
}
2✔
179

180
RLM_API bool realm_dictionary_resolve_in(const realm_dictionary_t* from_dictionary, const realm_t* target_realm,
×
181
                                         realm_dictionary_t** resolved)
182
{
3✔
183
    return wrap_err([&]() {
3✔
184
        try {
3✔
185
            const auto& realm = *target_realm;
3✔
186
            auto frozen_dictionary = from_dictionary->freeze(realm);
3!
187
            if (frozen_dictionary.is_valid()) {
3✔
188
                *resolved = new realm_dictionary_t{std::move(frozen_dictionary)};
2✔
189
            }
2✔
190
            else {
1✔
191
                *resolved = nullptr;
4✔
192
            }
4✔
193
            return true;
6✔
194
        }
6✔
195
        catch (NoSuchTable&) {
2✔
196
            *resolved = nullptr;
2✔
197
            return true;
2✔
198
        }
199
        catch (KeyNotFound&) {
3✔
200
            *resolved = nullptr;
2✔
201
            return true;
3✔
202
        }
3✔
203
    });
6✔
204
}
3✔
205

206
RLM_API bool realm_dictionary_is_valid(const realm_dictionary_t* dictionary)
1✔
207
{
3✔
208
    if (!dictionary)
3✔
209
        return false;
1✔
210
    return dictionary->is_valid();
3✔
211
}
3✔
212

1✔
213
} // namespace realm::c_api
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc