-
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.
Add option patcher to smooth compilation (#7)
- Loading branch information
1 parent
cb9ff02
commit 945b81c
Showing
14 changed files
with
267 additions
and
16 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 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
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,52 @@ | ||
package patcher | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/yoheimuta/go-protoparser/v4/interpret/unordered" | ||
) | ||
import "github.com/yoheimuta/go-protoparser/v4" | ||
|
||
type GoPackagePatcher struct { | ||
goModule string | ||
} | ||
|
||
func NewGoPackagePatcher(goModule string) *GoPackagePatcher { | ||
return &GoPackagePatcher{ | ||
goModule: goModule, | ||
} | ||
} | ||
|
||
func (p *GoPackagePatcher) Patch(outputPath, content string) (string, error) { | ||
parsed, err := protoparser.Parse(strings.NewReader(content)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
proto, err := unordered.InterpretProto(parsed) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dirs := strings.Split(outputPath, "/") | ||
goPackage := fmt.Sprintf(`option go_package = "%s/%s;%s";`, p.goModule, outputPath, dirs[len(dirs)-1]) | ||
|
||
for _, option := range proto.ProtoBody.Options { | ||
if option.OptionName == "go_package" { | ||
// break by lines | ||
// option.Meta.Pos.Line as the line to change | ||
splitted := strings.Split(content, "\n") | ||
splitted[option.Meta.Pos.Line-1] = goPackage | ||
return strings.Join(splitted, "\n"), nil | ||
} | ||
} | ||
|
||
// if no go_package option, add it | ||
splitted := strings.Split(content, "\n") | ||
// add the element after syntax line | ||
line := proto.Syntax.Meta.LastPos.Line | ||
splitted = append(splitted[:line], append([]string{goPackage}, splitted[line:]...)...) | ||
|
||
return strings.Join(splitted, "\n"), 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,101 @@ | ||
package patcher | ||
|
||
import "testing" | ||
|
||
const ( | ||
noPackageProtoFile = ` | ||
syntax = "proto3"; | ||
package pbufregistry.v1; | ||
// Module is a module registered in the registry. | ||
message Module { | ||
} | ||
` | ||
noPackageProtoFilePatched = ` | ||
syntax = "proto3"; | ||
option go_package = "github.com/pbufio/pbuf-registry/api/v1;v1"; | ||
package pbufregistry.v1; | ||
// Module is a module registered in the registry. | ||
message Module { | ||
} | ||
` | ||
|
||
withPackageProtoFile = ` | ||
syntax = "proto3"; | ||
package pbufregistry.v1; | ||
option go_package = "pbufregistry/api/v2;v2"; | ||
// Module is a module registered in the registry. | ||
message Module { | ||
} | ||
` | ||
|
||
withPackageProtoFilePatched = ` | ||
syntax = "proto3"; | ||
package pbufregistry.v1; | ||
option go_package = "github.com/pbufio/pbuf-registry/api/v1;v1"; | ||
// Module is a module registered in the registry. | ||
message Module { | ||
} | ||
` | ||
) | ||
|
||
func TestGoPackagePatcher_Patch(t *testing.T) { | ||
type fields struct { | ||
goModule string | ||
} | ||
type args struct { | ||
outputPath string | ||
content string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no package", | ||
fields: fields{ | ||
goModule: "github.com/pbufio/pbuf-registry", | ||
}, | ||
args: args{ | ||
outputPath: "api/v1", | ||
content: noPackageProtoFile, | ||
}, | ||
want: noPackageProtoFilePatched, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "with package", | ||
fields: fields{ | ||
goModule: "github.com/pbufio/pbuf-registry", | ||
}, | ||
args: args{ | ||
outputPath: "api/v1", | ||
content: withPackageProtoFile, | ||
}, | ||
want: withPackageProtoFilePatched, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := NewGoPackagePatcher(tt.fields.goModule) | ||
|
||
got, err := p.Patch(tt.args.outputPath, tt.args.content) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Patch() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("Patch() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,16 @@ | ||
package patcher | ||
|
||
type Patcher interface { | ||
Patch(outputPath, content string) (string, error) | ||
} | ||
|
||
func ApplyPatchers(patchers []Patcher, outputPath string, content string) (string, error) { | ||
for _, patcher := range patchers { | ||
var err error | ||
content, err = patcher.Patch(outputPath, content) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return content, nil | ||
} |
Oops, something went wrong.