Skip to content

Commit

Permalink
fix: Struct wrap/unwrap snake case #579 (#588)
Browse files Browse the repository at this point in the history
* fix: Struct wrap/unwrap snake case #579

* test: Add more tests about struct snake case

* style: format src/main.ts

Co-authored-by: TJT <tjt@maimemo.com>
  • Loading branch information
Celend and TJT committed Jun 5, 2022
1 parent ee1cced commit 33f89bf
Show file tree
Hide file tree
Showing 11 changed files with 1,234 additions and 40 deletions.
515 changes: 515 additions & 0 deletions integration/oneof-unions-snake/google/protobuf/struct.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions integration/oneof-unions-snake/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snakeToCamel=false,oneof=unions
12 changes: 12 additions & 0 deletions integration/oneof-unions-snake/simple-snake-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SimpleStruct } from './simple';

describe('oneof=unions,snakeToCamel=false', () => {
it('struct snake case', () => {
const simple: SimpleStruct = {
simple_struct: {
any: 'any'
}
}
expect(simple.simple_struct!['any']).toEqual('any')
})
});
9 changes: 9 additions & 0 deletions integration/oneof-unions-snake/simple.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Adding a comment to the syntax will become the first
// comment in the output source file.
syntax = "proto3";
import "google/protobuf/struct.proto";
package simple;

message SimpleStruct {
google.protobuf.Struct simple_struct = 1;
}
94 changes: 94 additions & 0 deletions integration/oneof-unions-snake/simple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable */
import * as Long from 'long';
import * as _m0 from 'protobufjs/minimal';
import { Struct } from './google/protobuf/struct';

export const protobufPackage = 'simple';

/**
* Adding a comment to the syntax will become the first
* comment in the output source file.
*/

export interface SimpleStruct {
simple_struct: { [key: string]: any } | undefined;
}

function createBaseSimpleStruct(): SimpleStruct {
return { simple_struct: undefined };
}

export const SimpleStruct = {
encode(message: SimpleStruct, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.simple_struct !== undefined) {
Struct.encode(Struct.wrap(message.simple_struct), writer.uint32(10).fork()).ldelim();
}
return writer;
},

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

fromJSON(object: any): SimpleStruct {
return {
simple_struct: isObject(object.simple_struct) ? object.simple_struct : undefined,
};
},

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

fromPartial<I extends Exact<DeepPartial<SimpleStruct>, I>>(object: I): SimpleStruct {
const message = createBaseSimpleStruct();
message.simple_struct = object.simple_struct ?? undefined;
return message;
},
};

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

export type DeepPartial<T> = T extends Builtin
? T
: T extends Array<infer U>
? Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: T extends { $case: string }
? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & { $case: T['$case'] }
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin
? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & Record<Exclude<keyof I, KeysOfUnion<P>>, never>;

// If you get a compile-error about 'Constructor<Long> and ... have no overlap',
// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'.
if (_m0.util.Long !== Long) {
_m0.util.Long = Long as any;
_m0.configure();
}

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

0 comments on commit 33f89bf

Please sign in to comment.