-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 05c0c80
Showing
11 changed files
with
827 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
proto.lock |
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,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2018 Steve Manuel <nilslice@gmail.com> | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,38 @@ | ||
### Work In Progress | ||
|
||
# protolock | ||
|
||
Track your `.proto` files and prevent incompatible changes to field names and numbers. | ||
|
||
## Why | ||
|
||
Ever _accidentally_ break your API compatibility while you're busy fixing problems? You may have forgotten to reserve the field number of a message or re-order fields after removing a property. A new team member may not be familiar with the backward-compatibility of Protocol Buffers and make an easy mistake. | ||
|
||
`protolock` attempts to help prevent this from happening. | ||
|
||
## Usage | ||
|
||
Similar in concept to the higher-level features of `git`, track and/or prevent changes to your `.proto` files. | ||
|
||
1. Initialize your repository: | ||
|
||
$ protolock init | ||
|
||
3. Add changes to protobuf messages or services: | ||
|
||
$ protolock commit | ||
|
||
2. Check that no breaking changes were made: | ||
|
||
$ protolock status | ||
|
||
4. Integrate into your protobuf compilation step: | ||
|
||
$ protolock status && protoc --I ... | ||
|
||
In all, prevent yourself from compiling your protobufs and generating code if breaking changes have been made. | ||
|
||
--- | ||
**Recommended:** commit the output `proto.lock` file into your version control system | ||
|
||
--- |
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,91 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/nilslice/protolock" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if len(os.Args) < 2 { | ||
os.Exit(0) | ||
} | ||
|
||
switch os.Args[1] { | ||
case "init": | ||
r, err := protolock.Init() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
err = saveToLockFile(r) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
case "status": | ||
report, err := protolock.Status() | ||
if err != nil { | ||
if len(report.Warnings) > 0 { | ||
for _, w := range report.Warnings { | ||
fmt.Fprintf( | ||
os.Stdout, | ||
"%s [%s]\n", | ||
w.Message, w.Filepath, | ||
) | ||
} | ||
|
||
term := "issue" | ||
if len(report.Warnings) > 1 { | ||
term = "issues" | ||
} | ||
fmt.Fprintf( | ||
os.Stdout, | ||
"Encountered %d %s during analysis.\n", | ||
len(report.Warnings), term, | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println(err) | ||
} | ||
|
||
case "commit": | ||
r, err := protolock.Commit() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
err = saveToLockFile(r) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
default: | ||
fmt.Println(os.Args) | ||
} | ||
} | ||
|
||
func saveToLockFile(r io.Reader) error { | ||
lockfile, err := os.Create("proto.lock") | ||
if err != nil { | ||
return err | ||
} | ||
defer lockfile.Close() | ||
|
||
_, err = io.Copy(lockfile, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,22 @@ | ||
package protolock | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// Commit will return an io.Reader with the lock representation data for caller to | ||
// use as needed. | ||
func Commit() (io.Reader, error) { | ||
if _, err := os.Stat(lockFileName); err != nil && os.IsNotExist(err) { | ||
fmt.Println(`no proto.lock found, please run "init" first`) | ||
os.Exit(1) | ||
} | ||
updated, err := getUpdatedLock() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return readerFromProtolock(updated) | ||
} |
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,35 @@ | ||
package protolock | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const protoSuffix = ".proto" | ||
|
||
// Init will return an io.Reader with the lock representation data for caller to | ||
// use as needed. | ||
func Init() (io.Reader, error) { | ||
if _, err := os.Stat(lockFileName); err == nil && !os.IsNotExist(err) { | ||
fmt.Println(`a proto.lock file was already found, use "commit" to update`) | ||
os.Exit(1) | ||
} | ||
updated, err := getUpdatedLock() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return readerFromProtolock(updated) | ||
} | ||
|
||
func readerFromProtolock(lock *Protolock) (io.Reader, error) { | ||
b, err := json.MarshalIndent(lock, "", " ") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return strings.NewReader(string(b)), nil | ||
} |
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,110 @@ | ||
package protolock | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const simpleProto = `syntax = "proto3"; | ||
package test; | ||
message Channel { | ||
int64 id = 1; | ||
string name = 2; | ||
string description = 3; | ||
} | ||
message NextRequest {} | ||
message PreviousRequest {} | ||
service ChannelChanger { | ||
rpc Next(stream NextRequest) returns (Channel); | ||
rpc Previous(PreviousRequest) returns (stream Channel); | ||
} | ||
` | ||
|
||
const noUsingReservedFieldsProto = `syntax = "proto3"; | ||
package test; | ||
message Channel { | ||
reserved 4, 8 to 11; | ||
reserved "foo", "bar"; | ||
int64 id = 1; | ||
string name = 2; | ||
string description = 3; | ||
} | ||
message NextRequest {} | ||
message PreviousRequest {} | ||
service ChannelChanger { | ||
rpc Next(stream NextRequest) returns (Channel); | ||
rpc Previous(PreviousRequest) returns (stream Channel); | ||
} | ||
` | ||
|
||
const usingReservedFieldsProto = `syntax = "proto3"; | ||
package test; | ||
message Channel { | ||
int64 id = 1; | ||
string name = 2; | ||
string description = 3; | ||
string foo = 4; | ||
bool bar = 5; | ||
} | ||
message NextRequest {} | ||
message PreviousRequest {} | ||
service ChannelChanger { | ||
rpc Next(stream NextRequest) returns (Channel); | ||
rpc Previous(PreviousRequest) returns (stream Channel); | ||
} | ||
` | ||
|
||
func TestParseOnReader(t *testing.T) { | ||
r := strings.NewReader(simpleProto) | ||
_, err := parse(r) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestCompareWithKnownIssue(t *testing.T) { | ||
cur := strings.NewReader(noUsingReservedFieldsProto) | ||
upd := strings.NewReader(usingReservedFieldsProto) | ||
|
||
curEntry, err := parse(cur) | ||
assert.NoError(t, err) | ||
curLock := Protolock{ | ||
Definitions: []Definition{ | ||
{ | ||
Filepath: "NA", | ||
Def: curEntry, | ||
}, | ||
}, | ||
} | ||
|
||
updEntry, err := parse(upd) | ||
assert.NoError(t, err) | ||
updLock := Protolock{ | ||
Definitions: []Definition{ | ||
{ | ||
Filepath: "NA", | ||
Def: updEntry, | ||
}, | ||
}, | ||
} | ||
|
||
report, err := compare(curLock, updLock) | ||
assert.Error(t, err) | ||
assert.Len(t, report.Warnings, 3) | ||
} | ||
|
||
func toJSON(t *testing.T, v interface{}) []byte { | ||
b, err := json.Marshal(v) | ||
assert.NoError(t, err) | ||
return b | ||
} |
Oops, something went wrong.