Skip to content

Commit

Permalink
[mathutil] Add min/max for integer type #123
Browse files Browse the repository at this point in the history
  • Loading branch information
at15 committed Feb 29, 2020
1 parent c5a6950 commit 841cbdf
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 23 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ DOCKER_REPO = dyweb/gommon
.PHONY: install
install: fmt test
cd ./cmd/gommon && $(GO) install -ldflags "$(FLAGS)" .
mv $(GOPATH)/bin/gommonbin $(GOPATH)/bin/gommon

.PHONY: fmt
fmt:
Expand Down
1 change: 1 addition & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ From 0.0.9
- [x] support `.gommonignore`, used by `gommon generate` on `_legacy` folder
- [x] clean up `.ignore` file support, move it to `fsutil` package
- [x] clean up `go.mod` by using a separated `go.mod` for `gommon` binary, thus removing cobra, viper, etcd.
- [x] min/max for integer https://github.com/dyweb/gommon/issues/123

## Finished

Expand Down
18 changes: 17 additions & 1 deletion generator/gotmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/format"
"io/ioutil"
"text/template"
"unicode"

"github.com/dyweb/gommon/errors"
"github.com/dyweb/gommon/util/fsutil"
Expand All @@ -29,7 +30,11 @@ func (c *GoTemplateConfig) Render(root string) error {
if b, err = ioutil.ReadFile(join(root, c.Src)); err != nil {
return errors.Wrap(err, "can't read template file")
}
if t, err = template.New(c.Src).Parse(string(b)); err != nil {
if t, err = template.New(c.Src).
Funcs(template.FuncMap{
"UcFirst": UcFirst,
}).
Parse(string(b)); err != nil {
return errors.Wrap(err, "can't parse template")
}
buf.WriteString(genutil.DefaultHeader(join(root, c.Src)))
Expand All @@ -49,3 +54,14 @@ func (c *GoTemplateConfig) Render(root string) error {
log.Debugf("rendered go tmpl %s to %s", join(root, c.Src), join(root, c.Dst))
return nil
}

// UcFirst change first character to upper case.
// It is based on https://github.com/99designs/gqlgen/blob/master/codegen/templates/templates.go#L205
func UcFirst(s string) string {
if s == "" {
return ""
}
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
return string(r)
}
21 changes: 21 additions & 0 deletions generator/gotmpl_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
package generator_test

import (
"bytes"
"testing"
"text/template"

"github.com/dyweb/gommon/generator"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUcFirst(t *testing.T) {
tmpl, err := template.New("test_ucfirst").Funcs(template.FuncMap{"UcFirst": generator.UcFirst}).Parse(`{{ .foo | UcFirst }}`)
require.Nil(t, err)
var buf bytes.Buffer
err = tmpl.Execute(&buf, map[string]string{
"foo": "int64",
})
assert.Nil(t, err)
assert.Equal(t, "Int64", buf.String())
}
44 changes: 22 additions & 22 deletions noodle/_examples/embed/gen/noodle.go

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions util/mathutil/gommon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
gotmpls:
- src: max_generated.go.tmpl
dst: max_generated.go
go: true
data:
- int
- uint
- int32
- uint32
- int64
- uint64
8 changes: 8 additions & 0 deletions util/mathutil/max.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mathutil

func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
87 changes: 87 additions & 0 deletions util/mathutil/max_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions util/mathutil/max_generated.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mathutil

{{ range .}}
func Max{{. | UcFirst }} (a, b {{.}}) {{.}} {
if a > b {
return a
}
return b
}

func Min{{. | UcFirst }} (a, b {{.}}) {{.}} {
if a < b {
return a
}
return b
}
{{ end }}
2 changes: 2 additions & 0 deletions util/mathutil/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package mathutil provides supplement functions for math package, e.g. MaxInt, MaxInt64
package mathutil

0 comments on commit 841cbdf

Please sign in to comment.