Thalweg · API reference ← Main docs

Crate twg_proto_flatten

Crate twg_proto_flatten 

Source
Expand description

Flattens a FileDescriptorSet into a single self-contained DescriptorProto compatible with a shallow, nested_type-only message registry (the shape built by, for example, a MessageRegistry::from_descriptor that only recurses nested_type).

The algorithm closes over TOP-LEVEL ancestors only, grafts each one once (preserving its own internal nesting), and rewrites references via longest-prefix substitution - which matters for a reference into a deeply-nested sibling type, where only the ancestor’s prefix needs rewriting, not the whole path.

§Why

The stock registry only recurses nested_type, keying entries as .{RootName} / .{RootName}.{Nested} with no package prefix, and the parser resolves message fields by looking up FieldDescriptorProto.type_name verbatim. Any field referencing a sibling top-level message or an imported type (e.g. .google.protobuf.StringValue) therefore fails at decode time with an unknown-type error the first time the field is populated.

§What this does

Given a FileDescriptorSet compiled with protoc --include_imports (or protox’s equivalent) and the fully-qualified name of a TOP-LEVEL root message, flatten_for_registry:

  1. Computes the transitive closure of top-level message/enum ancestors reachable from the root (cycle-safe - handles google.protobuf.Struct/Value/ListValue, which reference each other).
  2. Grafts every external top-level message/enum into the root’s nested_type/enum_type under a collision-safe mangled name (.google.protobuf.StringValue -> google_protobuf_StringValue).
  3. Rewrites every type_name in the resulting tree using the longest matching prefix substitution, so a reference into a NESTED member of a grafted ancestor keeps its relative path (.other.pkg.Foo.Bar -> .Root.other_pkg_Foo.Bar, not a flat .Root.other_pkg_Foo_Bar).

The protobuf wire format never carries type names, so this transform is invisible to producers: bytes on the wire decode identically.

Enums§

FlattenError

Functions§

flatten_for_registry
Flattens fds into a single self-contained DescriptorProto rooted at root_fqn, grafting the transitive closure of top-level ancestors and rewriting type references so a shallow, nested_type-only registry can resolve them. See the module documentation for the algorithm.