-
Notifications
You must be signed in to change notification settings - Fork 349
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
Add support for 'json_name' annotation. #210
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Simple } from './simple'; | ||
|
||
describe('simple', () => { | ||
it('generates field names correctly', () => { | ||
const simple: Simple = Simple.fromPartial({}); | ||
expect(Object.prototype.hasOwnProperty.call(simple, 'other_name')).toBe(true); | ||
}); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto3"; | ||
|
||
package simple; | ||
|
||
message Simple { | ||
string name = 1 [ json_name = "other_name" ]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable */ | ||
import { Writer, Reader } from 'protobufjs/minimal'; | ||
|
||
export const protobufPackage = 'simple'; | ||
|
||
export interface Simple { | ||
other_name: string; | ||
} | ||
|
||
const baseSimple: object = { other_name: '' }; | ||
|
||
export const Simple = { | ||
encode(message: Simple, writer: Writer = Writer.create()): Writer { | ||
writer.uint32(10).string(message.other_name); | ||
return writer; | ||
}, | ||
|
||
decode(input: Reader | Uint8Array, length?: number): Simple { | ||
const reader = input instanceof Uint8Array ? new Reader(input) : input; | ||
let end = length === undefined ? reader.len : reader.pos + length; | ||
const message = { ...baseSimple } as Simple; | ||
while (reader.pos < end) { | ||
const tag = reader.uint32(); | ||
switch (tag >>> 3) { | ||
case 1: | ||
message.other_name = reader.string(); | ||
break; | ||
default: | ||
reader.skipType(tag & 7); | ||
break; | ||
} | ||
} | ||
return message; | ||
}, | ||
|
||
fromJSON(object: any): Simple { | ||
const message = { ...baseSimple } as Simple; | ||
if (object.other_name !== undefined && object.other_name !== null) { | ||
message.other_name = String(object.other_name); | ||
} else { | ||
message.other_name = ''; | ||
} | ||
return message; | ||
}, | ||
|
||
fromPartial(object: DeepPartial<Simple>): Simple { | ||
const message = { ...baseSimple } as Simple; | ||
if (object.other_name !== undefined && object.other_name !== null) { | ||
message.other_name = object.other_name; | ||
} else { | ||
message.other_name = ''; | ||
} | ||
return message; | ||
}, | ||
|
||
toJSON(message: Simple): unknown { | ||
const obj: any = {}; | ||
message.other_name !== undefined && (obj.other_name = message.other_name); | ||
return obj; | ||
}, | ||
}; | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | 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 {} | ||
? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: Partial<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, my interpretation of
json_name
attribute would be that this particular name would still bename
and thenother_name
would only show up on the JSON on the wire (i.e. intoJson
methods) and then looked for when data is parsed in from the wire (i.e. in thefromJson
methods).Can we do that instead?