diff --git a/internal/proto/proto.go b/internal/proto/proto.go index 9035cee..2faeb0c 100644 --- a/internal/proto/proto.go +++ b/internal/proto/proto.go @@ -25,10 +25,6 @@ func NewProto(filenames []string, importPaths ...string) (p *Proto, err error) { importPaths = append(importPaths, build.Default.SrcDirs()...) importPaths = append(importPaths, "/") - parser := protoparse.Parser{ - ImportPaths: importPaths, - } - // resolve filenames: local filename save as is, remote files are downloads and saves as temp files paths := make([]string, 0, len(filenames)) for _, f := range filenames { @@ -46,12 +42,19 @@ func NewProto(filenames []string, importPaths ...string) (p *Proto, err error) { paths = append(paths, filename) } else if p, _ := filepath.Glob(f); len(p) > 0 { // check pattern + for k := range p { + importPaths = append(importPaths, filepath.Dir(p[k])) + } paths = append(paths, p...) } else { // path paths = append(paths, f) } } + parser := protoparse.Parser{ + ImportPaths: importPaths, + } + descriptors, err := parser.ParseFiles(paths...) if err != nil { return diff --git a/internal/proto/proto_test.go b/internal/proto/proto_test.go index d6b83f8..5763ba6 100644 --- a/internal/proto/proto_test.go +++ b/internal/proto/proto_test.go @@ -11,9 +11,10 @@ import ( "github.com/stretchr/testify/assert" ) -const testFile = "testdata/example.proto" - -var testfiles = []string{testFile} +var testfiles = []string{ + "testdata/example.proto", + "testdata/with_local_import.proto", +} func TestProto_NewProto_Success(t *testing.T) { p, err := NewProto(testfiles) @@ -23,7 +24,7 @@ func TestProto_NewProto_Success(t *testing.T) { func TestProto_NewProto_HTTP_Request(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - file, err := os.Open(testFile) + file, err := os.Open(testfiles[0]) if err != nil { t.Fatal(err) } @@ -70,6 +71,7 @@ func TestProto_FindMessage(t *testing.T) { }{ {"found", args{"HelloRequest"}, "HelloRequest", false}, {"found", args{"example.HelloRequest"}, "HelloRequest", false}, + {"found", args{"WithMoney"}, "WithMoney", false}, {"not found", args{"NotFound"}, "", true}, } diff --git a/internal/proto/testdata/google/type/money.proto b/internal/proto/testdata/google/type/money.proto new file mode 100644 index 0000000..b64f735 --- /dev/null +++ b/internal/proto/testdata/google/type/money.proto @@ -0,0 +1,33 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +// Represents an amount of money with its currency type. +message Money { + // The three-letter currency code defined in ISO 4217. + string currency_code = 1; + + // The whole units of the amount. + // For example if `currencyCode` is `"USD"`, then 1 unit is one US dollar. + int64 units = 2; + + // Number of nano (10^-9) units of the amount. + // The value must be between -999,999,999 and +999,999,999 inclusive. + // If `units` is positive, `nanos` must be positive or zero. + // If `units` is zero, `nanos` can be positive, zero, or negative. + // If `units` is negative, `nanos` must be negative or zero. + // For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000. + int32 nanos = 3; +} diff --git a/internal/proto/testdata/with_local_import.proto b/internal/proto/testdata/with_local_import.proto new file mode 100644 index 0000000..cd97f3a --- /dev/null +++ b/internal/proto/testdata/with_local_import.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package example; + +option go_package = "."; + +import "google/type/money.proto"; + +message WithMoney { + Money amount = 1; +}