From b067cf07debd4ecfedbb896d4db87fca8c8f5a65 Mon Sep 17 00:00:00 2001 From: Tate Thurston Date: Tue, 24 Oct 2023 20:18:18 -0700 Subject: [PATCH] initialize now accepts partial messages --- CHANGELOG.md | 4 ++ README.md | 46 ++++++++-------- .../proto/conformance/conformance.pb.ts | 28 +++++++--- .../protobuf/test_messages_proto3.pb.ts | 50 ++++++++++++----- e2e/serialization/json.pb.ts | 28 +++++++--- e2e/serialization/message.pb.ts | 18 ++++--- e2e/treeshaking/treeshaking.pb.ts | 12 +++-- .../closure-compiler/src/haberdasher.pb.ts | 12 +++-- examples/javascript/src/haberdasher.pb.js | 12 +++-- examples/protoc/src/haberdasher.pb.js | 12 +++-- examples/typescript/src/haberdasher.pb.ts | 6 ++- examples/typescript/src/size.pb.ts | 6 ++- examples/typescript/src/user.pb.ts | 6 ++- .../src/codegen/autogenerate/index.ts | 18 ++++--- .../src/runtime/well-known-types/any.pb.ts | 6 ++- .../src/runtime/well-known-types/api.pb.ts | 18 ++++--- .../runtime/well-known-types/duration.pb.ts | 6 ++- .../src/runtime/well-known-types/empty.pb.ts | 12 +++-- .../runtime/well-known-types/field_mask.pb.ts | 6 ++- .../well-known-types/source_context.pb.ts | 6 ++- .../src/runtime/well-known-types/struct.pb.ts | 18 ++++--- .../runtime/well-known-types/timestamp.pb.ts | 6 ++- .../src/runtime/well-known-types/type.pb.ts | 30 +++++++---- .../runtime/well-known-types/wrappers.pb.ts | 54 ++++++++++++------- 24 files changed, 275 insertions(+), 145 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba2c3f2..aba159d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - Fix JSON serializtion for Timestamp and Duration well known types. See [#39](https://github.com/tatethurston/ProtoScript/issues/39). - Accept all value permutations as described by the Proto3 JSON spec when parsing JSON messages. +- #initialize now accepts partial messages. This enables you to create a full message with select fields set to a user-provided value: + ```ts + const ron = User.initialize({ firstName: "Ron" }); + ``` ## v0.0.18 diff --git a/README.md b/README.md index 4d12a37..27502ed 100644 --- a/README.md +++ b/README.md @@ -76,44 +76,40 @@ Using the `User` generated by ProtoScript will look like this: ```typescript import { User, UserJSON } from "./user.pb.js"; -// protocol buffers -const bytes = User.encode({ - firstName: "Homer", - lastName: "Simpson", +const user = { + firstName: "Harry", + lastName: "Potter", active: true, - locations: ["Springfield"], - projects: { SPP: "Springfield Power Plant" }, + locations: ["Hogwarts"], + projects: { DA: "Dumbledore's Army }, manager: { - firstName: "Montgomery", - lastName: "Burns", - }, -}); -console.log(bytes); + firstName: "Albus", + lastName: "Dumbledore", + } +} + +// protocol buffers +const bytes = User.encode(user); +console.log(bytes); // Uint8Array const userFromBytes = User.decode(bytes); -console.log(userFromBytes); +console.log(userFromBytes); // our user above // json -const json = UserJSON.encode({ - firstName: "Homer", - lastName: "Simpson", - active: true, - locations: ["Springfield"], - projects: { SPP: "Springfield Power Plant" }, - manager: { - firstName: "Montgomery", - lastName: "Burns", - }, -}); -console.log(json); +const json = UserJSON.encode(user) +console.log(json); // json string const userFromJSON = UserJSON.decode(json); -console.log(userFromJSON); +console.log(userFromJSON); // our user above // ProtoScript generates and consumes plain JavaScript objects (POJOs) over classes. If you want to generate a full message // with default fields, you can use the #initialize method on the generated message class: const user = User.initialize(); console.log(user); + +// the generated object is just a POJO. You can encode any POJO as protobuf or json as long as it conforms to a subset of the message interface defined in your .proto: +console.log(User.encode(user)) +console.log(UserJSON.encode(user)) ``` ## Installation 📦 diff --git a/e2e/conformance/proto/conformance/conformance.pb.ts b/e2e/conformance/proto/conformance/conformance.pb.ts index 847ca28..659a95a 100644 --- a/e2e/conformance/proto/conformance/conformance.pb.ts +++ b/e2e/conformance/proto/conformance/conformance.pb.ts @@ -313,9 +313,10 @@ export const FailureSet = { /** * Initializes FailureSet with all fields set to their default value. */ - initialize: function (): FailureSet { + initialize: function (msg?: Partial): FailureSet { return { failure: [], + ...msg, }; }, @@ -380,7 +381,7 @@ export const ConformanceRequest = { /** * Initializes ConformanceRequest with all fields set to their default value. */ - initialize: function (): ConformanceRequest { + initialize: function (msg?: Partial): ConformanceRequest { return { protobufPayload: undefined, jsonPayload: undefined, @@ -391,6 +392,7 @@ export const ConformanceRequest = { testCategory: TestCategory._fromInt(0), jspbEncodingOptions: JspbEncodingConfig.initialize(), printUnknownFields: false, + ...msg, }; }, @@ -521,7 +523,9 @@ export const ConformanceResponse = { /** * Initializes ConformanceResponse with all fields set to their default value. */ - initialize: function (): ConformanceResponse { + initialize: function ( + msg?: Partial, + ): ConformanceResponse { return { parseError: undefined, serializeError: undefined, @@ -532,6 +536,7 @@ export const ConformanceResponse = { skipped: undefined, jspbPayload: undefined, textPayload: undefined, + ...msg, }; }, @@ -652,9 +657,10 @@ export const JspbEncodingConfig = { /** * Initializes JspbEncodingConfig with all fields set to their default value. */ - initialize: function (): JspbEncodingConfig { + initialize: function (msg?: Partial): JspbEncodingConfig { return { useJspbArrayAnyFormat: false, + ...msg, }; }, @@ -862,9 +868,10 @@ export const FailureSetJSON = { /** * Initializes FailureSet with all fields set to their default value. */ - initialize: function (): FailureSet { + initialize: function (msg?: Partial): FailureSet { return { failure: [], + ...msg, }; }, @@ -914,7 +921,7 @@ export const ConformanceRequestJSON = { /** * Initializes ConformanceRequest with all fields set to their default value. */ - initialize: function (): ConformanceRequest { + initialize: function (msg?: Partial): ConformanceRequest { return { protobufPayload: undefined, jsonPayload: undefined, @@ -925,6 +932,7 @@ export const ConformanceRequestJSON = { testCategory: TestCategory._fromInt(0), jspbEncodingOptions: JspbEncodingConfigJSON.initialize(), printUnknownFields: false, + ...msg, }; }, @@ -1048,7 +1056,9 @@ export const ConformanceResponseJSON = { /** * Initializes ConformanceResponse with all fields set to their default value. */ - initialize: function (): ConformanceResponse { + initialize: function ( + msg?: Partial, + ): ConformanceResponse { return { parseError: undefined, serializeError: undefined, @@ -1059,6 +1069,7 @@ export const ConformanceResponseJSON = { skipped: undefined, jspbPayload: undefined, textPayload: undefined, + ...msg, }; }, @@ -1168,9 +1179,10 @@ export const JspbEncodingConfigJSON = { /** * Initializes JspbEncodingConfig with all fields set to their default value. */ - initialize: function (): JspbEncodingConfig { + initialize: function (msg?: Partial): JspbEncodingConfig { return { useJspbArrayAnyFormat: false, + ...msg, }; }, diff --git a/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts b/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts index 9537905..7357388 100644 --- a/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts +++ b/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts @@ -451,7 +451,7 @@ export const TestAllTypesProto3 = { /** * Initializes TestAllTypesProto3 with all fields set to their default value. */ - initialize: function (): TestAllTypesProto3 { + initialize: function (msg?: Partial): TestAllTypesProto3 { return { optionalInt32: 0, optionalInt64: 0n, @@ -604,6 +604,7 @@ export const TestAllTypesProto3 = { fieldName16: 0, fieldName17: 0, FieldName18: 0, + ...msg, }; }, @@ -2602,10 +2603,13 @@ export const TestAllTypesProto3 = { /** * Initializes TestAllTypesProto3.NestedMessage with all fields set to their default value. */ - initialize: function (): TestAllTypesProto3.NestedMessage { + initialize: function ( + msg?: Partial, + ): TestAllTypesProto3.NestedMessage { return { a: 0, corecursive: undefined, + ...msg, }; }, @@ -3552,9 +3556,10 @@ export const ForeignMessage = { /** * Initializes ForeignMessage with all fields set to their default value. */ - initialize: function (): ForeignMessage { + initialize: function (msg?: Partial): ForeignMessage { return { c: 0, + ...msg, }; }, @@ -3613,8 +3618,12 @@ export const NullHypothesisProto3 = { /** * Initializes NullHypothesisProto3 with all fields set to their default value. */ - initialize: function (): NullHypothesisProto3 { - return {}; + initialize: function ( + msg?: Partial, + ): NullHypothesisProto3 { + return { + ...msg, + }; }, /** @@ -3656,8 +3665,10 @@ export const EnumOnlyProto3 = { /** * Initializes EnumOnlyProto3 with all fields set to their default value. */ - initialize: function (): EnumOnlyProto3 { - return {}; + initialize: function (msg?: Partial): EnumOnlyProto3 { + return { + ...msg, + }; }, /** @@ -3791,7 +3802,7 @@ export const TestAllTypesProto3JSON = { /** * Initializes TestAllTypesProto3 with all fields set to their default value. */ - initialize: function (): TestAllTypesProto3 { + initialize: function (msg?: Partial): TestAllTypesProto3 { return { optionalInt32: 0, optionalInt64: 0n, @@ -3944,6 +3955,7 @@ export const TestAllTypesProto3JSON = { fieldName16: 0, fieldName17: 0, FieldName18: 0, + ...msg, }; }, @@ -5739,10 +5751,13 @@ export const TestAllTypesProto3JSON = { /** * Initializes TestAllTypesProto3.NestedMessage with all fields set to their default value. */ - initialize: function (): TestAllTypesProto3.NestedMessage { + initialize: function ( + msg?: Partial, + ): TestAllTypesProto3.NestedMessage { return { a: 0, corecursive: undefined, + ...msg, }; }, @@ -6501,9 +6516,10 @@ export const ForeignMessageJSON = { /** * Initializes ForeignMessage with all fields set to their default value. */ - initialize: function (): ForeignMessage { + initialize: function (msg?: Partial): ForeignMessage { return { c: 0, + ...msg, }; }, @@ -6550,8 +6566,12 @@ export const NullHypothesisProto3JSON = { /** * Initializes NullHypothesisProto3 with all fields set to their default value. */ - initialize: function (): NullHypothesisProto3 { - return {}; + initialize: function ( + msg?: Partial, + ): NullHypothesisProto3 { + return { + ...msg, + }; }, /** @@ -6592,8 +6612,10 @@ export const EnumOnlyProto3JSON = { /** * Initializes EnumOnlyProto3 with all fields set to their default value. */ - initialize: function (): EnumOnlyProto3 { - return {}; + initialize: function (msg?: Partial): EnumOnlyProto3 { + return { + ...msg, + }; }, /** diff --git a/e2e/serialization/json.pb.ts b/e2e/serialization/json.pb.ts index 2df9d27..5b1d1da 100644 --- a/e2e/serialization/json.pb.ts +++ b/e2e/serialization/json.pb.ts @@ -147,10 +147,11 @@ export const Message = { /** * Initializes Message with all fields set to their default value. */ - initialize: function (): Message { + initialize: function (msg?: Partial): Message { return { fooBar: "", g: 0n, + ...msg, }; }, @@ -222,7 +223,7 @@ export const SampleMessage = { /** * Initializes SampleMessage with all fields set to their default value. */ - initialize: function (): SampleMessage { + initialize: function (msg?: Partial): SampleMessage { return { sampleMessage: Message.initialize(), sampleEnum: Enum._fromInt(0), @@ -242,6 +243,7 @@ export const SampleMessage = { sampleTimestamp: protoscript.Timestamp.initialize(), sampleDuration: protoscript.Duration.initialize(), sampleStruct: protoscript.Struct.initialize(), + ...msg, }; }, @@ -497,10 +499,11 @@ export const OptionalMessage = { /** * Initializes OptionalMessage with all fields set to their default value. */ - initialize: function (): OptionalMessage { + initialize: function (msg?: Partial): OptionalMessage { return { fooBar: undefined, g: undefined, + ...msg, }; }, @@ -572,7 +575,9 @@ export const OptionalSampleMessage = { /** * Initializes OptionalSampleMessage with all fields set to their default value. */ - initialize: function (): OptionalSampleMessage { + initialize: function ( + msg?: Partial, + ): OptionalSampleMessage { return { sampleMessage: undefined, sampleEnum: undefined, @@ -592,6 +597,7 @@ export const OptionalSampleMessage = { sampleTimestamp: undefined, sampleDuration: undefined, sampleStruct: undefined, + ...msg, }; }, @@ -888,10 +894,11 @@ export const MessageJSON = { /** * Initializes Message with all fields set to their default value. */ - initialize: function (): Message { + initialize: function (msg?: Partial): Message { return { fooBar: "", g: 0n, + ...msg, }; }, @@ -946,7 +953,7 @@ export const SampleMessageJSON = { /** * Initializes SampleMessage with all fields set to their default value. */ - initialize: function (): SampleMessage { + initialize: function (msg?: Partial): SampleMessage { return { sampleMessage: MessageJSON.initialize(), sampleEnum: Enum._fromInt(0), @@ -966,6 +973,7 @@ export const SampleMessageJSON = { sampleTimestamp: protoscript.TimestampJSON.initialize(), sampleDuration: protoscript.DurationJSON.initialize(), sampleStruct: protoscript.StructJSON.initialize(), + ...msg, }; }, @@ -1204,10 +1212,11 @@ export const OptionalMessageJSON = { /** * Initializes OptionalMessage with all fields set to their default value. */ - initialize: function (): OptionalMessage { + initialize: function (msg?: Partial): OptionalMessage { return { fooBar: undefined, g: undefined, + ...msg, }; }, @@ -1264,7 +1273,9 @@ export const OptionalSampleMessageJSON = { /** * Initializes OptionalSampleMessage with all fields set to their default value. */ - initialize: function (): OptionalSampleMessage { + initialize: function ( + msg?: Partial, + ): OptionalSampleMessage { return { sampleMessage: undefined, sampleEnum: undefined, @@ -1284,6 +1295,7 @@ export const OptionalSampleMessageJSON = { sampleTimestamp: undefined, sampleDuration: undefined, sampleStruct: undefined, + ...msg, }; }, diff --git a/e2e/serialization/message.pb.ts b/e2e/serialization/message.pb.ts index 8bbebe7..6092cdf 100644 --- a/e2e/serialization/message.pb.ts +++ b/e2e/serialization/message.pb.ts @@ -129,7 +129,7 @@ export const Foo = { /** * Initializes Foo with all fields set to their default value. */ - initialize: function (): Foo { + initialize: function (msg?: Partial): Foo { return { fieldOne: undefined, fieldTwo: {}, @@ -146,6 +146,7 @@ export const Foo = { fieldThirteen: undefined, fieldFourteen: undefined, fieldFifteen: [], + ...msg, }; }, @@ -336,11 +337,12 @@ export const Foo = { /** * Initializes Foo.FooBar with all fields set to their default value. */ - initialize: function (): Foo.FooBar { + initialize: function (msg?: Partial): Foo.FooBar { return { fieldOne: "", fieldTwo: {}, fieldThree: [], + ...msg, }; }, @@ -524,11 +526,12 @@ export const Bar = { /** * Initializes Bar with all fields set to their default value. */ - initialize: function (): Bar { + initialize: function (msg?: Partial): Bar { return { fieldOne: "", fieldTwo: {}, fieldThree: [], + ...msg, }; }, @@ -699,7 +702,7 @@ export const FooJSON = { /** * Initializes Foo with all fields set to their default value. */ - initialize: function (): Foo { + initialize: function (msg?: Partial): Foo { return { fieldOne: undefined, fieldTwo: {}, @@ -716,6 +719,7 @@ export const FooJSON = { fieldThirteen: undefined, fieldFourteen: undefined, fieldFifteen: [], + ...msg, }; }, @@ -895,11 +899,12 @@ export const FooJSON = { /** * Initializes Foo.FooBar with all fields set to their default value. */ - initialize: function (): Foo.FooBar { + initialize: function (msg?: Partial): Foo.FooBar { return { fieldOne: "", fieldTwo: {}, fieldThree: [], + ...msg, }; }, @@ -1046,11 +1051,12 @@ export const BarJSON = { /** * Initializes Bar with all fields set to their default value. */ - initialize: function (): Bar { + initialize: function (msg?: Partial): Bar { return { fieldOne: "", fieldTwo: {}, fieldThree: [], + ...msg, }; }, diff --git a/e2e/treeshaking/treeshaking.pb.ts b/e2e/treeshaking/treeshaking.pb.ts index b10bae1..a05c192 100644 --- a/e2e/treeshaking/treeshaking.pb.ts +++ b/e2e/treeshaking/treeshaking.pb.ts @@ -58,7 +58,7 @@ export const TreeshakingTest = { /** * Initializes TreeshakingTest with all fields set to their default value. */ - initialize: function (): TreeshakingTest { + initialize: function (msg?: Partial): TreeshakingTest { return { stringField: "", repeatedStringField: [], @@ -67,6 +67,7 @@ export const TreeshakingTest = { optionalMessageField: undefined, timestampField: protoscript.Timestamp.initialize(), mapField: {}, + ...msg, }; }, @@ -248,9 +249,10 @@ export const NestedMessage = { /** * Initializes NestedMessage with all fields set to their default value. */ - initialize: function (): NestedMessage { + initialize: function (msg?: Partial): NestedMessage { return { stringField: undefined, + ...msg, }; }, @@ -316,7 +318,7 @@ export const TreeshakingTestJSON = { /** * Initializes TreeshakingTest with all fields set to their default value. */ - initialize: function (): TreeshakingTest { + initialize: function (msg?: Partial): TreeshakingTest { return { stringField: "", repeatedStringField: [], @@ -325,6 +327,7 @@ export const TreeshakingTestJSON = { optionalMessageField: undefined, timestampField: protoscript.TimestampJSON.initialize(), mapField: {}, + ...msg, }; }, @@ -487,9 +490,10 @@ export const NestedMessageJSON = { /** * Initializes NestedMessage with all fields set to their default value. */ - initialize: function (): NestedMessage { + initialize: function (msg?: Partial): NestedMessage { return { stringField: undefined, + ...msg, }; }, diff --git a/examples/closure-compiler/src/haberdasher.pb.ts b/examples/closure-compiler/src/haberdasher.pb.ts index 4512ec5..5f4ddd6 100644 --- a/examples/closure-compiler/src/haberdasher.pb.ts +++ b/examples/closure-compiler/src/haberdasher.pb.ts @@ -62,9 +62,10 @@ export const Size = { /** * Initializes Size with all fields set to their default value. */ - initialize: function (): Size { + initialize: function (msg?: Partial): Size { return { inches: 0, + ...msg, }; }, @@ -126,11 +127,12 @@ export const Hat = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function (): Hat { + initialize: function (msg?: Partial): Hat { return { inches: 0, color: "", name: "", + ...msg, }; }, @@ -204,9 +206,10 @@ export const SizeJSON = { /** * Initializes Size with all fields set to their default value. */ - initialize: function (): Size { + initialize: function (msg?: Partial): Size { return { inches: 0, + ...msg, }; }, @@ -251,11 +254,12 @@ export const HatJSON = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function (): Hat { + initialize: function (msg?: Partial): Hat { return { inches: 0, color: "", name: "", + ...msg, }; }, diff --git a/examples/javascript/src/haberdasher.pb.js b/examples/javascript/src/haberdasher.pb.js index 9eecc67..dfce397 100644 --- a/examples/javascript/src/haberdasher.pb.js +++ b/examples/javascript/src/haberdasher.pb.js @@ -32,9 +32,10 @@ export const Size = { /** * Initializes Size with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, + ...msg, }; }, @@ -93,11 +94,12 @@ export const Hat = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, color: "", name: "", + ...msg, }; }, @@ -168,9 +170,10 @@ export const SizeJSON = { /** * Initializes Size with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, + ...msg, }; }, @@ -215,11 +218,12 @@ export const HatJSON = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, color: "", name: "", + ...msg, }; }, diff --git a/examples/protoc/src/haberdasher.pb.js b/examples/protoc/src/haberdasher.pb.js index 9eecc67..dfce397 100644 --- a/examples/protoc/src/haberdasher.pb.js +++ b/examples/protoc/src/haberdasher.pb.js @@ -32,9 +32,10 @@ export const Size = { /** * Initializes Size with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, + ...msg, }; }, @@ -93,11 +94,12 @@ export const Hat = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, color: "", name: "", + ...msg, }; }, @@ -168,9 +170,10 @@ export const SizeJSON = { /** * Initializes Size with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, + ...msg, }; }, @@ -215,11 +218,12 @@ export const HatJSON = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function () { + initialize: function (msg) { return { inches: 0, color: "", name: "", + ...msg, }; }, diff --git a/examples/typescript/src/haberdasher.pb.ts b/examples/typescript/src/haberdasher.pb.ts index 0a8b61e..009c94f 100644 --- a/examples/typescript/src/haberdasher.pb.ts +++ b/examples/typescript/src/haberdasher.pb.ts @@ -54,11 +54,12 @@ export const Hat = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function (): Hat { + initialize: function (msg?: Partial): Hat { return { size: srcSize.Size.initialize(), color: "", name: "", + ...msg, }; }, @@ -132,11 +133,12 @@ export const HatJSON = { /** * Initializes Hat with all fields set to their default value. */ - initialize: function (): Hat { + initialize: function (msg?: Partial): Hat { return { size: srcSize.SizeJSON.initialize(), color: "", name: "", + ...msg, }; }, diff --git a/examples/typescript/src/size.pb.ts b/examples/typescript/src/size.pb.ts index 7c6b3fc..02412a3 100644 --- a/examples/typescript/src/size.pb.ts +++ b/examples/typescript/src/size.pb.ts @@ -47,9 +47,10 @@ export const Size = { /** * Initializes Size with all fields set to their default value. */ - initialize: function (): Size { + initialize: function (msg?: Partial): Size { return { inches: 0, + ...msg, }; }, @@ -109,9 +110,10 @@ export const SizeJSON = { /** * Initializes Size with all fields set to their default value. */ - initialize: function (): Size { + initialize: function (msg?: Partial): Size { return { inches: 0, + ...msg, }; }, diff --git a/examples/typescript/src/user.pb.ts b/examples/typescript/src/user.pb.ts index 3a7d1fe..f036263 100644 --- a/examples/typescript/src/user.pb.ts +++ b/examples/typescript/src/user.pb.ts @@ -53,7 +53,7 @@ export const User = { /** * Initializes User with all fields set to their default value. */ - initialize: function (): User { + initialize: function (msg?: Partial): User { return { firstName: "", lastName: "", @@ -61,6 +61,7 @@ export const User = { manager: undefined, locations: [], projects: {}, + ...msg, }; }, @@ -210,7 +211,7 @@ export const UserJSON = { /** * Initializes User with all fields set to their default value. */ - initialize: function (): User { + initialize: function (msg?: Partial): User { return { firstName: "", lastName: "", @@ -218,6 +219,7 @@ export const UserJSON = { manager: undefined, locations: [], projects: {}, + ...msg, }; }, diff --git a/packages/protoscript/src/codegen/autogenerate/index.ts b/packages/protoscript/src/codegen/autogenerate/index.ts index 26658fe..79fb340 100644 --- a/packages/protoscript/src/codegen/autogenerate/index.ts +++ b/packages/protoscript/src/codegen/autogenerate/index.ts @@ -149,9 +149,9 @@ function writeProtobufSerializers( node.content.namespacedName } with all fields set to their default value. */ - initialize: function()${printIfTypescript( - `: ${node.content.namespacedName}`, - )} { + initialize: function(msg${printIfTypescript( + `?: Partial<${node.content.namespacedName}>`, + )}) ${printIfTypescript(`:${node.content.namespacedName}`)} { return { ${node.content.fields .map((field) => { @@ -175,7 +175,8 @@ function writeProtobufSerializers( return `${field.name}: ${field.defaultValue},`; } }) - .join("")} + .join("\n")} + ...msg, };`; result += "},\n\n"; } @@ -488,9 +489,9 @@ function writeJSONSerializers(types: ProtoTypes[], parents: string[]): string { node.content.namespacedName } with all fields set to their default value. */ - initialize: function()${printIfTypescript( - `: ${node.content.namespacedName}`, - )} { + initialize: function(msg${printIfTypescript( + `?: Partial<${node.content.namespacedName}>`, + )}) ${printIfTypescript(`:${node.content.namespacedName}`)} { return { ${node.content.fields .map((field) => { @@ -514,7 +515,8 @@ function writeJSONSerializers(types: ProtoTypes[], parents: string[]): string { return `${field.name}: ${field.defaultValue},`; } }) - .join("")} + .join("\n")} + ...msg };`; result += "},\n\n"; } diff --git a/packages/protoscript/src/runtime/well-known-types/any.pb.ts b/packages/protoscript/src/runtime/well-known-types/any.pb.ts index 99184e7..493e548 100644 --- a/packages/protoscript/src/runtime/well-known-types/any.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/any.pb.ts @@ -164,10 +164,11 @@ export const Any = { /** * Initializes Any with all fields set to their default value. */ - initialize: function (): Any { + initialize: function (msg?: Partial): Any { return { typeUrl: "", value: new Uint8Array(), + ...msg, }; }, @@ -234,10 +235,11 @@ export const AnyJSON = { /** * Initializes Any with all fields set to their default value. */ - initialize: function (): Any { + initialize: function (msg?: Partial): Any { return { typeUrl: "", value: new Uint8Array(), + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/api.pb.ts b/packages/protoscript/src/runtime/well-known-types/api.pb.ts index a1dec96..d7a20b7 100644 --- a/packages/protoscript/src/runtime/well-known-types/api.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/api.pb.ts @@ -226,7 +226,7 @@ export const Api = { /** * Initializes Api with all fields set to their default value. */ - initialize: function (): Api { + initialize: function (msg?: Partial): Api { return { name: "", methods: [], @@ -235,6 +235,7 @@ export const Api = { sourceContext: protoscript.SourceContext.initialize(), mixins: [], syntax: protoscript.Syntax._fromInt(0), + ...msg, }; }, @@ -355,7 +356,7 @@ export const Method = { /** * Initializes Method with all fields set to their default value. */ - initialize: function (): Method { + initialize: function (msg?: Partial): Method { return { name: "", requestTypeUrl: "", @@ -364,6 +365,7 @@ export const Method = { responseStreaming: false, options: [], syntax: protoscript.Syntax._fromInt(0), + ...msg, }; }, @@ -476,10 +478,11 @@ export const Mixin = { /** * Initializes Mixin with all fields set to their default value. */ - initialize: function (): Mixin { + initialize: function (msg?: Partial): Mixin { return { name: "", root: "", + ...msg, }; }, @@ -546,7 +549,7 @@ export const ApiJSON = { /** * Initializes Api with all fields set to their default value. */ - initialize: function (): Api { + initialize: function (msg?: Partial): Api { return { name: "", methods: [], @@ -555,6 +558,7 @@ export const ApiJSON = { sourceContext: protoscript.SourceContextJSON.initialize(), mixins: [], syntax: protoscript.Syntax._fromInt(0), + ...msg, }; }, @@ -661,7 +665,7 @@ export const MethodJSON = { /** * Initializes Method with all fields set to their default value. */ - initialize: function (): Method { + initialize: function (msg?: Partial): Method { return { name: "", requestTypeUrl: "", @@ -670,6 +674,7 @@ export const MethodJSON = { responseStreaming: false, options: [], syntax: protoscript.Syntax._fromInt(0), + ...msg, }; }, @@ -763,10 +768,11 @@ export const MixinJSON = { /** * Initializes Mixin with all fields set to their default value. */ - initialize: function (): Mixin { + initialize: function (msg?: Partial): Mixin { return { name: "", root: "", + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/duration.pb.ts b/packages/protoscript/src/runtime/well-known-types/duration.pb.ts index 1c861a0..936ad14 100644 --- a/packages/protoscript/src/runtime/well-known-types/duration.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/duration.pb.ts @@ -116,10 +116,11 @@ export const Duration = { /** * Initializes Duration with all fields set to their default value. */ - initialize: function (): Duration { + initialize: function (msg?: Partial): Duration { return { seconds: 0n, nanos: 0, + ...msg, }; }, @@ -192,10 +193,11 @@ export const DurationJSON = { /** * Initializes Duration with all fields set to their default value. */ - initialize: function (): Duration { + initialize: function (msg?: Partial): Duration { return { seconds: 0n, nanos: 0, + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/empty.pb.ts b/packages/protoscript/src/runtime/well-known-types/empty.pb.ts index 989040c..3c23265 100644 --- a/packages/protoscript/src/runtime/well-known-types/empty.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/empty.pb.ts @@ -43,8 +43,10 @@ export const Empty = { /** * Initializes Empty with all fields set to their default value. */ - initialize: function (): Empty { - return {}; + initialize: function (msg?: Partial): Empty { + return { + ...msg, + }; }, /** @@ -90,8 +92,10 @@ export const EmptyJSON = { /** * Initializes Empty with all fields set to their default value. */ - initialize: function (): Empty { - return {}; + initialize: function (msg?: Partial): Empty { + return { + ...msg, + }; }, /** diff --git a/packages/protoscript/src/runtime/well-known-types/field_mask.pb.ts b/packages/protoscript/src/runtime/well-known-types/field_mask.pb.ts index 4abe5cf..96d4d97 100644 --- a/packages/protoscript/src/runtime/well-known-types/field_mask.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/field_mask.pb.ts @@ -245,9 +245,10 @@ export const FieldMask = { /** * Initializes FieldMask with all fields set to their default value. */ - initialize: function (): FieldMask { + initialize: function (msg?: Partial): FieldMask { return { paths: [], + ...msg, }; }, @@ -313,9 +314,10 @@ export const FieldMaskJSON = { /** * Initializes FieldMask with all fields set to their default value. */ - initialize: function (): FieldMask { + initialize: function (msg?: Partial): FieldMask { return { paths: [], + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/source_context.pb.ts b/packages/protoscript/src/runtime/well-known-types/source_context.pb.ts index a05dca1..11381fc 100644 --- a/packages/protoscript/src/runtime/well-known-types/source_context.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/source_context.pb.ts @@ -49,9 +49,10 @@ export const SourceContext = { /** * Initializes SourceContext with all fields set to their default value. */ - initialize: function (): SourceContext { + initialize: function (msg?: Partial): SourceContext { return { fileName: "", + ...msg, }; }, @@ -117,9 +118,10 @@ export const SourceContextJSON = { /** * Initializes SourceContext with all fields set to their default value. */ - initialize: function (): SourceContext { + initialize: function (msg?: Partial): SourceContext { return { fileName: "", + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/struct.pb.ts b/packages/protoscript/src/runtime/well-known-types/struct.pb.ts index d5386bf..addc398 100644 --- a/packages/protoscript/src/runtime/well-known-types/struct.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/struct.pb.ts @@ -151,9 +151,10 @@ export const Struct = { /** * Initializes Struct with all fields set to their default value. */ - initialize: function (): Struct { + initialize: function (msg?: Partial): Struct { return { fields: {}, + ...msg, }; }, @@ -273,7 +274,7 @@ export const Value = { /** * Initializes Value with all fields set to their default value. */ - initialize: function (): Value { + initialize: function (msg?: Partial): Value { return { nullValue: undefined, numberValue: undefined, @@ -281,6 +282,7 @@ export const Value = { boolValue: undefined, structValue: undefined, listValue: undefined, + ...msg, }; }, @@ -379,9 +381,10 @@ export const ListValue = { /** * Initializes ListValue with all fields set to their default value. */ - initialize: function (): ListValue { + initialize: function (msg?: Partial): ListValue { return { values: [], + ...msg, }; }, @@ -481,9 +484,10 @@ export const StructJSON = { /** * Initializes Struct with all fields set to their default value. */ - initialize: function (): Struct { + initialize: function (msg?: Partial): Struct { return { fields: {}, + ...msg, }; }, @@ -577,7 +581,7 @@ export const ValueJSON = { /** * Initializes Value with all fields set to their default value. */ - initialize: function (): Value { + initialize: function (msg?: Partial): Value { return { nullValue: undefined, numberValue: undefined, @@ -585,6 +589,7 @@ export const ValueJSON = { boolValue: undefined, structValue: undefined, listValue: undefined, + ...msg, }; }, @@ -671,9 +676,10 @@ export const ListValueJSON = { /** * Initializes ListValue with all fields set to their default value. */ - initialize: function (): ListValue { + initialize: function (msg?: Partial): ListValue { return { values: [], + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/timestamp.pb.ts b/packages/protoscript/src/runtime/well-known-types/timestamp.pb.ts index 6b91692..52eaa48 100644 --- a/packages/protoscript/src/runtime/well-known-types/timestamp.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/timestamp.pb.ts @@ -145,10 +145,11 @@ export const Timestamp = { /** * Initializes Timestamp with all fields set to their default value. */ - initialize: function (): Timestamp { + initialize: function (msg?: Partial): Timestamp { return { seconds: 0n, nanos: 0, + ...msg, }; }, @@ -221,10 +222,11 @@ export const TimestampJSON = { /** * Initializes Timestamp with all fields set to their default value. */ - initialize: function (): Timestamp { + initialize: function (msg?: Partial): Timestamp { return { seconds: 0n, nanos: 0, + ...msg, }; }, diff --git a/packages/protoscript/src/runtime/well-known-types/type.pb.ts b/packages/protoscript/src/runtime/well-known-types/type.pb.ts index 61cb7cb..79d9b1c 100644 --- a/packages/protoscript/src/runtime/well-known-types/type.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/type.pb.ts @@ -283,7 +283,7 @@ export const Type = { /** * Initializes Type with all fields set to their default value. */ - initialize: function (): Type { + initialize: function (msg?: Partial): Type { return { name: "", fields: [], @@ -292,6 +292,7 @@ export const Type = { sourceContext: protoscript.SourceContext.initialize(), syntax: Syntax._fromInt(0), edition: "", + ...msg, }; }, @@ -406,7 +407,7 @@ export const Field = { /** * Initializes Field with all fields set to their default value. */ - initialize: function (): Field { + initialize: function (msg?: Partial): Field { return { kind: Field.Kind._fromInt(0), cardinality: Field.Cardinality._fromInt(0), @@ -418,6 +419,7 @@ export const Field = { options: [], jsonName: "", defaultValue: "", + ...msg, }; }, @@ -824,7 +826,7 @@ export const Enum = { /** * Initializes Enum with all fields set to their default value. */ - initialize: function (): Enum { + initialize: function (msg?: Partial): Enum { return { name: "", enumvalue: [], @@ -832,6 +834,7 @@ export const Enum = { sourceContext: protoscript.SourceContext.initialize(), syntax: Syntax._fromInt(0), edition: "", + ...msg, }; }, @@ -943,11 +946,12 @@ export const EnumValue = { /** * Initializes EnumValue with all fields set to their default value. */ - initialize: function (): EnumValue { + initialize: function (msg?: Partial): EnumValue { return { name: "", number: 0, options: [], + ...msg, }; }, @@ -1028,10 +1032,11 @@ export const Option = { /** * Initializes Option with all fields set to their default value. */ - initialize: function (): Option { + initialize: function (msg?: Partial