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
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