Skip to content

Commit

Permalink
Allow proto files to have a separate configurable idl root
Browse files Browse the repository at this point in the history
 - fix lint
  • Loading branch information
kaialang committed Aug 2, 2023
1 parent b7add15 commit b9e3a70
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
5 changes: 3 additions & 2 deletions codegen/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ func newTestPackageHelper(t *testing.T) *PackageHelper {
},
TraceKey: "trace-key",
ModuleIdlSubDir: map[string]string{"endpoints": "endpoints-idl", "default": "clients-idl"},
ProtoRelIdlRootDir: "./bazel-out/idl",
}

h, err := NewPackageHelper(
Expand Down Expand Up @@ -540,8 +541,8 @@ func TestGRPCClientNewClientSpec(t *testing.T) {
},
}
h := newTestPackageHelper(t)

idlFile := filepath.Join(h.IdlPath(), h.GetModuleIdlSubDir(false), "clients/echo/echo.proto")
assert.Equal(t, "/go/src/github.com/uber/zanzibar/examples/example-gateway/bazel-out/idl", h.ProtoIdlPath())
idlFile := filepath.Join(h.ProtoIdlPath(), h.GetModuleIdlSubDir(false), "clients/echo/echo.proto")
expectedSpec := &ClientSpec{
ModuleSpec: nil,
YAMLFile: instance.YAMLFileName,
Expand Down
1 change: 1 addition & 0 deletions codegen/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (p *PackageHelperOptions) protoRelIdlRootDir() string {
if p.ProtoRelIdlRootDir != "" {
return p.ProtoRelIdlRootDir
}

return p.relIdlRootDir()
}

Expand Down
37 changes: 37 additions & 0 deletions codegen/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var fooThrift = filepath.Join(
"examples/example-gateway/idl/",
"clients-idl/clients/foo/foo.thrift")

var fooProto = filepath.Join(
os.Getenv("GOPATH"),
"/src/github.com/uber/zanzibar/",
"examples/example-gateway/bazel-out/idl/",
"clients-idl/clients/foo/foo.proto")

var testCopyrightHeader = `// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -74,6 +80,7 @@ func newPackageHelper(t *testing.T) *codegen.PackageHelper {
".proto": packageRoot + "/build/gen-code",
},
TraceKey: "trace-key",
ProtoRelIdlRootDir: "./bazel-out/idl",
}

h, err := codegen.NewPackageHelper(
Expand All @@ -87,14 +94,39 @@ func newPackageHelper(t *testing.T) *codegen.PackageHelper {
return h
}

func TestProtoRelIdlRootDirInitializeToIDL(t *testing.T) {
relativeGatewayPath := "../examples/example-gateway"
absGatewayPath, err := filepath.Abs(relativeGatewayPath)
assert.NoError(t, err)

packageRoot := "foo/"
options := &codegen.PackageHelperOptions{}

h, err := codegen.NewPackageHelper(
packageRoot,
absGatewayPath,
options,
)

assert.NoError(t, err)
assert.Equal(t, "/go/src/github.com/uber/zanzibar/examples/example-gateway/idl", h.ProtoIdlPath())
}

func TestImportPath(t *testing.T) {
h := newPackageHelper(t)
p, err := h.TypeImportPath(fooThrift)
assert.Nil(t, err, "should not return error")
assert.Equal(t, "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/clients-idl/clients/foo/foo",
p, "wrong type import path")

p, err = h.TypeImportPath(fooProto)
assert.Nil(t, err, "should not return error")
assert.Equal(t, "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/clients-idl/clients/foo",
p, "wrong type import path")

_, err = h.TypeImportPath("/Users/xxx/go/src/github.com/uber/zanzibar/examples/example-gateway/build/idl/github.com/uber/zanzibar/clients/foo/foo.go")
assert.Error(t, err, "should return error for not a thrift file")

_, err = h.TypeImportPath("/Users/xxx/go/src/github.com/uber/zanzibar/examples/example-gateway/build/zanzibar/clients/foo/foo.thrift")
assert.Error(t, err, "should return error for not in IDL dir")
}
Expand All @@ -104,6 +136,11 @@ func TestTypePackageName(t *testing.T) {
packageName, err := h.TypePackageName(fooThrift)
assert.Nil(t, err, "should not return error")
assert.Equal(t, "clientsIDlClientsFooFoo", packageName, "wrong package name")

packageName, err = h.TypePackageName(fooProto)
assert.Nil(t, err, "should not return error")
assert.Equal(t, "clientsIDlClientsFooFoo", packageName, "wrong package name")

_, err = h.TypeImportPath("/Users/xxx/go/src/github.com/uber/zanzibar/examples/example-gateway/build/idl/github.com/uber/zanzibar/clients/foo/foo.txt")
assert.Error(t, err, "should return error for not a thrift file")
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func TestModuleSpecNoThriftExist(t *testing.T) {
}

func TestProtoModuleSpec(t *testing.T) {
echoProto := "../examples/example-gateway/idl/clients-idl/clients/echo/echo.proto"
echoProto := "../examples/example-gateway/bazel-out/idl/clients-idl/clients/echo/echo.proto"
_, err := codegen.NewProtoModuleSpec(echoProto, false, newPackageHelper(t))
assert.NoError(t, err, "unable to parse the proto file")
}

func TestProtoModuleSpecParseError(t *testing.T) {
tmpFile, err := ioutil.TempFile("../examples/example-gateway/idl/clients-idl/clients/", "temp*.proto")
tmpFile, err := ioutil.TempFile("../examples/example-gateway/bazel-out/idl/clients-idl/clients/", "temp*.proto")
assert.NoError(t, err, "failed to create temp file")
defer func(name string) {
_ = os.Remove(name)
Expand Down
1 change: 1 addition & 0 deletions examples/example-gateway/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defaultHeaders:
packageRoot: github.com/uber/zanzibar/examples/example-gateway
targetGenDir: ./build
idlRootDir: ./idl
protoIdlRootDir: ./bazel-out/idl
moduleIdlSubDir:
endpoints: endpoints-idl
default: clients-idl
Expand Down

0 comments on commit b9e3a70

Please sign in to comment.