Skip to content

Commit

Permalink
Ensure the order of import to stablize the output
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored and lyonlai committed Oct 26, 2020
1 parent 9635d7e commit bbc170d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
11 changes: 11 additions & 0 deletions data/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package data
import (
"path"
"path/filepath"
"sort"
"strings"
)

Expand All @@ -27,6 +28,16 @@ type File struct {
PackageNonScalarType []Type
}

// StableDependencies are dependencies in a stable order.
func (f *File) StableDependencies() []*Dependency {
out := make([]*Dependency, len(f.Dependencies))
copy(out, f.Dependencies)
sort.Slice(out, func(i, j int) bool {
return out[i].SourceFile < out[j].SourceFile
})
return out
}

// NeedsOneOfSupport indicates the file needs one of support type utilities
func (f *File) NeedsOneOfSupport() bool {
for _, m := range f.Messages {
Expand Down
10 changes: 6 additions & 4 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package generator

import (
"bytes"
"strings"
"text/template"

plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
log "github.com/sirupsen/logrus" // nolint: depguard

"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/data"
"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/registry"
"git.sqcorp.co/cash/gap/errors"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
log "github.com/sirupsen/logrus" // nolint: depguard
"strings"
"text/template"
)

// TypeScriptGRPCGatewayGenerator is the protobuf generator for typescript
Expand Down
32 changes: 17 additions & 15 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package generator
import (
"bytes"
"fmt"
"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/data"
"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/registry"
"github.com/Masterminds/sprig"
"strings"
"text/template"

"github.com/Masterminds/sprig"

"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/data"
"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/registry"
)

const tmpl = `
Expand All @@ -17,46 +19,46 @@ const tmpl = `
{{define "enums"}}
{{range .}}export enum {{.Name}} {
{{range .Values}}
{{- range .Values}}
{{.}} = "{{.}}",
{{end}}
{{- end}}
}
{{end}}{{end}}
{{define "messages"}}{{range .}}
{{- if .HasOneOfFields}}
type Base{{.Name}} = {
{{range .NonOneOfFields}}
{{- range .NonOneOfFields}}
{{.Name}}?: {{tsType .}}
{{end}}
{{- end}}
}
export type {{.Name}} = Base{{.Name}}
{{range $groupId, $fields := .OneOfFieldsGroups}} & OneOf<{ {{range $fields}}{{.Name}}: {{tsType .}},{{end}} }>
{{end}}
{{- else -}}
export type {{.Name}} = {
{{range .Fields}}
{{- range .Fields}}
{{.Name}}?: {{tsType .}}
{{end}}
{{- end}}
}
{{end}}
{{end}}{{end}}
{{define "services"}}{{range .}}export class {{.Name}} {
{{range .Methods}}
{{- range .Methods}}
static {{.Name}}(req: {{tsType .Input}}): Promise<gap.FetchState<{{tsType .Output}}>> {
return gap.gapFetchGRPC<{{tsType .Input}}, {{tsType .Output}}>("{{.URL}}", req)
}
{{end}}
{{- end}}
}
{{end}}{{end}}
/*
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
*/
{{if gt (len .Dependencies) 0}}{{- include "dependencies" .Dependencies -}}{{end}}
{{if .Dependencies}}{{- include "dependencies" .StableDependencies -}}{{end}}
{{- if .NeedsOneOfSupport}}
type Absent<T, K extends keyof T> = { [k in Exclude<keyof T, K>]?: undefined };
type OneOf<T> =
Expand All @@ -67,9 +69,9 @@ type OneOf<T> =
: never)
: never);
{{end}}
{{- if gt (len .Enums) 0}}{{include "enums" .Enums}}{{end}}
{{- if gt (len .Messages) 0}}{{include "messages" .Messages}}{{end}}
{{- if gt (len .Services) 0}}{{include "services" .Services}}{{end}}
{{- if .Enums}}{{include "enums" .Enums}}{{end}}
{{- if .Messages}}{{include "messages" .Messages}}{{end}}
{{- if .Services}}{{include "services" .Services}}{{end}}
`

// GetTemplate gets the templates to for the typescript file
Expand Down
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/generator"
"git.sqcorp.co/cash/gap/errors"
"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
log "github.com/sirupsen/logrus" // nolint: depguard
"io/ioutil"
"os"
"strings"

"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
log "github.com/sirupsen/logrus" // nolint: depguard

"git.sqcorp.co/cash/gap/cmd/protoc-gen-grpc-gateway-ts/generator"
"git.sqcorp.co/cash/gap/errors"
)

func decodeReq() *plugin.CodeGeneratorRequest {
Expand Down Expand Up @@ -45,14 +47,14 @@ func main() {
panic(err)
}

log.Info("Starts generating file request")
log.Debug("Starts generating file request")
resp, err := g.Generate(req)
if err != nil {
panic(err)
}

encodeResponse(resp)
log.Info("generation finished")
log.Debug("generation finished")
}

func configureLogging(paramsMap map[string]string) error {
Expand Down

0 comments on commit bbc170d

Please sign in to comment.