forked from grpc-ecosystem/protoc-gen-grpc-gateway-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
406 additions
and
147 deletions.
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
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,25 @@ | ||
package generator | ||
|
||
// JSONCamelCase converts a snake_case identifier to a camelCase identifier, | ||
// according to the protobuf JSON specification. | ||
// | ||
// Copied from: google.golang.org/protobuf/internal/strs | ||
func JSONCamelCase(s string) string { | ||
var b []byte | ||
var wasUnderscore bool | ||
for i := 0; i < len(s); i++ { // proto identifiers are always ASCII | ||
c := s[i] | ||
if c != '_' { | ||
if wasUnderscore && isASCIILower(c) { | ||
c -= 'a' - 'A' // convert to uppercase | ||
} | ||
b = append(b, c) | ||
} | ||
wasUnderscore = c == '_' | ||
} | ||
return string(b) | ||
} | ||
|
||
func isASCIILower(c byte) bool { | ||
return 'a' <= c && c <= 'z' | ||
} |
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,37 @@ | ||
package generator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dpup/protoc-gen-grpc-gateway-ts/registry" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFieldName(t *testing.T) { | ||
tests := []struct { | ||
useProtoNames bool | ||
input string | ||
want string | ||
}{ | ||
{useProtoNames: false, input: "k8s_field", want: "k8sField"}, | ||
|
||
{useProtoNames: false, input: "foo_bar", want: "fooBar"}, | ||
{useProtoNames: false, input: "foobar", want: "foobar"}, | ||
{useProtoNames: false, input: "foo_bar_baz", want: "fooBarBaz"}, | ||
|
||
{useProtoNames: false, input: "foobar3", want: "foobar3"}, | ||
{useProtoNames: false, input: "foo3bar", want: "foo3bar"}, | ||
{useProtoNames: false, input: "foo3_bar", want: "foo3Bar"}, | ||
{useProtoNames: false, input: "foo_3bar", want: "foo3bar"}, | ||
{useProtoNames: false, input: "foo_3_bar", want: "foo3Bar"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.input, func(t *testing.T) { | ||
r := ®istry.Registry{UseProtoNames: tt.useProtoNames} | ||
fn := fieldName(r) | ||
if got := fn(tt.input); got != tt.want { | ||
assert.Equal(t, got, tt.want, "fieldName(%s) = %s, want %s", tt.input, got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFieldsWithNumbers(t *testing.T) { | ||
// Verifies that the proto generates the field name in the right format. | ||
|
||
createTestFile("fieldsWithNumbers.ts", ` | ||
import {FieldsWithNumbers} from "./names.pb" | ||
export const newFieldsWithNumbers = (f: FieldsWithNumbers) => f.k8sField; | ||
export const result = newFieldsWithNumbers({k8sField: 'test'}); | ||
`) | ||
|
||
defer removeTestFile("fieldsWithNumbers.ts") | ||
|
||
result := runTsc() | ||
assert.Nil(t, result.err) | ||
assert.Equal(t, 0, result.exitCode) | ||
|
||
assert.Empty(t, result.stderr) | ||
assert.Empty(t, result.stdout) | ||
} |
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,62 @@ | ||
package test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var projectRoot = "" | ||
|
||
func init() { | ||
wd, _ := os.Getwd() | ||
for !strings.HasSuffix(wd, "protoc-gen-grpc-gateway-ts") { | ||
wd = filepath.Dir(wd) | ||
} | ||
projectRoot = wd | ||
} | ||
|
||
func runTsc() cmdResult { | ||
cmd := exec.Command("npx", "tsc", "--project", ".", "--noEmit") | ||
cmd.Dir = projectRoot + "/test/testdata/" | ||
|
||
cmdOutput := new(bytes.Buffer) | ||
cmdError := new(bytes.Buffer) | ||
cmd.Stderr = cmdOutput | ||
cmd.Stdout = cmdError | ||
|
||
err := cmd.Run() | ||
|
||
return cmdResult{ | ||
stdout: cmdOutput.String(), | ||
stderr: cmdError.String(), | ||
err: err, | ||
exitCode: cmd.ProcessState.ExitCode(), | ||
} | ||
} | ||
|
||
type cmdResult struct { | ||
stdout string | ||
stderr string | ||
err error | ||
exitCode int | ||
} | ||
|
||
func createTestFile(fname, content string) { | ||
f, err := os.Create(projectRoot + "/test/testdata/" + fname) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
if _, err = f.WriteString(content); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func removeTestFile(fname string) { | ||
if err := os.Remove(projectRoot + "/test/testdata/" + fname); err != nil { | ||
panic(err) | ||
} | ||
} |
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 @@ | ||
syntax = "proto3"; | ||
|
||
package com.squareup.cash.gap; | ||
|
||
// See https://github.com/dpup/protoc-gen-grpc-gateway-ts/issues/1 | ||
message FieldsWithNumbers { | ||
string k8s_field = 1; | ||
} |