A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
msgpack.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
22#ifndef A11_DATA_MSGPACK_H_
23#define A11_DATA_MSGPACK_H_
24
25#include <cstddef>
26#include <cstdint>
27#include <string>
28#include <string_view>
29
30#include <absl/base/nullability.h>
31#include <absl/functional/function_ref.h>
32#include <absl/status/status.h>
33#include <absl/status/statusor.h>
34#include <nlohmann/json.hpp>
35
36namespace a11::data {
37
45 public:
47 MsgpackWriter() : bytes_(&owned_) {}
48
55 explicit MsgpackWriter(std::string* absl_nonnull target) : bytes_(target) {}
56
57 // Copying or moving would invalidate bytes_ when it points to owned_.
58 MsgpackWriter(const MsgpackWriter&) = delete;
60
62 absl::Status Pack(const nlohmann::json& value);
63
64 // Typed field writers encode directly into the buffer and match Pack()'s
65 // MessagePack representation. MsgpackWriterEncodingTest pins equivalence.
66
68 void PackNil();
70 void PackBool(bool value);
72 void PackUint(std::uint64_t value);
74 void PackInt(std::int64_t value);
76 void PackString(std::string_view value);
78 void PackBinary(std::string_view value);
80 void PackArrayHeader(size_t length);
82 void PackMapHeader(size_t length);
83
91 absl::Status PackRecord(
92 absl::FunctionRef<absl::Status(MsgpackWriter* absl_nonnull)> encode);
93
95 [[nodiscard]] const std::string& bytes() const { return *bytes_; }
96
103 [[nodiscard]] std::string TakeBytes() { return std::move(*bytes_); }
104
105 private:
107 void Reserve(size_t extra);
108
109 std::string owned_;
110 std::string* absl_nonnull bytes_;
111};
112
115 public:
117 explicit MsgpackReader(std::string_view bytes) : bytes_(bytes) {}
118
120 absl::StatusOr<nlohmann::json> Read();
121
129 absl::StatusOr<std::string_view> ReadBinaryView();
130
138 absl::StatusOr<size_t> ReadArrayLength();
140 absl::Status EnsureFullyConsumed() const;
141
143 [[nodiscard]] size_t position() const { return position_; }
144
145 private:
146 std::string_view bytes_;
147 size_t position_ = 0;
148};
149
151absl::StatusOr<std::string> PackStatus(const absl::Status& status);
153absl::StatusOr<absl::Status> UnpackStatus(std::string_view bytes);
154
156nlohmann::json Binary(std::string_view bytes);
158absl::StatusOr<std::string> GetBinary(const nlohmann::json& value,
159 std::string_view field_name);
160
161} // namespace a11::data
162
163#endif // A11_DATA_MSGPACK_H_
Sequentially decode the fields of one A11 MessagePack wire record.
Definition msgpack.h:114
absl::StatusOr< nlohmann::json > Read()
Decode the next field and advance the byte cursor.
Definition msgpack.cc:441
absl::StatusOr< std::string_view > ReadBinaryView()
Read the next field as MessagePack binary without copying its payload.
Definition msgpack.cc:457
absl::StatusOr< size_t > ReadArrayLength()
Read the next field as a MessagePack array header, returning its length.
Definition msgpack.cc:489
MsgpackReader(std::string_view bytes)
Read from bytes, which must remain alive while this reader is used.
Definition msgpack.h:117
absl::Status EnsureFullyConsumed() const
Return an error if unread trailing bytes remain in the record.
Definition msgpack.cc:516
size_t position() const
Current byte offset, useful when diagnosing malformed peer data.
Definition msgpack.h:143
Incrementally encode the fields of one A11 wire record.
Definition msgpack.h:44
void PackInt(std::int64_t value)
Append a signed integer in the narrowest MessagePack form that fits.
Definition msgpack.cc:352
MsgpackWriter(const MsgpackWriter &)=delete
MsgpackWriter()
Encode into a buffer of this writer's own.
Definition msgpack.h:47
void PackBool(bool value)
Append a MessagePack boolean.
Definition msgpack.cc:330
void PackUint(std::uint64_t value)
Append an unsigned integer in the narrowest MessagePack form that fits.
Definition msgpack.cc:334
void PackString(std::string_view value)
Append a string field, copying value once.
Definition msgpack.cc:376
void PackArrayHeader(size_t length)
Append an array header; length element fields must follow.
Definition msgpack.cc:408
void PackBinary(std::string_view value)
Append a binary field, copying value once.
Definition msgpack.cc:393
void PackNil()
Append a MessagePack nil.
Definition msgpack.cc:326
absl::Status PackRecord(absl::FunctionRef< absl::Status(MsgpackWriter *absl_nonnull)> encode)
Append a nested record as one binary field, encoded by encode.
Definition msgpack.cc:432
MsgpackWriter(std::string *absl_nonnull target)
Encode into a caller-supplied buffer, appending to what is there.
Definition msgpack.h:55
std::string TakeBytes()
Transfer the encoded bytes out of this writer.
Definition msgpack.h:103
absl::Status Pack(const nlohmann::json &value)
Append a JSON-compatible field to the encoded record.
Definition msgpack.cc:313
MsgpackWriter & operator=(const MsgpackWriter &)=delete
const std::string & bytes() const
View the encoded bytes without transferring ownership.
Definition msgpack.h:95
void PackMapHeader(size_t length)
Append a map header; length key/value field pairs must follow.
Definition msgpack.cc:420
std::string value
Definition discover.cc:114
Definition json.cc:35
absl::StatusOr< std::string > GetBinary(const nlohmann::json &value, std::string_view field_name)
Read a binary JSON value, naming field_name in any validation error.
Definition msgpack.cc:528
absl::StatusOr< absl::Status > UnpackStatus(std::string_view bytes)
Decode a structured status without losing A11 status details.
Definition msgpack.cc:546
nlohmann::json Binary(std::string_view bytes)
Wrap arbitrary bytes as a MessagePack binary JSON value.
Definition msgpack.cc:523
absl::StatusOr< std::string > PackStatus(const absl::Status &status)
Encode a structured Abseil status for action and transport status fields.
Definition msgpack.cc:538
std::string target
Which editor definition syntax is about; every one when empty, which is the default.
Definition main.cc:74