|
| 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 | +} |
0 commit comments