Skip to content

Any with registries

google.protobuf.Any stores an arbitrary message together with a type URL. If you know the expected schema, unpack directly. If you only learn the type URL at runtime, use a registry.

See Well-known types and Registries for the underlying APIs.

syntax = "proto3";
package example;
import "google/protobuf/any.proto";
message User {
string first_name = 1;
string last_name = 2;
}
message Event {
google.protobuf.Any payload = 1;
}
import { create } from "@bufbuild/protobuf";
import { anyPack } from "@bufbuild/protobuf/wkt";
import { EventSchema, UserSchema } from "./gen/example_pb";
const user = create(UserSchema, {
firstName: "Marge",
lastName: "Simpson",
});
const event = create(EventSchema, {
payload: anyPack(UserSchema, user),
});
import { anyIs, anyUnpack } from "@bufbuild/protobuf/wkt";
import { UserSchema } from "./gen/example_pb";
if (event.payload && anyIs(event.payload, UserSchema)) {
const user = anyUnpack(event.payload, UserSchema);
user?.firstName;
}
import { createRegistry } from "@bufbuild/protobuf";
import { anyUnpack } from "@bufbuild/protobuf/wkt";
import { UserSchema } from "./gen/example_pb";
const registry = createRegistry(UserSchema);
if (event.payload) {
const message = anyUnpack(event.payload, registry);
message?.$typeName;
}

Use the same registry when converting Any values to and from JSON. See Well-known types, Registries, and Serialization.