diff --git a/Makefile b/Makefile index 14ef388..36f7f35 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ test: install cd examples/go-generate && make cd examples/single-package-mode && make cd examples/helpers && make + cd examples/arithmetics && make # cd examples/go-kit && make .PHONY: docker.build diff --git a/README.md b/README.md index 4636a62..c3af012 100644 --- a/README.md +++ b/README.md @@ -73,34 +73,59 @@ See [examples](./examples). This project uses [Masterminds/sprig](https://github.com/Masterminds/sprig) library and additional functions to extend the builtin [text/template](https://golang.org/pkg/text/template) helpers. -Non-exhaustive list of new helpers:s +Non-exhaustive list of new helpers: * **all the functions from [sprig](https://github.com/Masterminds/sprig)** +* `string` * `json` * `prettyjson` +* `splitArray` * `first` * `last` -* `splitArray` * `upperFirst` * `lowerFirst` * `camelCase` * `lowerCamelCase` * `kebabCase` +* `contains` +* `trimstr` +* `index` * `snakeCase` * `getProtoFile` * `getMessageType` * `getEnumValue` * `isFieldMessage` +* `isFieldMessageTimeStamp` * `isFieldRepeated` +* `haskellType` * `goType` +* `goZeroValue` * `goTypeWithPackage` * `jsType` * `jsSuffixReserved` * `namespacedFlowType` * `httpVerb` * `httpPath` +* `httpPathsAdditionalBindings` +* `httpBody` * `shortType` * `urlHasVarsFromMessage` +* `lowerGoNormalize` +* `goNormalize` +* `leadingComment` +* `trailingComment` +* `leadingDetachedComments` +* `stringFieldExtension` +* `stringMethodOptionsExtension` +* `boolFieldExtension` +* `isFieldMap` +* `fieldMapKeyType` +* `fieldMapValueType` +* `replaceDict` +* `add` +* `subtract` +* `multiply` +* `divide` See the project helpers for the complete list. diff --git a/examples/arithmetics/Makefile b/examples/arithmetics/Makefile new file mode 100644 index 0000000..60bc04f --- /dev/null +++ b/examples/arithmetics/Makefile @@ -0,0 +1,13 @@ +.PHONY: build +build: + mkdir -p output + protoc -I. --gotemplate_out=template_dir=templates,debug=true,all=true:output proto/*.proto + + +.PHONY: re +re: clean build + + +.PHONY: clean +clean: + rm -rf output diff --git a/examples/arithmetics/output/arithmetics b/examples/arithmetics/output/arithmetics new file mode 100644 index 0000000..bd80b8a --- /dev/null +++ b/examples/arithmetics/output/arithmetics @@ -0,0 +1,5 @@ + +add(1,2) = 3 +subtract(1,2) = -1 +multiply(1,2) = 2 +divide(2,1) = 2 diff --git a/examples/arithmetics/proto/arithmetics.proto b/examples/arithmetics/proto/arithmetics.proto new file mode 100644 index 0000000..92238fe --- /dev/null +++ b/examples/arithmetics/proto/arithmetics.proto @@ -0,0 +1,2 @@ +syntax = "proto3"; +package Arithmetics; diff --git a/examples/arithmetics/templates/arithmetics.tmpl b/examples/arithmetics/templates/arithmetics.tmpl new file mode 100644 index 0000000..c877598 --- /dev/null +++ b/examples/arithmetics/templates/arithmetics.tmpl @@ -0,0 +1,6 @@ +{{with $a := 1}}{{with $b := 2}} +add(1,2) = {{add $a $b}} +subtract(1,2) = {{subtract $a $b}} +multiply(1,2) = {{multiply $a $b}} +divide(2,1) = {{divide $b $a}} +{{end}}{{end}} \ No newline at end of file diff --git a/helpers/helpers.go b/helpers/helpers.go index 612f86c..8b16baa 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -133,6 +133,21 @@ var ProtoHelpersFuncMap = template.FuncMap{ "fieldMapKeyType": fieldMapKeyType, "fieldMapValueType": fieldMapValueType, "replaceDict": replaceDict, + "add": func(a int, b int) int { + return a + b + }, + "subtract": func(a int, b int) int { + return a - b + }, + "multiply": func(a int, b int) int { + return a * b + }, + "divide": func(a int, b int) int { + if b == 0 { + panic("psssst ... little help here ... you cannot divide by 0") + } + return a / b + }, } var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location