Skip to content

Commit

Permalink
fix: JOIN-16944 duplicated type in Root. Define a Root instance and u…
Browse files Browse the repository at this point in the history
…se in every generated file (#59)
  • Loading branch information
kirpichenko authored Feb 28, 2022
1 parent e9910b5 commit efa59c2
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 20 deletions.
4 changes: 4 additions & 0 deletions internal/generator/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,9 @@ func (r *Runner) Run(plugin *protogen.Plugin) error {
r.generateTypescriptFile(file, generatedFileStream)
}

// Generate file with shared protobuf Root instance
rootFileStream := plugin.NewGeneratedFile("root.ts", "")
r.generateRoot(rootFileStream)

return nil
}
16 changes: 14 additions & 2 deletions internal/generator/runner_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/iancoleman/strcase"
"github.com/join-com/protoc-gen-ts/internal/join_proto"
Expand All @@ -26,6 +27,7 @@ func (r *Runner) generateTypescriptFile(protoFile *protogen.File, generatedFileS
)

r.generateTypescriptImports(protoFile, generatedFileStream)
r.generateRootChange(protoFile, generatedFileStream)
r.generateTypescriptNamespace(generatedFileStream, protoFile)
}

Expand Down Expand Up @@ -63,13 +65,23 @@ func (r *Runner) generateTypescriptImports(protoFile *protogen.File, generatedFi
for _, importLine := range importLines {
generatedFileStream.P(importLine)
}
generatedFileStream.P("")

if len(protoFile.Proto.GetService()) > 0 {
generatedFileStream.P("import { grpc } from '@join-com/grpc'\n")
generatedFileStream.P("import { grpc } from '@join-com/grpc'")
}

numStepsBack := len(strings.Split(currentSourcePath, "/")) - 1
if numStepsBack == 0 {
generatedFileStream.P("import { root } from './root'\n")
} else {
generatedFileStream.P("import { root } from '" + strings.Repeat("../", numStepsBack) + "root'\n")
}
}

func (r *Runner) generateRootChange(protoFile *protogen.File, generatedFileStream *protogen.GeneratedFile) {
generatedFileStream.P("protobufjs.roots['decorated'] = root\n")
}

func (r *Runner) fileHasFlavors(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) bool {
for _, messageSpec := range protoFile.Proto.GetMessageType() {
for _, fieldSpec := range messageSpec.GetField() {
Expand Down
12 changes: 12 additions & 0 deletions internal/generator/runner_generate_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package generator

import (
"google.golang.org/protobuf/compiler/protogen"
)

func (r *Runner) generateRoot(generatedFileStream *protogen.GeneratedFile) {
generatedFileStream.P(
"import { Root } from 'protobufjs'\n\n",
"export const root = new Root()",
)
}
6 changes: 4 additions & 2 deletions tests/__tests__/generated/Flavors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
import * as protobufjs from 'protobufjs/light'

import { WithFlavor } from '@coderspirit/nominal'

import { grpc } from '@join-com/grpc'
import { root } from './root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Flavors {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generated/Regressions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from './root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Regressions {
interface ConvertibleTo<T> {
Expand Down
6 changes: 4 additions & 2 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.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
Expand All @@ -9,8 +9,10 @@ import { Common as Common_Common } from './common/Common'
import { Common as Common_Extra } from './common/Extra'
import { GoogleProtobuf as GoogleProtobuf_Empty } from './google/protobuf/Empty'
import { GoogleProtobuf as GoogleProtobuf_Timestamp } from './google/protobuf/Timestamp'

import { grpc } from '@join-com/grpc'
import { root } from './root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Foo {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generated/common/Common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from '../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
interface ConvertibleTo<T> {
Expand Down
5 changes: 4 additions & 1 deletion tests/__tests__/generated/common/Extra.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { GoogleProtobuf } from '../google/protobuf/Timestamp'
import { root } from '../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generated/google/protobuf/Empty.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from '../../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
interface ConvertibleTo<T> {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generated/google/protobuf/Timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from '../../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
interface ConvertibleTo<T> {
Expand Down
3 changes: 3 additions & 0 deletions tests/__tests__/generated/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Root } from 'protobufjs'

export const root = new Root()
6 changes: 4 additions & 2 deletions tests/__tests__/generatedRedundant/Flavors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
import * as protobufjs from 'protobufjs/light'

import { WithFlavor } from '@coderspirit/nominal'

import { grpc } from '@join-com/grpc'
import { root } from './root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Flavors {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generatedRedundant/Regressions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from './root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Regressions {
interface ConvertibleTo<T> {
Expand Down
6 changes: 4 additions & 2 deletions tests/__tests__/generatedRedundant/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.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
Expand All @@ -9,8 +9,10 @@ import { Common as Common_Common } from './common/Common'
import { Common as Common_Extra } from './common/Extra'
import { GoogleProtobuf as GoogleProtobuf_Empty } from './google/protobuf/Empty'
import { GoogleProtobuf as GoogleProtobuf_Timestamp } from './google/protobuf/Timestamp'

import { grpc } from '@join-com/grpc'
import { root } from './root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Foo {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generatedRedundant/common/Common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from '../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
interface ConvertibleTo<T> {
Expand Down
5 changes: 4 additions & 1 deletion tests/__tests__/generatedRedundant/common/Extra.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { GoogleProtobuf } from '../google/protobuf/Timestamp'
import { root } from '../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
Expand Down
6 changes: 5 additions & 1 deletion tests/__tests__/generatedRedundant/google/protobuf/Empty.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from '../../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
interface ConvertibleTo<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209
// GENERATOR VERSION: 2.1.0.e9910b5.1646051017
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

import { root } from '../../root'

protobufjs.roots['decorated'] = root

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
interface ConvertibleTo<T> {
Expand Down
3 changes: 3 additions & 0 deletions tests/__tests__/generatedRedundant/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Root } from 'protobufjs'

export const root = new Root()

0 comments on commit efa59c2

Please sign in to comment.