Skip to content

Commit a126911

Browse files
Merge pull request #2 from loopholelabs/proto-gen
feat: Add encoding/decoding generation from proto files
2 parents 6f05cc8 + a54b33e commit a126911

File tree

23 files changed

+1022
-1
lines changed

23 files changed

+1022
-1
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/loopholelabs/polyglot-go
22

33
go 1.18
44

5-
require github.com/stretchr/testify v1.7.5
5+
require (
6+
github.com/stretchr/testify v1.7.5
7+
google.golang.org/protobuf v1.28.0
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
5+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
6+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/loopholelabs/frpc-go v0.5.1 h1:PqOffcaz6eCF/fqKby+W+A3TaxnqyuWGA87y/3w0juk=
8+
github.com/loopholelabs/frpc-go v0.5.1/go.mod h1:F0TG5nvc7GJRr5cmR2k36P4id+glkKYcy59hK877CM0=
49
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
510
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
611
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
712
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
813
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
914
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
1015
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
16+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
17+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
18+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
19+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
20+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
1121
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1222
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1323
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/version/version.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2022 Loophole Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
const (
20+
Version = "v0.3.1"
21+
)

pkg/generator/defaults.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2022 Loophole Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package generator
18+
19+
const (
20+
extension = ".polyglot.go"
21+
pointer = "*"
22+
mapSuffix = "Map"
23+
slice = "[]"
24+
polyglotAnyKind = "polyglot.AnyKind"
25+
)

pkg/generator/file.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2022 Loophole Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package generator
18+
19+
type File interface {
20+
P(v ...interface{})
21+
}

pkg/generator/generator.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright 2022 Loophole Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package generator
18+
19+
import (
20+
"github.com/loopholelabs/polyglot-go/pkg/utils"
21+
"text/template"
22+
23+
"github.com/loopholelabs/polyglot-go/internal/version"
24+
"github.com/loopholelabs/polyglot-go/templates"
25+
"google.golang.org/protobuf/compiler/protogen"
26+
"google.golang.org/protobuf/proto"
27+
"google.golang.org/protobuf/types/pluginpb"
28+
)
29+
30+
type Generator struct {
31+
options *protogen.Options
32+
templ *template.Template
33+
CustomFields func() string
34+
CustomEncode func() string
35+
CustomDecode func() string
36+
}
37+
38+
func New() *Generator {
39+
var g *Generator
40+
templ := template.Must(template.New("main").Funcs(template.FuncMap{
41+
"CamelCase": utils.CamelCaseFullName,
42+
"CamelCaseName": utils.CamelCaseName,
43+
"MakeIterable": utils.MakeIterable,
44+
"Counter": utils.Counter,
45+
"FirstLowerCase": utils.FirstLowerCase,
46+
"FirstLowerCaseName": utils.FirstLowerCaseName,
47+
"FindValue": findValue,
48+
"GetKind": getKind,
49+
"GetLUTEncoder": getLUTEncoder,
50+
"GetLUTDecoder": getLUTDecoder,
51+
"GetEncodingFields": getEncodingFields,
52+
"GetDecodingFields": getDecodingFields,
53+
"GetKindLUT": getKindLUT,
54+
"CustomFields": func() string {
55+
return g.CustomFields()
56+
},
57+
"CustomEncode": func() string {
58+
return g.CustomEncode()
59+
},
60+
"CustomDecode": func() string {
61+
return g.CustomDecode()
62+
},
63+
}).ParseFS(templates.FS, "*"))
64+
g = &Generator{
65+
options: &protogen.Options{
66+
ParamFunc: func(name string, value string) error { return nil },
67+
ImportRewriteFunc: func(path protogen.GoImportPath) protogen.GoImportPath { return path },
68+
},
69+
templ: templ,
70+
CustomEncode: func() string { return "" },
71+
CustomDecode: func() string { return "" },
72+
CustomFields: func() string { return "" },
73+
}
74+
return g
75+
}
76+
77+
func (*Generator) UnmarshalRequest(buf []byte) (*pluginpb.CodeGeneratorRequest, error) {
78+
req := new(pluginpb.CodeGeneratorRequest)
79+
return req, proto.Unmarshal(buf, req)
80+
}
81+
82+
func (*Generator) MarshalResponse(res *pluginpb.CodeGeneratorResponse) ([]byte, error) {
83+
return proto.Marshal(res)
84+
}
85+
86+
func (g *Generator) Generate(req *pluginpb.CodeGeneratorRequest) (res *pluginpb.CodeGeneratorResponse, err error) {
87+
plugin, err := g.options.New(req)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
for _, f := range plugin.Files {
93+
if !f.Generate {
94+
continue
95+
}
96+
genFile := plugin.NewGeneratedFile(FileName(f.GeneratedFilenamePrefix), f.GoImportPath)
97+
98+
packageName := string(f.Desc.Package().Name())
99+
if packageName == "" {
100+
packageName = string(f.GoPackageName)
101+
}
102+
103+
err = g.ExecuteTemplate(genFile, f, packageName, true)
104+
if err != nil {
105+
return nil, err
106+
}
107+
}
108+
109+
return plugin.Response(), nil
110+
}
111+
112+
func (g *Generator) ExecuteTemplate(
113+
genFile *protogen.GeneratedFile,
114+
protoFile *protogen.File,
115+
packageName string,
116+
header bool,
117+
) error {
118+
return g.templ.ExecuteTemplate(genFile, "base.templ", map[string]interface{}{
119+
"pluginVersion": version.Version,
120+
"sourcePath": protoFile.Desc.Path(),
121+
"package": packageName,
122+
"requiredImports": requiredImports,
123+
"enums": protoFile.Desc.Enums(),
124+
"messages": protoFile.Desc.Messages(),
125+
"header": header,
126+
})
127+
}

pkg/generator/headers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2022 Loophole Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package generator
18+
19+
import (
20+
"github.com/loopholelabs/polyglot-go/pkg/utils"
21+
)
22+
23+
func FileName(name string) string {
24+
return utils.AppendString(name, extension)
25+
}

pkg/generator/imports.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2022 Loophole Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package generator
18+
19+
var (
20+
requiredImports = []string{
21+
"github.com/loopholelabs/polyglot-go",
22+
"errors",
23+
}
24+
)

0 commit comments

Comments
 (0)