-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.hpp
More file actions
84 lines (69 loc) · 2.7 KB
/
Copy pathattribute.hpp
File metadata and controls
84 lines (69 loc) · 2.7 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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#pragma once
#include <cstddef>
#include "datadog/attribute.hpp"
#include "datadog/impl/core/json/primitives/string.hpp"
#include "datadog/impl/core/util/assert.hpp"
namespace datadog::impl {
/**
* Returns the number of bytes required to encode the given Attribute value in JSON
* format.
*/
size_t GetJsonSize(const Attribute& value);
/**
* Encodes the given Attribute value in JSON format.
*/
size_t WriteJson(char* dst, size_t n, const Attribute& value);
/**
* Special-case function for use in JSON struct serialization routines: encodes the
* given Attribute value as a JSON object, excluding any top-level properties for which
* `filter_func(name)` returns false.
*
* @param filter_func - A callable that takes a property name and returns true if
* it should be included in the serialized JSON object, or false if that name should be
* excluded due to a conflict with a standard property name.
*/
template <typename FilterFunc>
size_t WriteFilteredJsonObject(
char* dst, size_t n, const Attribute& value, FilterFunc filter_func
) {
char* ptr = dst;
char* const dst_end = dst + n;
// Open object literal
*ptr++ = '{';
// Write name:value pairs for each property, comma-delimited, serializing values
// recursively
size_t num_properties_written = 0;
const size_t num_properties = value.GetObjectPropertyCount();
for (int i = 0, num = static_cast<int>(num_properties); i < num; i++) {
// If we've been given a filter func, ensure that any properties with reserved names
// (i.e. those where filter_func(name) => false) are skipped
std::string_view property_name = value.GetObjectPropertyNameAt(i);
if constexpr (!std::is_same_v<FilterFunc, std::nullptr_t>) {
if (!filter_func(property_name)) {
continue;
}
}
// Prepend a comma for all values except the first
if (num_properties_written > 0) {
*ptr++ = ',';
}
// Write property name and value, colon-delimited
ptr += WriteJson(ptr, dst_end - ptr, property_name);
*ptr++ = ':';
ptr += WriteJson(ptr, dst_end - ptr, value.GetObjectPropertyValueAt(i));
num_properties_written++;
}
// Close object literal
*ptr++ = '}';
// Return total number of bytes written, which should have been less than or equal to
// the available space in the buffer
const size_t num_bytes_written = ptr - dst;
DATADOG_ASSERT(num_bytes_written <= n, "buffer overflow on object encode");
return num_bytes_written;
}
} // namespace datadog::impl