18 Commits

Author SHA1 Message Date
moul-bot
d6e112de34 feat: semantic release (#151)
feat: semantic release
2020-01-15 17:40:37 +01:00
Manfred Touron
f3d1bfdee9 feat: semantic release 2020-01-15 17:36:50 +01:00
Manfred Touron
066f210f39 Merge pull request #148 from batazor/fix
Fix index helpers
2019-12-04 11:52:42 +01:00
Victor Login
b244c9cc31 Fix index helpers 2019-11-27 19:30:42 +03:00
Manfred Touron
3a6b47dc92 add parameter file-mode for template at file level (#143)
add parameter file-mode for template at file level
2019-05-30 20:12:52 +02:00
Manfred Touron
5b5d8bf81e Add stringFileOptionsExtension (#144)
Add stringFileOptionsExtension
2019-05-30 20:12:25 +02:00
Manfred Touron
42f1289ddc Update README.md 2019-05-03 13:58:23 +02:00
Gero
2d28c0f341 Add stringFileOptionsExtension 2019-04-26 15:34:25 -03:00
Mike Lee
e4d1fef8fe fmt 2019-03-29 19:24:47 -04:00
Mike Lee
1d71729a7c add parameter file-mode for templates that wish to parse at a file level instead of each service 2019-03-29 13:35:55 -04:00
Manfred Touron
c6eca2356c Merge pull request #142 from moul/dev/moul/rebase-85
Introducing two new helpers, one for returning in templates go_packag…
2019-02-10 22:00:41 +01:00
Manfred Touron
3576fd314a Merge pull request #141 from moul/dev/moul/fix-int64-extension
fix: fix 'int64*Extension' helpers
2019-02-10 21:41:51 +01:00
webii
f43d2a2ac8 Introducing two new helpers, one for returning in templates go_package path, and second one giving only last element of go_package namespace 2019-02-10 21:41:03 +01:00
Manfred Touron
baffaa4b6a Merge pull request #127 from piotrekmonko/fix_intermittent_errors
fixes intermittent error reporting
2019-02-10 21:37:31 +01:00
Manfred Touron
db5e9b7f99 fix: fix 'int64*Extension' helpers 2019-02-10 19:55:05 +01:00
Manfred Touron
be3a88a34a Merge pull request #140 from moul/dev/moul/message-options
feat: add message option helpers
2019-02-08 19:12:27 +01:00
Manfred Touron
bd48cf6168 feat: add message option helpers 2019-02-08 19:07:56 +01:00
Peter Monko
6949b76e12 fixes intermittent error reporting 2018-10-03 13:31:08 +02:00
13 changed files with 293 additions and 8 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@@ -0,0 +1 @@
* @moul

6
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,6 @@
#github: ["moul"]
patreon: moul
open_collective: moul
custom:
- "https://www.buymeacoffee.com/moul"
- "https://manfred.life/donate"

31
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@@ -0,0 +1,31 @@
---
name: Bug report
about: Create a report to help us improve
title: "[BUG] "
labels: bug
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Type '....'
3. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Screenshots / Logs**
If applicable, add screenshots or logs to help explain your problem.
**Versions (please complete the following information, if relevant):**
- Software version: [e.g. v1.2.3, latest, building from sources]
- OS: [e.g. Ubuntu, Mac, iOS, ...]
- Golang version [e.g. 1.13]
**Additional context**
Add any other context about the problem here.

8
.github/ISSUE_TEMPLATE/custom.md vendored Normal file
View File

@@ -0,0 +1,8 @@
---
name: Custom
about: 'Anything else: questions, discussions, thanks, ascii-arts, ...'
title: ''
labels: discussion
assignees: moul
---

View File

@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: "[IDEA] "
labels: enhancement
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

9
.github/PULL_REQUEST_TEMPLATE.md vendored Normal file
View File

@@ -0,0 +1,9 @@
<!--
Thank you for your contribution to this repo!
Before submitting a pull request, please check the following:
- reference any related issue, PR, link
- use the "WIP" title prefix if you need help or more time to finish your PR
you can remove this markdown comment
-->

6
.github/renovate.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"extends": [
"config:base"
],
"groupName": "all"
}

13
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,13 @@
name: Semantic Release
on: push
jobs:
semantic-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: codfish/semantic-release-action@v1
if: github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

8
.releaserc.js Normal file
View File

@@ -0,0 +1,8 @@
module.exports = {
branch: 'master',
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
'@semantic-release/github',
],
};

View File

@@ -153,6 +153,10 @@ $> docker run --rm -v "$(pwd):$(pwd)" -w "$(pwd)" moul/protoc-gen-gotemplate -I.
* [translator](https://github.com/moul/translator): Translator Micro-service using Gettext and Go-Kit
* [acl](https://github.com/moul/acl): ACL micro-service (gRPC/protobuf + http/json)
## See also
* [pbhbs](https://github.com/gponsinet/pbhbs): protobuf gen based on handlebarjs template
## License
MIT

View File

@@ -210,10 +210,8 @@ func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse
case f := <-resultChan:
files = append(files, f)
case err = <-errChan:
}
}
if err != nil {
panic(err)
}
}
return files
}

View File

@@ -94,15 +94,15 @@ var ProtoHelpersFuncMap = template.FuncMap{
"trimstr": func(cutset, s string) string {
return strings.Trim(s, cutset)
},
"index": func(array interface{}, i int32) interface{} {
"index": func(array interface{}, i int) interface{} {
slice := reflect.ValueOf(array)
if slice.Kind() != reflect.Slice {
panic("Error in index(): given a non-slice type")
}
if i < 0 || int(i) >= slice.Len() {
if i < 0 || i >= slice.Len() {
panic("Error in index(): index out of bounds")
}
return slice.Index(int(i)).Interface()
return slice.Index(i).Interface()
},
"add": func(a int, b int) int {
return a + b
@@ -146,10 +146,14 @@ var ProtoHelpersFuncMap = template.FuncMap{
"leadingComment": leadingComment,
"trailingComment": trailingComment,
"leadingDetachedComments": leadingDetachedComments,
"stringFileOptionsExtension": stringFileOptionsExtension,
"stringMessageExtension": stringMessageExtension,
"stringFieldExtension": stringFieldExtension,
"int64FieldExtension": int64FieldExtension,
"int64MessageExtension": int64MessageExtension,
"stringMethodOptionsExtension": stringMethodOptionsExtension,
"boolMethodOptionsExtension": boolMethodOptionsExtension,
"boolMessageExtension": boolMessageExtension,
"boolFieldExtension": boolFieldExtension,
"isFieldMap": isFieldMap,
"fieldMapKeyType": fieldMapKeyType,
@@ -157,6 +161,8 @@ var ProtoHelpersFuncMap = template.FuncMap{
"replaceDict": replaceDict,
"setStore": setStore,
"getStore": getStore,
"goPkg": goPkg,
"goPkgLastElement": goPkgLastElement,
}
var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location
@@ -343,6 +349,46 @@ func stringMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorP
return *str
}
// stringFileOptionsExtension extracts file options of a string type.
// To define your own extensions see:
// https://developers.google.com/protocol-buffers/docs/proto#customoptions
// Typically the fieldID of private extensions should be in the range:
// 50000-99999
func stringFileOptionsExtension(fieldID int32, f *descriptor.FileDescriptorProto) string {
if f == nil {
return ""
}
if f.Options == nil {
return ""
}
var extendedType *descriptor.FileOptions
var extensionType *string
eds := proto.RegisteredExtensions(f.Options)
if eds[fieldID] == nil {
ed := &proto.ExtensionDesc{
ExtendedType: extendedType,
ExtensionType: extensionType,
Field: fieldID,
Tag: fmt.Sprintf("bytes,%d", fieldID),
}
proto.RegisterExtension(ed)
eds = proto.RegisteredExtensions(f.Options)
}
ext, err := proto.GetExtension(f.Options, eds[fieldID])
if err != nil {
return ""
}
str, ok := ext.(*string)
if !ok {
return ""
}
return *str
}
func stringFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) string {
if f == nil {
return ""
@@ -386,7 +432,7 @@ func int64FieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) int6
return 0
}
var extendedType *descriptor.FieldOptions
var extensionType *string
var extensionType *int64
eds := proto.RegisteredExtensions(f.Options)
if eds[fieldID] == nil {
@@ -394,7 +440,7 @@ func int64FieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) int6
ExtendedType: extendedType,
ExtensionType: extensionType,
Field: fieldID,
Tag: fmt.Sprintf("bytes,%d", fieldID),
Tag: fmt.Sprintf("varint,%d", fieldID),
}
proto.RegisterExtension(ed)
eds = proto.RegisteredExtensions(f.Options)
@@ -413,6 +459,76 @@ func int64FieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) int6
return *i
}
func int64MessageExtension(fieldID int32, f *descriptor.DescriptorProto) int64 {
if f == nil {
return 0
}
if f.Options == nil {
return 0
}
var extendedType *descriptor.MessageOptions
var extensionType *int64
eds := proto.RegisteredExtensions(f.Options)
if eds[fieldID] == nil {
ed := &proto.ExtensionDesc{
ExtendedType: extendedType,
ExtensionType: extensionType,
Field: fieldID,
Tag: fmt.Sprintf("varint,%d", fieldID),
}
proto.RegisterExtension(ed)
eds = proto.RegisteredExtensions(f.Options)
}
ext, err := proto.GetExtension(f.Options, eds[fieldID])
if err != nil {
return 0
}
i, ok := ext.(*int64)
if !ok {
return 0
}
return *i
}
func stringMessageExtension(fieldID int32, f *descriptor.DescriptorProto) string {
if f == nil {
return ""
}
if f.Options == nil {
return ""
}
var extendedType *descriptor.MessageOptions
var extensionType *string
eds := proto.RegisteredExtensions(f.Options)
if eds[fieldID] == nil {
ed := &proto.ExtensionDesc{
ExtendedType: extendedType,
ExtensionType: extensionType,
Field: fieldID,
Tag: fmt.Sprintf("bytes,%d", fieldID),
}
proto.RegisterExtension(ed)
eds = proto.RegisteredExtensions(f.Options)
}
ext, err := proto.GetExtension(f.Options, eds[fieldID])
if err != nil {
return ""
}
str, ok := ext.(*string)
if !ok {
return ""
}
return *str
}
func boolMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorProto) bool {
if f == nil {
return false
@@ -483,6 +599,41 @@ func boolFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) bool
return *b
}
func boolMessageExtension(fieldID int32, f *descriptor.DescriptorProto) bool {
if f == nil {
return false
}
if f.Options == nil {
return false
}
var extendedType *descriptor.MessageOptions
var extensionType *bool
eds := proto.RegisteredExtensions(f.Options)
if eds[fieldID] == nil {
ed := &proto.ExtensionDesc{
ExtendedType: extendedType,
ExtensionType: extensionType,
Field: fieldID,
Tag: fmt.Sprintf("varint,%d", fieldID),
}
proto.RegisterExtension(ed)
eds = proto.RegisteredExtensions(f.Options)
}
ext, err := proto.GetExtension(f.Options, eds[fieldID])
if err != nil {
return false
}
b, ok := ext.(*bool)
if !ok {
return false
}
return *b
}
func init() {
for k, v := range sprig.TxtFuncMap() {
ProtoHelpersFuncMap[k] = v
@@ -1164,3 +1315,13 @@ func replaceDict(src string, dict map[string]interface{}) string {
}
return src
}
func goPkg(f *descriptor.FileDescriptorProto) string {
return f.Options.GetGoPackage()
}
func goPkgLastElement(f *descriptor.FileDescriptorProto) string {
pkg := goPkg(f)
pkgSplitted := strings.Split(pkg, "/")
return pkgSplitted[len(pkgSplitted)-1]
}

20
main.go
View File

@@ -48,6 +48,7 @@ func main() {
debug = false
all = false
singlePackageMode = false
fileMode = false
)
if parameter := g.Request.GetParameter(); parameter != "" {
for _, param := range strings.Split(parameter, ",") {
@@ -85,6 +86,14 @@ func main() {
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
case "file-mode":
switch strings.ToLower(parts[1]) {
case boolTrue, "t":
fileMode = true
case boolFalse, "f":
default:
log.Printf("Err: invalid value for file-mode: %q", parts[1])
}
default:
log.Printf("Err: unknown parameter: %q", param)
}
@@ -125,6 +134,17 @@ func main() {
continue
}
if fileMode {
if s := file.GetService(); s != nil && len(s) > 0 {
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
for _, tmpl := range encoder.Files() {
concatOrAppend(tmpl)
}
}
continue
}
for _, service := range file.GetService() {
encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
for _, tmpl := range encoder.Files() {