Skip to content

Commit

Permalink
Merge pull request #50 from join-com/fix-field-deprecation
Browse files Browse the repository at this point in the history
Fix field deprecation
  • Loading branch information
castarco authored Oct 7, 2021
2 parents 338a2a7 + 8f3616f commit 6950541
Show file tree
Hide file tree
Showing 15 changed files with 1,216 additions and 915 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ commands:
jobs:
build_and_test:
docker:
- image: circleci/golang:1.16-node
- image: circleci/golang:1.17-node
<<: *docker-auth
steps:
- install_protoc
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/join-com/protoc-gen-ts

go 1.16
go 1.17

require (
github.com/iancoleman/strcase v0.1.3
google.golang.org/protobuf v1.26.0
github.com/iancoleman/strcase v0.2.0
google.golang.org/protobuf v1.27.1
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw=
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
3 changes: 1 addition & 2 deletions internal/generator/runner_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ func (r *Runner) generateTypescriptInterfaceField(
requiredFields bool,
) {
fieldOptions := fieldSpec.GetOptions()
messageOptions := messageSpec.GetOptions()
if fieldOptions != nil && messageOptions != nil && messageOptions.GetDeprecated() {
if fieldOptions != nil && fieldOptions.GetDeprecated() {
r.P(generatedFileStream, "/**\n * @deprecated\n */")
}

Expand Down
8 changes: 6 additions & 2 deletions tests/__tests__/generated/Flavors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export namespace Flavors {
public emails!: Email[]

public asInterface(): IUserProfile {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
const field = message[fieldName as keyof IUserProfile]
if (field == null || (Array.isArray(field) && field.length === 0)) {
Expand Down Expand Up @@ -107,7 +109,9 @@ export namespace Flavors {
public userId?: UserId

public asInterface(): IUserRequest {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IUserRequest] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down
127 changes: 126 additions & 1 deletion tests/__tests__/generated/Regressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,82 @@ export namespace Regressions {
inner?: IReg01Inner
}

export interface IWithDeprecatedField {
notDeprecated?: string
/**
* @deprecated
*/
deprecated?: string
}

/**
* @deprecated
*/
export interface IDeprecatedWithDeprecatedField {
notDeprecated?: string
/**
* @deprecated
*/
deprecated?: string
}

/**
* @deprecated
*/
@protobufjs.Type.d('regressions_DeprecatedWithDeprecatedField')
export class DeprecatedWithDeprecatedField
extends protobufjs.Message<DeprecatedWithDeprecatedField>
implements
ConvertibleTo<IDeprecatedWithDeprecatedField>,
IDeprecatedWithDeprecatedField
{
@protobufjs.Field.d(1, 'string', 'optional')
public notDeprecated?: string

/**
* @deprecated
*/
@protobufjs.Field.d(2, 'string', 'optional')
public deprecated?: string

public asInterface(): IDeprecatedWithDeprecatedField {
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (
message[fieldName as keyof IDeprecatedWithDeprecatedField] == null
) {
// We remove the key to avoid problems with code making too many assumptions
delete message[fieldName as keyof IDeprecatedWithDeprecatedField]
}
}
return message
}

public static fromInterface(
this: void,
value: IDeprecatedWithDeprecatedField
): DeprecatedWithDeprecatedField {
return DeprecatedWithDeprecatedField.fromObject(value)
}

public static decodePatched(
this: void,
reader: protobufjs.Reader | Uint8Array
): IDeprecatedWithDeprecatedField {
return DeprecatedWithDeprecatedField.decode(reader).asInterface()
}

public static encodePatched(
this: void,
message: IDeprecatedWithDeprecatedField,
writer?: protobufjs.Writer
): protobufjs.Writer {
return DeprecatedWithDeprecatedField.encode(message, writer)
}
}

@protobufjs.Type.d('regressions_Reg01Inner')
export class Reg01Inner
extends protobufjs.Message<Reg01Inner>
Expand All @@ -26,7 +102,9 @@ export namespace Regressions {
public value?: string

public asInterface(): IReg01Inner {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IReg01Inner] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down Expand Up @@ -97,4 +175,51 @@ export namespace Regressions {
return Reg01Outer.encode(message, writer)
}
}

@protobufjs.Type.d('regressions_WithDeprecatedField')
export class WithDeprecatedField
extends protobufjs.Message<WithDeprecatedField>
implements ConvertibleTo<IWithDeprecatedField>, IWithDeprecatedField
{
@protobufjs.Field.d(1, 'string', 'optional')
public notDeprecated?: string

@protobufjs.Field.d(2, 'string', 'optional')
public deprecated?: string

public asInterface(): IWithDeprecatedField {
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IWithDeprecatedField] == null) {
// We remove the key to avoid problems with code making too many assumptions
delete message[fieldName as keyof IWithDeprecatedField]
}
}
return message
}

public static fromInterface(
this: void,
value: IWithDeprecatedField
): WithDeprecatedField {
return WithDeprecatedField.fromObject(value)
}

public static decodePatched(
this: void,
reader: protobufjs.Reader | Uint8Array
): IWithDeprecatedField {
return WithDeprecatedField.decode(reader).asInterface()
}

public static encodePatched(
this: void,
message: IWithDeprecatedField,
writer?: protobufjs.Writer
): protobufjs.Writer {
return WithDeprecatedField.encode(message, writer)
}
}
}
15 changes: 12 additions & 3 deletions tests/__tests__/generated/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export namespace Foo {
fieldUint64Repeated?: number[]
fieldSint32?: number
fieldSint32Repeated?: number[]
/**
* @deprecated
*/
fieldSint64?: number
fieldSint64Repeated?: number[]
fieldFixed32?: number
Expand Down Expand Up @@ -188,7 +191,9 @@ export namespace Foo {
public title?: string

public asInterface(): INested {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof INested] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down Expand Up @@ -227,7 +232,9 @@ export namespace Foo {
public id?: number

public asInterface(): IRequest {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IRequest] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down Expand Up @@ -272,7 +279,9 @@ export namespace Foo {
public optionalField?: number

public asInterface(): IRequiredPropertiesTest {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IRequiredPropertiesTest] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down
7 changes: 6 additions & 1 deletion tests/__tests__/generated/common/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export namespace Common {
}

export interface IOtherPkgMessage {
/**
* @deprecated
*/
firstName?: string
latsName?: string
}
Expand All @@ -26,7 +29,9 @@ export namespace Common {
public latsName?: string

public asInterface(): IOtherPkgMessage {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IOtherPkgMessage] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down
3 changes: 3 additions & 0 deletions tests/__tests__/generated/common/Extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export namespace Common {
}

export interface IExtraPkgMessage {
/**
* @deprecated
*/
firstName?: string
lastName?: string
birthDate?: Date
Expand Down
4 changes: 3 additions & 1 deletion tests/__tests__/generated/google/protobuf/Empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export namespace GoogleProtobuf {
implements ConvertibleTo<IEmpty>, IEmpty
{
public asInterface(): IEmpty {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof IEmpty] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down
4 changes: 3 additions & 1 deletion tests/__tests__/generated/google/protobuf/Timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export namespace GoogleProtobuf {
public nanos?: number

public asInterface(): ITimestamp {
const message = { ...this }
const message = {
...this,
}
for (const fieldName of Object.keys(message)) {
if (message[fieldName as keyof ITimestamp] == null) {
// We remove the key to avoid problems with code making too many assumptions
Expand Down
15 changes: 15 additions & 0 deletions tests/__tests__/proto/regressions.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ option go_package = "github.com/join-com/protoc-gen-ts/regressions";


// Regression 01
// ----------------------------------------------------------------------------

message Reg01Inner {
string value = 1;
Expand All @@ -15,3 +16,17 @@ message Reg01Inner {
message Reg01Outer {
Reg01Inner inner = 1;
}

// Regression 02
// ----------------------------------------------------------------------------

message WithDeprecatedField {
string not_deprecated = 1;
string deprecated = 2 [deprecated=true];
}

message DeprecatedWithDeprecatedField {
option deprecated = true;
string not_deprecated = 1;
string deprecated = 2 [deprecated=true];
}
Loading

0 comments on commit 6950541

Please sign in to comment.