Skip to content

Commit

Permalink
fix: make struct types play well with type registry (#503)
Browse files Browse the repository at this point in the history
Co-authored-by: aikoven <[email protected]>
  • Loading branch information
aikoven and aikoven authored Feb 12, 2022
1 parent 10b8879 commit d62f854
Show file tree
Hide file tree
Showing 11 changed files with 704 additions and 91 deletions.
28 changes: 17 additions & 11 deletions integration/grpc-js/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const Struct = {
},

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = Struct.fromPartial({});
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach((key) => {
struct.fields[key] = object[key];
Expand Down Expand Up @@ -340,23 +340,25 @@ export const Value = {
},

wrap(value: any): Value {
const result = createBaseValue();

if (value === null) {
return { nullValue: NullValue.NULL_VALUE } as Value;
result.nullValue = NullValue.NULL_VALUE;
} else if (typeof value === 'boolean') {
return { boolValue: value } as Value;
result.boolValue = value;
} else if (typeof value === 'number') {
return { numberValue: value } as Value;
result.numberValue = value;
} else if (typeof value === 'string') {
return { stringValue: value } as Value;
result.stringValue = value;
} else if (Array.isArray(value)) {
return { listValue: value } as Value;
result.listValue = value;
} else if (typeof value === 'object') {
return { structValue: value } as Value;
} else if (typeof value === 'undefined') {
return {} as Value;
} else {
result.structValue = value;
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
}

return result;
},

unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
Expand Down Expand Up @@ -430,7 +432,11 @@ export const ListValue = {
},

wrap(value: Array<any> | undefined): ListValue {
return { values: value ?? [] };
const result = createBaseListValue();

result.values = value ?? [];

return result;
},

unwrap(message: ListValue): Array<any> {
Expand Down
28 changes: 17 additions & 11 deletions integration/oneof-unions/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const Struct = {
},

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = Struct.fromPartial({});
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach((key) => {
struct.fields[key] = object[key];
Expand Down Expand Up @@ -359,23 +359,25 @@ export const Value = {
},

wrap(value: any): Value {
const result = createBaseValue();

if (value === null) {
return { kind: { $case: 'nullValue', nullValue: NullValue.NULL_VALUE } };
result.kind = { $case: 'nullValue', nullValue: NullValue.NULL_VALUE };
} else if (typeof value === 'boolean') {
return { kind: { $case: 'boolValue', boolValue: value } };
result.kind = { $case: 'boolValue', boolValue: value };
} else if (typeof value === 'number') {
return { kind: { $case: 'numberValue', numberValue: value } };
result.kind = { $case: 'numberValue', numberValue: value };
} else if (typeof value === 'string') {
return { kind: { $case: 'stringValue', stringValue: value } };
result.kind = { $case: 'stringValue', stringValue: value };
} else if (Array.isArray(value)) {
return { kind: { $case: 'listValue', listValue: value } };
result.kind = { $case: 'listValue', listValue: value };
} else if (typeof value === 'object') {
return { kind: { $case: 'structValue', structValue: value } };
} else if (typeof value === 'undefined') {
return {} as Value;
} else {
result.kind = { $case: 'structValue', structValue: value };
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
}

return result;
},

unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
Expand Down Expand Up @@ -450,7 +452,11 @@ export const ListValue = {
},

wrap(value: Array<any> | undefined): ListValue {
return { values: value ?? [] };
const result = createBaseListValue();

result.values = value ?? [];

return result;
},

unwrap(message: ListValue): Array<any> {
Expand Down
28 changes: 17 additions & 11 deletions integration/simple-string-enums/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const Struct = {
},

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = Struct.fromPartial({});
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach((key) => {
struct.fields[key] = object[key];
Expand Down Expand Up @@ -349,23 +349,25 @@ export const Value = {
},

wrap(value: any): Value {
const result = createBaseValue();

if (value === null) {
return { nullValue: NullValue.NULL_VALUE } as Value;
result.nullValue = NullValue.NULL_VALUE;
} else if (typeof value === 'boolean') {
return { boolValue: value } as Value;
result.boolValue = value;
} else if (typeof value === 'number') {
return { numberValue: value } as Value;
result.numberValue = value;
} else if (typeof value === 'string') {
return { stringValue: value } as Value;
result.stringValue = value;
} else if (Array.isArray(value)) {
return { listValue: value } as Value;
result.listValue = value;
} else if (typeof value === 'object') {
return { structValue: value } as Value;
} else if (typeof value === 'undefined') {
return {} as Value;
} else {
result.structValue = value;
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
}

return result;
},

unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
Expand Down Expand Up @@ -439,7 +441,11 @@ export const ListValue = {
},

wrap(value: Array<any> | undefined): ListValue {
return { values: value ?? [] };
const result = createBaseListValue();

result.values = value ?? [];

return result;
},

unwrap(message: ListValue): Array<any> {
Expand Down
28 changes: 17 additions & 11 deletions integration/struct/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const Struct = {
},

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = Struct.fromPartial({});
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach((key) => {
struct.fields[key] = object[key];
Expand Down Expand Up @@ -340,23 +340,25 @@ export const Value = {
},

wrap(value: any): Value {
const result = createBaseValue();

if (value === null) {
return { nullValue: NullValue.NULL_VALUE } as Value;
result.nullValue = NullValue.NULL_VALUE;
} else if (typeof value === 'boolean') {
return { boolValue: value } as Value;
result.boolValue = value;
} else if (typeof value === 'number') {
return { numberValue: value } as Value;
result.numberValue = value;
} else if (typeof value === 'string') {
return { stringValue: value } as Value;
result.stringValue = value;
} else if (Array.isArray(value)) {
return { listValue: value } as Value;
result.listValue = value;
} else if (typeof value === 'object') {
return { structValue: value } as Value;
} else if (typeof value === 'undefined') {
return {} as Value;
} else {
result.structValue = value;
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
}

return result;
},

unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
Expand Down Expand Up @@ -430,7 +432,11 @@ export const ListValue = {
},

wrap(value: Array<any> | undefined): ListValue {
return { values: value ?? [] };
const result = createBaseListValue();

result.values = value ?? [];

return result;
},

unwrap(message: ListValue): Array<any> {
Expand Down
Binary file modified integration/type-registry/bar/bar.bin
Binary file not shown.
Binary file modified integration/type-registry/foo.bin
Binary file not shown.
5 changes: 5 additions & 0 deletions integration/type-registry/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";
package foo;

import "google/protobuf/timestamp.proto";
import "google/protobuf/struct.proto";

message Foo {
google.protobuf.Timestamp timestamp = 1;
Expand All @@ -11,3 +12,7 @@ message Foo {
message Foo2 {
google.protobuf.Timestamp timestamp = 1;
}

message WithStruct {
google.protobuf.Struct struct = 1;
}
64 changes: 64 additions & 0 deletions integration/type-registry/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { messageTypeRegistry } from './typeRegistry';
import { util, configure, Writer, Reader } from 'protobufjs/minimal';
import * as Long from 'long';
import { Timestamp } from './google/protobuf/timestamp';
import { Struct } from './google/protobuf/struct';

export const protobufPackage = 'foo';

Expand All @@ -16,6 +17,11 @@ export interface Foo2 {
timestamp: Date | undefined;
}

export interface WithStruct {
$type: 'foo.WithStruct';
struct: { [key: string]: any } | undefined;
}

function createBaseFoo(): Foo {
return { $type: 'foo.Foo', timestamp: undefined };
}
Expand Down Expand Up @@ -124,6 +130,60 @@ export const Foo2 = {

messageTypeRegistry.set(Foo2.$type, Foo2);

function createBaseWithStruct(): WithStruct {
return { $type: 'foo.WithStruct', struct: undefined };
}

export const WithStruct = {
$type: 'foo.WithStruct' as const,

encode(message: WithStruct, writer: Writer = Writer.create()): Writer {
if (message.struct !== undefined) {
Struct.encode(Struct.wrap(message.struct), writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: Reader | Uint8Array, length?: number): WithStruct {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseWithStruct();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.struct = Struct.unwrap(Struct.decode(reader, reader.uint32()));
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): WithStruct {
return {
$type: WithStruct.$type,
struct: isObject(object.struct) ? object.struct : undefined,
};
},

toJSON(message: WithStruct): unknown {
const obj: any = {};
message.struct !== undefined && (obj.struct = message.struct);
return obj;
},

fromPartial<I extends Exact<DeepPartial<WithStruct>, I>>(object: I): WithStruct {
const message = createBaseWithStruct();
message.struct = object.struct ?? undefined;
return message;
},
};

messageTypeRegistry.set(WithStruct.$type, WithStruct);

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
Expand Down Expand Up @@ -170,6 +230,10 @@ if (util.Long !== Long) {
configure();
}

function isObject(value: any): boolean {
return typeof value === 'object' && value !== null;
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Loading

0 comments on commit d62f854

Please sign in to comment.