js_util.hpp
3.9 KB
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
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#pragma once
#include <limits>
#include <sstream>
#include <stdexcept>
#include "object_schema.hpp"
#include "shared_realm.hpp"
namespace realm {
namespace js {
enum class AggregateFunc {
Min,
Max,
Sum,
Avg
};
template<typename T>
class RealmDelegate;
template<typename T>
static inline RealmDelegate<T> *get_delegate(realm::Realm *realm) {
return static_cast<RealmDelegate<T> *>(realm->m_binding_context.get());
}
template<typename T>
static inline T stot(const std::string &s) {
std::istringstream iss(s);
T value;
iss >> value;
if (iss.fail()) {
throw std::invalid_argument("Cannot convert string '" + s + "'");
}
return value;
}
static inline uint32_t validated_positive_index(std::string string) {
int64_t index = stot<int64_t>(string);
if (index < 0) {
throw std::out_of_range(std::string("Index ") + string + " cannot be less than zero.");
}
if (index > std::numeric_limits<uint32_t>::max()) {
throw std::out_of_range(std::string("Index ") + string + " must be a 32-bit unsigned integer");
}
return static_cast<uint32_t>(index);
}
static inline void validate_argument_count(size_t count, size_t expected, const char *message = nullptr) {
if (count != expected) {
throw std::invalid_argument(message ? message : "Invalid arguments");
}
}
static inline void validate_argument_count(size_t count, size_t min, size_t max, const char *message = nullptr) {
if (count < min || count > max) {
throw std::invalid_argument(message ? message : "Invalid arguments");
}
}
static inline void validate_argument_count_at_least(size_t count, size_t expected, const char *message = nullptr) {
if (count < expected) {
throw std::invalid_argument(message ? message : "Invalid arguments");
}
}
template<typename T, AggregateFunc func>
void compute_aggregate_on_collection(typename T::ContextType ctx, typename T::ObjectType this_object,
typename T::Arguments &args, typename T::ReturnValue &return_value) {
auto list = get_internal<typename T::Type, T>(this_object);
size_t column = 0;
if (list->get_type() == realm::PropertyType::Object) {
const ObjectSchema& object_schema = list->get_object_schema();
std::string property_name = T::Value::validated_to_string(ctx, args[0]);
const Property* property = object_schema.property_for_name(property_name);
if (!property) {
throw std::invalid_argument(util::format("Property '%1' does not exist on object '%2'",
property_name, object_schema.name));
}
column = property->table_column;
}
else {
args.validate_maximum(0);
}
util::Optional<Mixed> mixed;
switch (func) {
case AggregateFunc::Min:
return_value.set(list->min(column));
break;
case AggregateFunc::Max:
return_value.set(list->max(column));
break;
case AggregateFunc::Sum:
return_value.set(list->sum(column));
break;
case AggregateFunc::Avg:
return_value.set(list->average(column));
break;
}
}
} // js
} // realm