Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.1 KiB

# μpb - a small protobuf implementation in C
6 years ago
|Platform|Build Status|
|--------|------------|
|macOS|[![Build Status](https://storage.googleapis.com/upb-kokoro-results/status-badge/macos.png)](https://fusion.corp.google.com/projectanalysis/summary/KOKORO/prod%3Aupb%2Fmacos%2Fcontinuous)|
|ubuntu|[![Build Status](https://storage.googleapis.com/upb-kokoro-results/status-badge/ubuntu.png)](https://fusion.corp.google.com/projectanalysis/summary/KOKORO/prod%3Aupb%2Fubuntu%2Fcontinuous)|
μpb (often written 'upb') is a small protobuf implementation written in C.
upb generates a C API for creating, parsing, and serializing messages
as declared in `.proto` files. upb is heavily arena-based -- all
messages always live in an arena (note: the arena can live in stack or
static memory if desired). Here is a simple example:
6 years ago
```c
#include "conformance/conformance.upb.h"
void foo(const char* data, size_t size) {
upb_arena *arena;
/* Generated message type. */
6 years ago
conformance_ConformanceRequest *request;
conformance_ConformanceResponse *response;
arena = upb_arena_new();
request = conformance_ConformanceRequest_parse(data, size, arena);
response = conformance_ConformanceResponse_new(arena);
switch (conformance_ConformanceRequest_payload_case(request)) {
case conformance_ConformanceRequest_payload_protobuf_payload: {
upb_strview payload = conformance_ConformanceRequest_protobuf_payload(request);
// ...
break;
}
case conformance_ConformanceRequest_payload_NOT_SET:
fprintf(stderr, "conformance_upb: Request didn't have payload.\n");
break;
default: {
static const char msg[] = "Unsupported input format.";
conformance_ConformanceResponse_set_skipped(
response, upb_strview_make(msg, sizeof(msg)));
break;
}
}
/* Frees all messages on the arena. */
6 years ago
upb_arena_free(arena);
}
```
API and ABI are both subject to change! Please do not distribute
as a shared library for this reason (for now at least).
6 years ago
## Using upb in your project
6 years ago
Currently only Bazel is supported (CMake support is partial and incomplete
but full CMake support is an eventual goal).
6 years ago
To use upb in your Bazel project, first add upb to your `WORKSPACE` file,
either as a `git_repository()` or as a `new_local_repository()` with a
Git Submodule:
6 years ago
```python
# Add this to your WORKSPACE file.
git_repository(
name = "upb",
remote = "https://github.com/protocolbuffers/upb.git",
# You may want to substitute a more recent commit here.
commit = "d5af87d06bbe3abad66970ce9c7ae0a7de8bb3c6",
)
```
6 years ago
Then in your BUILD file you can add `upb_proto_library()` rules that
generate code for a corresponding `proto_library()` rule. For
example:
6 years ago
```python
# Add this to your BUILD file.
load(":upb_proto_library.bzl", "upb_proto_library")
6 years ago
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
)
6 years ago
upb_proto_library(
name = "foo_upbproto",
deps = [":foo_proto"],
)
6 years ago
cc_library(
name = "lib_that_uses_protos",
srcs = ["lib_that_uses_protos.cc"],
deps = [":foo_upbproto"],
)
```
6 years ago
Then in your `.c` file you can #include the generated header:
6 years ago
```c
#include "foo.upb.h"
6 years ago
/* Insert code that uses generated types. */
```
6 years ago
## Old "handlers" interfaces
6 years ago
This library contains several semi-deprecated interfaces (see BUILD
file for more info about which interfaces are deprecated). These
deprecated interfaces are still used in some significant projects,
such as the Ruby and PHP C bindings for protobuf in the [main protobuf
repo](https://github.com/protocolbuffers/protobuf). The goal is to
migrate the Ruby/PHP bindings to use the newer, simpler interfaces
instead. Please do not use the old interfaces in new code.
6 years ago
## Lua bindings
6 years ago
This repo has some Lua bindings for the core library. These are
experimental and very incomplete. These are currently included in
order to validate that the C API is suitable for wrapping. As the
project matures these Lua bindings may become publicly available.
## Contact
8 years ago
Author: Josh Haberman ([jhaberman@gmail.com](mailto:jhaberman@gmail.com),
[haberman@google.com](mailto:haberman@google.com))