Add arithmetics helpers - fix #94
This commit is contained in:
parent
77a820ea6f
commit
19fb3c8da9
1
Makefile
1
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
|
||||
|
29
README.md
29
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.
|
||||
|
||||
|
13
examples/arithmetics/Makefile
Normal file
13
examples/arithmetics/Makefile
Normal 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
|
5
examples/arithmetics/output/arithmetics
Normal file
5
examples/arithmetics/output/arithmetics
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
add(1,2) = 3
|
||||
subtract(1,2) = -1
|
||||
multiply(1,2) = 2
|
||||
divide(2,1) = 2
|
2
examples/arithmetics/proto/arithmetics.proto
Normal file
2
examples/arithmetics/proto/arithmetics.proto
Normal file
@ -0,0 +1,2 @@
|
||||
syntax = "proto3";
|
||||
package Arithmetics;
|
6
examples/arithmetics/templates/arithmetics.tmpl
Normal file
6
examples/arithmetics/templates/arithmetics.tmpl
Normal 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}}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user