Skip to content

Commit

Permalink
feat: TECH can not read property encode of undefined (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirpichenko authored Jan 28, 2022
1 parent 3661390 commit f1fa0f2
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 221 deletions.
25 changes: 11 additions & 14 deletions internal/generator/runner_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func (r *Runner) generateTypescriptNamespace(generatedFileStream *protogen.Gener
)
r.indentLevel += 2

r.generateTypescriptClassDecoratorDefinition(generatedFileStream, protoFile)
if r.currentPackage == "google.protobuf" {
r.generateTypescriptCommonPackageDecoratorDefinition(generatedFileStream, protoFile)
}

// This interface is namespace-private, as it's being replicated for every generated file
r.P(
Expand All @@ -146,22 +148,17 @@ func (r *Runner) generateTypescriptNamespace(generatedFileStream *protogen.Gener
r.P(generatedFileStream, "\n}")
}

func (r *Runner) generateTypescriptClassDecoratorDefinition(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) {
func (r *Runner) generateTypescriptCommonPackageDecoratorDefinition(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) {
r.P(
generatedFileStream,
"const registerGrpcClass = <T extends protobufjs.Message<T>>(",
" typeName: string",
"): protobufjs.TypeDecorator<T> => {",
" if (protobufjs.util.decorateRoot.get(typeName) != null) {",
" // eslint-disable-next-line @typescript-eslint/ban-types",
" return (",
" // eslint-disable-next-line @typescript-eslint/no-unused-vars",
" _: protobufjs.Constructor<T>",
" ): void => {",
" // Do nothing",
" }",
"const registerCommonClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {",
" const registeredType = protobufjs.util.decorateRoot.get(typeName)",
" if (registeredType == null) {",
" return protobufjs.Type.d(typeName)",
" }",
" return (ctor: protobufjs.Constructor<T>): void => {",
" Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false })",
" }",
" return protobufjs.Type.d(typeName)",
"}",
)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/generator/runner_generate_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ func (r *Runner) generateTypescriptMessageClass(generatedFileStream *protogen.Ge
if !hasEnums {
implementedInterfaces += ", I" + className
}

classDecoratorName := "@protobufjs.Type.d"
if r.currentPackage == "google.protobuf" {
classDecoratorName = "@registerCommonClass"
}

r.P(
generatedFileStream,
"@registerGrpcClass('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')",
classDecoratorName+"('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')",
"export class "+className+" extends protobufjs.Message<"+className+"> implements "+implementedInterfaces+" {\n",
)
r.indentLevel += 2
Expand Down
18 changes: 3 additions & 15 deletions tests/__tests__/generated/Flavors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
Expand All @@ -11,18 +11,6 @@ import { grpc } from '@join-com/grpc'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Flavors {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand All @@ -40,7 +28,7 @@ export namespace Flavors {
emails: Email[]
}

@registerGrpcClass('flavors_UserProfile')
@protobufjs.Type.d('flavors_UserProfile')
export class UserProfile
extends protobufjs.Message<UserProfile>
implements ConvertibleTo<IUserProfile>, IUserProfile
Expand Down Expand Up @@ -94,7 +82,7 @@ export namespace Flavors {
}
}

@registerGrpcClass('flavors_UserRequest')
@protobufjs.Type.d('flavors_UserRequest')
export class UserRequest
extends protobufjs.Message<UserRequest>
implements ConvertibleTo<IUserRequest>, IUserRequest
Expand Down
22 changes: 5 additions & 17 deletions tests/__tests__/generated/Regressions.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Regressions {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand Down Expand Up @@ -52,7 +40,7 @@ export namespace Regressions {
/**
* @deprecated
*/
@registerGrpcClass('regressions_DeprecatedMessageWithDeprecatedField')
@protobufjs.Type.d('regressions_DeprecatedMessageWithDeprecatedField')
export class DeprecatedMessageWithDeprecatedField
extends protobufjs.Message<DeprecatedMessageWithDeprecatedField>
implements ConvertibleTo<IDeprecatedMessageWithDeprecatedField>, IDeprecatedMessageWithDeprecatedField
Expand Down Expand Up @@ -102,7 +90,7 @@ export namespace Regressions {
}
}

@registerGrpcClass('regressions_MessageWithDeprecatedField')
@protobufjs.Type.d('regressions_MessageWithDeprecatedField')
export class MessageWithDeprecatedField
extends protobufjs.Message<MessageWithDeprecatedField>
implements ConvertibleTo<IMessageWithDeprecatedField>, IMessageWithDeprecatedField
Expand Down Expand Up @@ -146,7 +134,7 @@ export namespace Regressions {
}
}

@registerGrpcClass('regressions_Reg01Inner')
@protobufjs.Type.d('regressions_Reg01Inner')
export class Reg01Inner extends protobufjs.Message<Reg01Inner> implements ConvertibleTo<IReg01Inner>, IReg01Inner {
@protobufjs.Field.d(1, 'string', 'optional')
public value?: string
Expand Down Expand Up @@ -177,7 +165,7 @@ export namespace Regressions {
}
}

@registerGrpcClass('regressions_Reg01Outer')
@protobufjs.Type.d('regressions_Reg01Outer')
export class Reg01Outer extends protobufjs.Message<Reg01Outer> implements ConvertibleTo<IReg01Outer>, IReg01Outer {
@protobufjs.Field.d(1, Reg01Inner, 'optional')
public inner?: Reg01Inner
Expand Down
26 changes: 7 additions & 19 deletions tests/__tests__/generated/Test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
Expand All @@ -14,18 +14,6 @@ import { grpc } from '@join-com/grpc'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Foo {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand Down Expand Up @@ -114,7 +102,7 @@ export namespace Foo {
optionalField?: number
}

@registerGrpcClass('foo_CustomOptionsTest')
@protobufjs.Type.d('foo_CustomOptionsTest')
export class CustomOptionsTest
extends protobufjs.Message<CustomOptionsTest>
implements ConvertibleTo<ICustomOptionsTest>
Expand Down Expand Up @@ -179,7 +167,7 @@ export namespace Foo {
/**
* @deprecated
*/
@registerGrpcClass('foo_Nested')
@protobufjs.Type.d('foo_Nested')
export class Nested extends protobufjs.Message<Nested> implements ConvertibleTo<INested>, INested {
@protobufjs.Field.d(1, 'string', 'optional')
public title?: string
Expand Down Expand Up @@ -210,7 +198,7 @@ export namespace Foo {
}
}

@registerGrpcClass('foo_Request')
@protobufjs.Type.d('foo_Request')
export class Request extends protobufjs.Message<Request> implements ConvertibleTo<IRequest>, IRequest {
@protobufjs.Field.d(1, 'int32', 'optional')
public id?: number
Expand Down Expand Up @@ -241,7 +229,7 @@ export namespace Foo {
}
}

@registerGrpcClass('foo_RequiredPropertiesTest')
@protobufjs.Type.d('foo_RequiredPropertiesTest')
export class RequiredPropertiesTest
extends protobufjs.Message<RequiredPropertiesTest>
implements ConvertibleTo<IRequiredPropertiesTest>, IRequiredPropertiesTest
Expand Down Expand Up @@ -296,7 +284,7 @@ export namespace Foo {
}
}

@registerGrpcClass('foo_Test')
@protobufjs.Type.d('foo_Test')
export class Test extends protobufjs.Message<Test> implements ConvertibleTo<ITest> {
@protobufjs.Field.d(1, 'int32', 'optional')
public fieldInt32?: number
Expand Down Expand Up @@ -475,7 +463,7 @@ export namespace Foo {
}
}

@registerGrpcClass('foo_BigWrapper')
@protobufjs.Type.d('foo_BigWrapper')
export class BigWrapper extends protobufjs.Message<BigWrapper> implements ConvertibleTo<IBigWrapper> {
@protobufjs.Field.d(1, Test, 'optional')
public nestedTest?: Test
Expand Down
16 changes: 2 additions & 14 deletions tests/__tests__/generated/common/Common.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand All @@ -30,7 +18,7 @@ export namespace Common {
latsName?: string
}

@registerGrpcClass('common_OtherPkgMessage')
@protobufjs.Type.d('common_OtherPkgMessage')
export class OtherPkgMessage
extends protobufjs.Message<OtherPkgMessage>
implements ConvertibleTo<IOtherPkgMessage>, IOtherPkgMessage
Expand Down
16 changes: 2 additions & 14 deletions tests/__tests__/generated/common/Extra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'
Expand All @@ -8,18 +8,6 @@ import { GoogleProtobuf } from '../google/protobuf/Timestamp'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand All @@ -33,7 +21,7 @@ export namespace Common {
birthDate?: Date
}

@registerGrpcClass('common_ExtraPkgMessage')
@protobufjs.Type.d('common_ExtraPkgMessage')
export class ExtraPkgMessage extends protobufjs.Message<ExtraPkgMessage> implements ConvertibleTo<IExtraPkgMessage> {
/**
* @deprecated
Expand Down
21 changes: 9 additions & 12 deletions tests/__tests__/generated/google/protobuf/Empty.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
const registerCommonClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
const registeredType = protobufjs.util.decorateRoot.get(typeName)
if (registeredType == null) {
return protobufjs.Type.d(typeName)
}
return (ctor: protobufjs.Constructor<T>): void => {
Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false })
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}

export interface IEmpty {}

@registerGrpcClass('google_protobuf_Empty')
@registerCommonClass('google_protobuf_Empty')
export class Empty extends protobufjs.Message<Empty> implements ConvertibleTo<IEmpty>, IEmpty {
public asInterface(): IEmpty {
const message = {
Expand Down
21 changes: 9 additions & 12 deletions tests/__tests__/generated/google/protobuf/Timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
// GENERATOR VERSION: 2.1.0.d41be8f.1643383265
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
const registerGrpcClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>,
): void => {
// Do nothing
}
const registerCommonClass = <T extends protobufjs.Message<T>>(typeName: string): protobufjs.TypeDecorator<T> => {
const registeredType = protobufjs.util.decorateRoot.get(typeName)
if (registeredType == null) {
return protobufjs.Type.d(typeName)
}
return (ctor: protobufjs.Constructor<T>): void => {
Object.defineProperty(ctor, '$type', { value: registeredType, enumerable: false })
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
Expand All @@ -27,7 +24,7 @@ export namespace GoogleProtobuf {
nanos?: number
}

@registerGrpcClass('google_protobuf_Timestamp')
@registerCommonClass('google_protobuf_Timestamp')
export class Timestamp extends protobufjs.Message<Timestamp> implements ConvertibleTo<ITimestamp>, ITimestamp {
@protobufjs.Field.d(1, 'int64', 'optional')
public seconds?: number
Expand Down
Loading

0 comments on commit f1fa0f2

Please sign in to comment.