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

initialize now accepts partial messages #45

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 21 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 📦
Expand Down
28 changes: 20 additions & 8 deletions e2e/conformance/proto/conformance/conformance.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): FailureSet {
return {
failure: [],
...msg,
};
},

Expand Down Expand Up @@ -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>): ConformanceRequest {
return {
protobufPayload: undefined,
jsonPayload: undefined,
Expand All @@ -391,6 +392,7 @@ export const ConformanceRequest = {
testCategory: TestCategory._fromInt(0),
jspbEncodingOptions: JspbEncodingConfig.initialize(),
printUnknownFields: false,
...msg,
};
},

Expand Down Expand Up @@ -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>,
): ConformanceResponse {
return {
parseError: undefined,
serializeError: undefined,
Expand All @@ -532,6 +536,7 @@ export const ConformanceResponse = {
skipped: undefined,
jspbPayload: undefined,
textPayload: undefined,
...msg,
};
},

Expand Down Expand Up @@ -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>): JspbEncodingConfig {
return {
useJspbArrayAnyFormat: false,
...msg,
};
},

Expand Down Expand Up @@ -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>): FailureSet {
return {
failure: [],
...msg,
};
},

Expand Down Expand Up @@ -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>): ConformanceRequest {
return {
protobufPayload: undefined,
jsonPayload: undefined,
Expand All @@ -925,6 +932,7 @@ export const ConformanceRequestJSON = {
testCategory: TestCategory._fromInt(0),
jspbEncodingOptions: JspbEncodingConfigJSON.initialize(),
printUnknownFields: false,
...msg,
};
},

Expand Down Expand Up @@ -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>,
): ConformanceResponse {
return {
parseError: undefined,
serializeError: undefined,
Expand All @@ -1059,6 +1069,7 @@ export const ConformanceResponseJSON = {
skipped: undefined,
jspbPayload: undefined,
textPayload: undefined,
...msg,
};
},

Expand Down Expand Up @@ -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>): JspbEncodingConfig {
return {
useJspbArrayAnyFormat: false,
...msg,
};
},

Expand Down
50 changes: 36 additions & 14 deletions e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): TestAllTypesProto3 {
return {
optionalInt32: 0,
optionalInt64: 0n,
Expand Down Expand Up @@ -604,6 +604,7 @@ export const TestAllTypesProto3 = {
fieldName16: 0,
fieldName17: 0,
FieldName18: 0,
...msg,
};
},

Expand Down Expand Up @@ -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>,
): TestAllTypesProto3.NestedMessage {
return {
a: 0,
corecursive: undefined,
...msg,
};
},

Expand Down Expand Up @@ -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>): ForeignMessage {
return {
c: 0,
...msg,
};
},

Expand Down Expand Up @@ -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>,
): NullHypothesisProto3 {
return {
...msg,
};
},

/**
Expand Down Expand Up @@ -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>): EnumOnlyProto3 {
return {
...msg,
};
},

/**
Expand Down Expand Up @@ -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>): TestAllTypesProto3 {
return {
optionalInt32: 0,
optionalInt64: 0n,
Expand Down Expand Up @@ -3944,6 +3955,7 @@ export const TestAllTypesProto3JSON = {
fieldName16: 0,
fieldName17: 0,
FieldName18: 0,
...msg,
};
},

Expand Down Expand Up @@ -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>,
): TestAllTypesProto3.NestedMessage {
return {
a: 0,
corecursive: undefined,
...msg,
};
},

Expand Down Expand Up @@ -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>): ForeignMessage {
return {
c: 0,
...msg,
};
},

Expand Down Expand Up @@ -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>,
): NullHypothesisProto3 {
return {
...msg,
};
},

/**
Expand Down Expand Up @@ -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>): EnumOnlyProto3 {
return {
...msg,
};
},

/**
Expand Down
Loading