Skip to content

Commit

Permalink
templates/{go,gogo,goshared}: Support GoGo customtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
rvolosatovs committed Feb 25, 2019
1 parent ffecbd8 commit 0065aca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion templates/go/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"text/template"

"github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"github.com/lyft/protoc-gen-validate/templates/goshared"
)

func Register(tpl *template.Template, params pgs.Parameters) {
goshared.Register(tpl, params)
goshared.Register(tpl, params, pgsgo.InitContext)
template.Must(tpl.Parse(fileTpl))
template.Must(tpl.New("required").Parse(requiredTpl))
template.Must(tpl.New("timestamp").Parse(timestampTpl))
Expand Down
4 changes: 2 additions & 2 deletions templates/gogo/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"text/template"

"github.com/lyft/protoc-gen-star"

pgsgo "github.com/lyft/protoc-gen-star/lang/go"
shared "github.com/lyft/protoc-gen-validate/templates/goshared"
)

func Register(tpl *template.Template, params pgs.Parameters) {
shared.Register(tpl, params)
shared.Register(tpl, params, pgsgo.InitGoGoContext)
template.Must(tpl.Parse(fileTpl))
template.Must(tpl.New("required").Parse(requiredTpl))
template.Must(tpl.New("timestamp").Parse(timestampTpl))
Expand Down
11 changes: 7 additions & 4 deletions templates/goshared/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/lyft/protoc-gen-validate/templates/shared"
)

func Register(tpl *template.Template, params pgs.Parameters) {
fns := goSharedFuncs{pgsgo.InitContext(params)}
func Register(tpl *template.Template, params pgs.Parameters, initContext func(pgs.Parameters) pgsgo.Context) {
fns := goSharedFuncs{initContext(params)}

tpl.Funcs(map[string]interface{}{
"accessor": fns.accessor,
Expand Down Expand Up @@ -91,8 +91,11 @@ func (fns goSharedFuncs) accessor(ctx shared.RuleContext) string {
if ctx.AccessorOverride != "" {
return ctx.AccessorOverride
}

return fmt.Sprintf("m.Get%s()", fns.Name(ctx.Field))
name := fns.Name(ctx.Field)
if name == "" {
return fmt.Sprintf("m.%s", fns.Type(ctx.Field).Value())
}
return fmt.Sprintf("m.Get%s()", name)
}

func (fns goSharedFuncs) errName(m pgs.Message) pgs.Name {
Expand Down

0 comments on commit 0065aca

Please sign in to comment.