Skip to content

Commit

Permalink
Added ability to import local files (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
axl1232 authored Jun 13, 2024
1 parent 11052b5 commit 33a9223
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
11 changes: 7 additions & 4 deletions internal/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions internal/proto/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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},
}

Expand Down
33 changes: 33 additions & 0 deletions internal/proto/testdata/google/type/money.proto
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions internal/proto/testdata/with_local_import.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package example;

option go_package = ".";

import "google/type/money.proto";

message WithMoney {
Money amount = 1;
}

0 comments on commit 33a9223

Please sign in to comment.