Skip to content

Commit 0e8f76b

Browse files
authored
Merge pull request stackb#318 from stackb/fix/scala-import-alias
proto_scala_library: support aliasing for scala imports
2 parents 6308ec2 + 95bbeff commit 0e8f76b

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

pkg/rule/rules_scala/scala_library.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func getScalapbImports(files []*protoc.File) []string {
338338
switch namedLiteral.Name {
339339
case "import":
340340
if namedLiteral.Source != "" {
341-
imps = append(imps, namedLiteral.Source)
341+
imps = append(imps, parseScalaImportNamedLiteral(namedLiteral.Source)...)
342342
}
343343
}
344344
}
@@ -362,6 +362,28 @@ func getScalapbImports(files []*protoc.File) []string {
362362
return protoc.DeduplicateAndSort(imps)
363363
}
364364

365+
func parseScalaImportNamedLiteral(lit string) (imports []string) {
366+
ob := strings.Index(lit, "{")
367+
cb := strings.Index(lit, "}")
368+
if ob == -1 || cb == -1 {
369+
return []string{lit}
370+
}
371+
prefix := strings.TrimRight(lit[:ob], ".")
372+
exprs := strings.Split(lit[ob+1:cb], ",")
373+
for _, expr := range exprs {
374+
expr = strings.TrimSpace(expr)
375+
parts := strings.Split(expr, "=>")
376+
if len(parts) == 2 {
377+
source := strings.TrimSpace(parts[0])
378+
imports = append(imports, prefix+"."+source)
379+
} else {
380+
imports = append(imports, prefix+"."+expr)
381+
382+
}
383+
}
384+
return
385+
}
386+
365387
// javaPackageOption is a utility function to seek for the java_package option.
366388
func javaPackageOption(options []proto.Option) (string, bool) {
367389
for _, opt := range options {

pkg/rule/rules_scala/scala_library_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ func TestGetJavaPackageOption(t *testing.T) {
4646
}
4747
}
4848

49+
// TestParseScalaImportNamedLiteral asserts the ability to parse
50+
// a subset of scala import expressions.
51+
func TestParseScalaImportNamedLiteral(t *testing.T) {
52+
for name, tc := range map[string]struct {
53+
imp string
54+
want []string
55+
}{
56+
"degenerate": {
57+
want: []string{""},
58+
},
59+
"single import": {
60+
imp: "a.b.c.Foo",
61+
want: []string{"a.b.c.Foo"},
62+
},
63+
"multiple import": {
64+
imp: "a.b.c.{Foo,Bar}",
65+
want: []string{"a.b.c.Foo", "a.b.c.Bar"},
66+
},
67+
"multiple import +ws": {
68+
imp: "a.b.c.{ Foo , Bar } ",
69+
want: []string{"a.b.c.Foo", "a.b.c.Bar"},
70+
},
71+
"alias import": {
72+
imp: "a.b.c.{ Foo => Fog , Bar => Baz }",
73+
want: []string{"a.b.c.Foo", "a.b.c.Bar"},
74+
},
75+
} {
76+
t.Run(name, func(t *testing.T) {
77+
got := parseScalaImportNamedLiteral(tc.imp)
78+
if diff := cmp.Diff(tc.want, got); diff != "" {
79+
t.Errorf("(-want +got):\n%s", diff)
80+
}
81+
})
82+
}
83+
}
84+
4985
// TestGetScalapbImports shows that an import named in (scalapb.options) works as expected.
5086
func TestGetScalapbImports(t *testing.T) {
5187
for name, tc := range map[string]struct {
@@ -74,6 +110,18 @@ option (scalapb.options) = {
74110
},
75111
want: []string{"corp.common.utils.WithORM"},
76112
},
113+
"with scalapb import (aliased)": {
114+
in: map[string]string{
115+
"foo.proto": `syntax = "proto3";
116+
import "scalapb/scalapb.proto";
117+
118+
option (scalapb.options) = {
119+
import: "corp.common.utils.{WithORM => WithORMAlias}"
120+
};`,
121+
},
122+
want: []string{"corp.common.utils.WithORM"},
123+
},
124+
77125
"with field type": {
78126
in: map[string]string{
79127
"foo.proto": `

0 commit comments

Comments
 (0)