diff --git a/Makefile b/Makefile index ca01826..0efb3dc 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ test: install cd examples/sitemap && make cd examples/go-generate && make cd examples/single-package-mode && make + cd examples/helpers && make # cd examples/go-kit && make .PHONY: docker.build diff --git a/examples/helpers/Makefile b/examples/helpers/Makefile new file mode 100644 index 0000000..99d4be8 --- /dev/null +++ b/examples/helpers/Makefile @@ -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 diff --git a/examples/helpers/example.txt b/examples/helpers/example.txt new file mode 100644 index 0000000..9a7773b --- /dev/null +++ b/examples/helpers/example.txt @@ -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}} +{{- /*{{- $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 \ No newline at end of file diff --git a/examples/helpers/example.txt.tmpl b/examples/helpers/example.txt.tmpl new file mode 100644 index 0000000..c5f1638 --- /dev/null +++ b/examples/helpers/example.txt.tmpl @@ -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 \ No newline at end of file diff --git a/examples/helpers/helpers.proto b/examples/helpers/helpers.proto new file mode 100644 index 0000000..d45e9bf --- /dev/null +++ b/examples/helpers/helpers.proto @@ -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) {} +}