Add a new 'helpers' example

This commit is contained in:
Manfred Touron 2017-12-19 15:55:33 +01:00
parent 9f831eb4de
commit 5da3d00df2
5 changed files with 150 additions and 0 deletions

View File

@ -19,6 +19,7 @@ test: install
cd examples/sitemap && make cd examples/sitemap && make
cd examples/go-generate && make cd examples/go-generate && make
cd examples/single-package-mode && make cd examples/single-package-mode && make
cd examples/helpers && make
# cd examples/go-kit && make # cd examples/go-kit && make
.PHONY: docker.build .PHONY: docker.build

13
examples/helpers/Makefile Normal file
View File

@ -0,0 +1,13 @@
.PHONY: build
build:
mkdir -p output
protoc -I. --gotemplate_out=template_dir=.,debug=true:. *.proto
.PHONY: re
re: clean build
.PHONY: clean
clean:
rm -rf output

View File

@ -0,0 +1,55 @@
# Common variables
{{.File.Name}}: helpers.proto
{{.File.Name | upper}}: HELPERS.PROTO
{{.File.Package | base | replace "." "-"}} dummy
{{$packageDir := .File.Name | dir}}{{$packageDir}} .
{{$packageName := .File.Name | base | replace ".proto" ""}}{{$packageName}} helpers
{{$packageImport := .File.Package | replace "." "_"}}{{$packageImport}} dummy
{{$namespacedPackage := .File.Package}}{{$namespacedPackage}} dummy
{{$currentFile := .File.Name | getProtoFile}}{{$currentFile}} <nil>
{{- /*{{- $currentPackageName := $currentFile.GoPkg.Name}}{{$currentPackageName}}*/}}
# TODO: more variables
# Sprig: strings
{{trim " hello "}}: hello
{{trimAll "$" "$5.00"}}: 5.00
{{trimSuffix "-" "hello-"}}: hello
{{upper "hello"}}: HELLO
{{lower "HELLO"}}: hello
{{title "hello world"}}: Hello World
{{untitle "Hello World"}}: hello world
{{repeat 3 "hello"}}: hellohellohello
{{substr 0 5 "hello world"}}: hello
{{nospace "hello w o r l d"}}: helloworld
{{trunc 5 "hello world"}}: hello
{{abbrev 5 "hello world"}}: he...
{{abbrevboth 5 10 "1234 5678 9123"}}: ...5678...
{{initials "First Try"}}: FT
{{randNumeric 3}}: 146
{{- /*{{wrap 80 $someText}}*/}}:
{{wrapWith 5 "\t" "Hello World"}}: Hello World
{{contains "cat" "catch"}}: true
{{hasPrefix "cat" "catch"}}: true
{{cat "hello" "beautiful" "world"}}: hello beautiful world
{{- /*{{indent 4 $lots_of_text}}*/}}:
{{- /*{{indent 4 $lots_of_text}}*/}}:
{{"I Am Henry VIII" | replace " " "-"}}: I-Am-Henry-VIII
{{len .Service.Method | plural "one anchovy" "many anchovies"}}: many anchovies
{{snakecase "FirstName"}}: first_name
{{camelcase "http_server"}}: HttpServer
{{shuffle "hello"}}: holle
{{regexMatch "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" "test@acme.com"}}: true
{{- /*{{regexFindAll "[2,4,6,8]" "123456789"}}*/}}:
{{regexFind "[a-zA-Z][1-9]" "abcd1234"}}: d1
{{regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W"}}: -W-xxW-
{{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}: -${1}-${1}-
{{regexSplit "z+" "pizza" -1}}: [pi a]
# Sprig: advanced
{{if contains "cat" "catch"}}yes{{else}}no{{end}}: yes
{{1 | plural "one anchovy" "many anchovies"}}: one anchovy
{{2 | plural "one anchovy" "many anchovies"}}: many anchovies
{{3 | plural "one anchovy" "many anchovies"}}: many anchovies
# TODO: more sprig examples
# TODO: all built-in examples

View File

@ -0,0 +1,56 @@
# Common variables
{{`{{.File.Name}}`}}: {{.File.Name}}
{{`{{.File.Name | upper}}`}}: {{.File.Name | upper}}
{{`{{.File.Package | base | replace "." "-"}}`}} {{.File.Package | base | replace "." "-"}}
{{- /*{{`{{$file := .File}}{{$file}}`}} {{$file := .File}}{{$file}}*/}}
{{`{{$packageDir := .File.Name | dir}}{{$packageDir}}`}} {{$packageDir := .File.Name | dir}}{{$packageDir}}
{{`{{$packageName := .File.Name | base | replace ".proto" ""}}{{$packageName}}`}} {{$packageName := .File.Name | base | replace ".proto" ""}}{{$packageName}}
{{`{{$packageImport := .File.Package | replace "." "_"}}{{$packageImport}}`}} {{$packageImport := .File.Package | replace "." "_"}}{{$packageImport}}
{{`{{$namespacedPackage := .File.Package}}{{$namespacedPackage}}`}} {{$namespacedPackage := .File.Package}}{{$namespacedPackage}}
{{`{{$currentFile := .File.Name | getProtoFile}}{{$currentFile}}`}} {{$currentFile := .File.Name | getProtoFile}}{{$currentFile}}
{{`{{- /*{{- $currentPackageName := $currentFile.GoPkg.Name}}{{$currentPackageName}}*/}}`}} {{- /*{{- $currentPackageName := $currentFile.GoPkg.Name}}{{$currentPackageName}}*/}}
# TODO: more variables
# Sprig: strings
{{`{{trim " hello "}}`}}: {{trim " hello "}}
{{`{{trimAll "$" "$5.00"}}`}}: {{trimAll "$" "$5.00"}}
{{`{{trimSuffix "-" "hello-"}}`}}: {{trimSuffix "-" "hello-"}}
{{`{{upper "hello"}}`}}: {{upper "hello"}}
{{`{{lower "HELLO"}}`}}: {{lower "HELLO"}}
{{`{{title "hello world"}}`}}: {{title "hello world"}}
{{`{{untitle "Hello World"}}`}}: {{untitle "Hello World"}}
{{`{{repeat 3 "hello"}}`}}: {{repeat 3 "hello"}}
{{`{{substr 0 5 "hello world"}}`}}: {{substr 0 5 "hello world"}}
{{`{{nospace "hello w o r l d"}}`}}: {{nospace "hello w o r l d"}}
{{`{{trunc 5 "hello world"}}`}}: {{trunc 5 "hello world"}}
{{`{{abbrev 5 "hello world"}}`}}: {{abbrev 5 "hello world"}}
{{`{{abbrevboth 5 10 "1234 5678 9123"}}`}}: {{abbrevboth 5 10 "1234 5678 9123"}}
{{`{{initials "First Try"}}`}}: {{initials "First Try"}}
{{`{{randNumeric 3}}`}}: {{randNumeric 3}}
{{`{{- /*{{wrap 80 $someText}}*/}}`}}: {{- /*{{wrap 80 $someText}}*/}}
{{`{{wrapWith 5 "\t" "Hello World"}}`}}: {{wrapWith 5 "\t" "Hello World"}}
{{`{{contains "cat" "catch"}}`}}: {{contains "cat" "catch"}}
{{`{{hasPrefix "cat" "catch"}}`}}: {{hasPrefix "cat" "catch"}}
{{`{{cat "hello" "beautiful" "world"}}`}}: {{cat "hello" "beautiful" "world"}}
{{`{{- /*{{indent 4 $lots_of_text}}*/}}`}}: {{- /*{{indent 4 $lots_of_text}}*/}}
{{`{{- /*{{indent 4 $lots_of_text}}*/}}`}}: {{- /*{{indent 4 $lots_of_text}}*/}}
{{`{{"I Am Henry VIII" | replace " " "-"}}`}}: {{"I Am Henry VIII" | replace " " "-"}}
{{`{{len .Service.Method | plural "one anchovy" "many anchovies"}}`}}: {{len .Service.Method | plural "one anchovy" "many anchovies"}}
{{`{{snakecase "FirstName"}}`}}: {{snakecase "FirstName"}}
{{`{{camelcase "http_server"}}`}}: {{camelcase "http_server"}}
{{`{{shuffle "hello"}}`}}: {{shuffle "hello"}}
{{`{{regexMatch "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" "test@acme.com"}}`}}: {{regexMatch "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" "test@acme.com"}}
{{`{{- /*{{regexFindAll "[2,4,6,8]" "123456789"}}*/}}`}}: {{- /*{{regexFindAll "[2,4,6,8]" "123456789"}}*/}}
{{`{{regexFind "[a-zA-Z][1-9]" "abcd1234"}}`}}: {{regexFind "[a-zA-Z][1-9]" "abcd1234"}}
{{`{{regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W"}}`}}: {{regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W"}}
{{`{{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}`}}: {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}
{{`{{regexSplit "z+" "pizza" -1}}`}}: {{regexSplit "z+" "pizza" -1}}
# Sprig: advanced
{{`{{if contains "cat" "catch"}}yes{{else}}no{{end}}`}}: {{if contains "cat" "catch"}}yes{{else}}no{{end}}
{{`{{1 | plural "one anchovy" "many anchovies"}}`}}: {{1 | plural "one anchovy" "many anchovies"}}
{{`{{2 | plural "one anchovy" "many anchovies"}}`}}: {{2 | plural "one anchovy" "many anchovies"}}
{{`{{3 | plural "one anchovy" "many anchovies"}}`}}: {{3 | plural "one anchovy" "many anchovies"}}
# TODO: more sprig examples
# TODO: all built-in examples

View File

@ -0,0 +1,25 @@
syntax = "proto3";
package dummy;
option go_package = "github.com/moul/protoc-gen-gotemplate/examples/helpers";
message Dummy1 {
float aaa = 1;
string bbb = 2;
int32 ccc = 3;
int64 ddd = 4;
repeated string eee = 5;
}
message Dummy2 {
float fff = 1;
Dummy1 ggg = 2;
}
message Dummy3 {}
service DummyService {
rpc Hhh(Dummy1) returns (Dummy2) {}
rpc Iii(Dummy2) returns (Dummy1) {}
}