C/C++ API Reference
Loading...
Searching...
No Matches
map_utils.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14//
15// The header provides a set of helper utils for protobuf related operations.
16// The APIs may not be finalized yet.
17
18#pragma once
19
20#include <cstddef>
21#include <string_view>
22
23#include "pw_assert/check.h"
24#include "pw_protobuf/stream_decoder.h"
25#include "pw_status/status.h"
26#include "pw_status/try.h"
27#include "pw_stream/stream.h"
28
29namespace pw::protobuf {
30
32
33// The function writes an entry for the proto map<string, bytes> field type.
34//
35// Args:
36// field_number - The field number for the map.
37// key - The string payload for the key value of the entry.
38// key_size - Number of bytes in the key.
39// value - The value payload for the entry.
40// value_size - Number of bytes in the value.
41// stream_pipe_buffer - A non-zero size buffer for the function to read and
42// store data from the reader and write to the given writer.
43// writer - The output writer to write to.
44//
45// Returns:
46// OK - Entry is successfully written.
47// RESOURCE_EXHAUSTED - Entry would exceed write limit.
48// INVALID_ARGUMENT - Field number is invalid.
49//
50// Since all length-delimited fields can be treated as `bytes`,
51// it can be used to write any string to length-delimited field map, i.e.
52// map<string, message>, map<string, bytes> etc.
53Status WriteProtoStringToBytesMapEntry(uint32_t field_number,
54 stream::Reader& key,
55 size_t key_size,
56 stream::Reader& value,
57 size_t value_size,
58 ByteSpan stream_pipe_buffer,
59 stream::Writer& writer);
60
62
63} // namespace pw::protobuf