Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: Use JsonObject to represent google.protobuf.Struct #893

Merged
merged 3 commits into from
Jun 17, 2024

Conversation

timostamm
Copy link
Member

@timostamm timostamm commented Jun 14, 2024

The well-known message google.protobuf.Struct stores a dynamic object with the same range of types as a plain object in JSON. Unfortunately, it is cumbersome to construct a Struct message, and we recommend to use fromJson() to do so.

This PR changes the generated code to represent google.protobuf.Struct with JsonObject. For example:

syntax = "proto3";
import "google/protobuf/struct.proto";

message Example {
  google.protobuf.Struct struct = 1;
}

Generated code before:

import { Struct } from "@bufbuild/protobuf/wkt";

export type Example = Message<"Example"> & {
  /**
   * @generated from field: google.protobuf.Struct struct = 1;
   */
  struct?: Struct;
 
}

Generated code with this PR:

import { JsonObject } from "@bufbuild/protobuf";

export type Example = Message<"Example"> & {
  /**
   * @generated from field: google.protobuf.Struct struct = 1;
   */
  struct?: JsonObject;
}

JsonObject is defined as:

type JsonObject = { [k: string]: JsonValue };
type JsonValue =
  | number
  | string
  | boolean
  | null
  | JsonObject
  | JsonValue[];

The change applies to all fields, singular and repeated, map values, oneof members, and extensions. The generated messages Struct, Value, and ListValue from @bufbuild/protobuf/wkt are exempt, and remain unchanged.

Bundle size increases by 2%, performance does not regress.

@timostamm timostamm marked this pull request as ready for review June 17, 2024 09:44
@timostamm timostamm merged commit 1c17e1b into v2 Jun 17, 2024
7 checks passed
@timostamm timostamm deleted the tstamm/struct-as-json-object branch June 17, 2024 15:59
@timostamm timostamm mentioned this pull request Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants