-
-
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.
Merge pull request #81 from bavix/remove-fix-gopackage.sh
Remove fix gopackage.sh
- Loading branch information
Showing
5 changed files
with
169 additions
and
63 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 was deleted.
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,50 @@ | ||
package patcher | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
optionGoPackageRegexp = regexp.MustCompile("option go_package.+;\n?") | ||
syntaxRegexp = regexp.MustCompile("syntax.+;\n?") | ||
ErrSyntaxNotFound = errors.New("proto syntax not found") | ||
) | ||
|
||
type fileWriterWrapper struct { | ||
writer io.Writer | ||
packageName string | ||
} | ||
|
||
func NewWriterWrapper(writer io.Writer, packageName string) io.Writer { | ||
return &fileWriterWrapper{writer: writer, packageName: packageName} | ||
} | ||
|
||
func (f *fileWriterWrapper) Write(p []byte) (int, error) { | ||
const ( | ||
syntaxIndexes = 2 | ||
prefix = "github.com/bavix/gripmock" | ||
) | ||
|
||
n := len(p) | ||
goPackage := []byte(fmt.Sprintf("option go_package = \"%s/%s\";\n", prefix, f.packageName)) | ||
|
||
if optionGoPackageRegexp.Match(p) { | ||
_, err := f.writer.Write(optionGoPackageRegexp.ReplaceAll(p, goPackage)) | ||
|
||
return n, err | ||
} | ||
|
||
indexes := syntaxRegexp.FindIndex(p) | ||
if len(indexes) != syntaxIndexes { | ||
return 0, ErrSyntaxNotFound | ||
} | ||
|
||
_, err := f.writer.Write( | ||
append(p[:indexes[1]], append(goPackage, p[indexes[1]:]...)...), | ||
) | ||
|
||
return n, 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,84 @@ | ||
package patcher_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/bavix/gripmock/internal/pkg/patcher" | ||
) | ||
|
||
func TestWriterWrapper_OptionUpdate(t *testing.T) { | ||
pile, err := os.OpenFile( | ||
"./../../../protogen/example/multi-files/file1.proto", | ||
os.O_RDONLY, | ||
0444, | ||
) | ||
require.NoError(t, err) | ||
|
||
var result bytes.Buffer | ||
|
||
tmp := patcher.NewWriterWrapper(&result, "patcher/v1") | ||
|
||
_, err = io.Copy(tmp, pile) | ||
require.NoError(t, err) | ||
require.NoError(t, pile.Close()) | ||
|
||
require.Contains(t, result.String(), "github.com/bavix/gripmock/protogen/patcher/v1") | ||
} | ||
|
||
func TestWriterWrapper_OptionInsert(t *testing.T) { | ||
pile := bytes.NewReader([]byte(`syntax = "proto3"; | ||
package multifiles; | ||
service Gripmock1 { | ||
rpc SayHello (Request1) returns (Reply1); | ||
} | ||
message Request1 { | ||
string name = 1; | ||
} | ||
message Reply1 { | ||
string message = 1; | ||
int32 return_code = 2; | ||
}`)) | ||
|
||
var result bytes.Buffer | ||
|
||
tmp := patcher.NewWriterWrapper(&result, "patcher/v2") | ||
|
||
_, err := io.Copy(tmp, pile) | ||
require.NoError(t, err) | ||
|
||
require.Contains(t, result.String(), "github.com/bavix/gripmock/protogen/patcher/v2") | ||
} | ||
|
||
func TestWriterWrapper_SyntaxError(t *testing.T) { | ||
pile := bytes.NewReader([]byte(` | ||
package multifiles; | ||
service Gripmock1 { | ||
rpc SayHello (Request1) returns (Reply1); | ||
} | ||
message Request1 { | ||
string name = 1; | ||
} | ||
message Reply1 { | ||
string message = 1; | ||
int32 return_code = 2; | ||
}`)) | ||
|
||
var result bytes.Buffer | ||
|
||
tmp := patcher.NewWriterWrapper(&result, "patcher/v3") | ||
|
||
_, err := io.Copy(tmp, pile) | ||
require.ErrorIs(t, patcher.ErrSyntaxNotFound, err) | ||
} |