-
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #82 from bavix/nightly
[2.4.x] Improving internal code
- Loading branch information
Showing
12 changed files
with
217 additions
and
139 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 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
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 | ||
} |
Oops, something went wrong.