Merge pull request #95 from Tommy-42/master

Add arithmetics helpers - fix #94
This commit is contained in:
Julien Hayotte 2018-03-21 17:29:47 +01:00 committed by GitHub
commit 283713bd93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 2 deletions

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -0,0 +1,5 @@
add(1,2) = 3
subtract(1,2) = -1
multiply(1,2) = 2
divide(2,1) = 2

View File

@ -0,0 +1,2 @@
syntax = "proto3";
package Arithmetics;

View File

@ -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}}

View File

@ -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