Skip to content

Commit

Permalink
fix: typescript errors for struct with optional=all (#1008)
Browse files Browse the repository at this point in the history
Fixes #578 

Due to the use of the `.foreach` function TS has no context of the clue
that `struct.fields` is definently not undefined.
  • Loading branch information
UnderKoen authored Feb 28, 2024
1 parent ba97f98 commit e838e38
Show file tree
Hide file tree
Showing 26 changed files with 1,250 additions and 79 deletions.
9 changes: 5 additions & 4 deletions integration/grpc-js/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/nestjs-simple/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,21 @@ function createBaseStruct(): Struct {
export const Struct = {
wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = Value.wrap(object[key]);
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = Value.unwrap(message.fields[key]);
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/nice-grpc/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/oneof-unions-snake/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/oneof-unions/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
Binary file added integration/simple-esmodule-interop/empty.bin
Binary file not shown.
9 changes: 5 additions & 4 deletions integration/simple-snake/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/simple-string-enums/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/static-only/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/struct/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/type-annotations/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
9 changes: 5 additions & 4 deletions integration/type-registry/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
13 changes: 8 additions & 5 deletions integration/use-map-type/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,22 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields.set(key, object[key]);
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
[...message.fields.keys()].forEach((key) => {
object[key] = message.fields.get(key);
});
if (message.fields) {
for (const key of message.fields.keys()) {
object[key] = message.fields.get(key);
}
}
return object;
},
};
Expand Down
9 changes: 5 additions & 4 deletions integration/use-numeric-enum-json/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,21 @@ export const Struct = {

wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();

if (object !== undefined) {
Object.keys(object).forEach((key) => {
for (const key of Object.keys(object)) {
struct.fields[key] = object[key];
});
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = message.fields[key];
});
}
}
return object;
},
Expand Down
Loading

0 comments on commit e838e38

Please sign in to comment.