Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam010 committed Aug 3, 2019
0 parents commit e054e31
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE project directories - (VS-Code, IntelliJ)
.idea
.vscode

# Generated binaries directory
bin
11 changes: 11 additions & 0 deletions check.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package main;

import "type.proto";

message Check {
string field_1 = 1 [(main.rule1).type.one = "rule1.type.one", (main.rule1).type.two = "rule1.type.two"];
string field_2 = 2 [(main.rule2).type.one = "rule2.type.one", (main.rule2).type.two = "rule2.type.two"];
string field_3 = 3 [(main.rule2).type.two = "rule2.type.two", (main.rule2).type.one = "rule2.type.one"];
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module protoc-gen-sample

go 1.12

require (
github.com/golang/protobuf v1.3.2
github.com/lyft/protoc-gen-star v0.4.11
github.com/spf13/afero v1.2.2
github.com/stretchr/testify v1.3.0 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/lyft/protoc-gen-star v0.4.11 h1:zW6fJQBtCtVeSiO/Kbpzv32GO0J/Z8egSLeohES202w=
github.com/lyft/protoc-gen-star v0.4.11/go.mod h1:mE8fbna26u7aEA2QCVvvfBU/ZrPgocG1206xAFPcs94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"github.com/golang/protobuf/proto"
pgs "github.com/lyft/protoc-gen-star"
)

//TODO: GO111MODULE=on go build -o bin/protoc-gen-sample
//go:generate protoc -I . type.proto --go_out=plugins=grpc:.
//go:generate protoc -I . check.proto --plugin=bin/protoc-gen-sample --sample_out=:.

func main() {
pgs.Init().RegisterModule(&cMod{&pgs.ModuleBase{}}).Render()
}

type cMod struct {
*pgs.ModuleBase
}

func (m *cMod) Name() string {
return "Sample"
}

func (m *cMod) Execute(targets map[string]pgs.File, packages map[string]pgs.Package) []pgs.Artifact {
for _, fl := range targets {
for _, msg := range fl.Messages() {

content := ""

for _, f := range msg.Fields() {

if f.Name().String() == "field_1" {
opt := f.Descriptor().GetOptions()
ext, _ := proto.GetExtension(opt, E_Rule1)
content += fmt.Sprintln(f.Name().String(), ext.(*Rule1).Type)
} else {
opt := f.Descriptor().GetOptions()
ext, _ := proto.GetExtension(opt, E_Rule2)
content += fmt.Sprintln(f.Name().String(), ext.(*Rule2).GetType())
}
}

m.OverwriteGeneratorFile("sample.txt", content)
}
}
return m.Artifacts()
}
3 changes: 3 additions & 0 deletions sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
field_1 one:"rule1.type.one" two:"rule1.type.two"
field_2 two:"rule2.type.two"
field_3 one:"rule2.type.one"
219 changes: 219 additions & 0 deletions type.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions type.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

package main;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
Rule1 rule1 = 999999;
Rule2 rule2 = 999998;
}

message Rule1 {
String type = 1;
}

message Rule2 {
oneof one_of {
String type = 1;
}
}

message String {
string one = 1;
string two = 2;
}

0 comments on commit e054e31

Please sign in to comment.