Extract ProtoHelpersFuncMap in its own file
This commit is contained in:
parent
d7c3d1a32b
commit
add7ae7237
31
encoder.go
31
encoder.go
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -10,39 +9,10 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||
)
|
||||
|
||||
var ProtoHelpersFuncMap = template.FuncMap{
|
||||
"string": func(i interface {
|
||||
String() string
|
||||
}) string {
|
||||
return i.String()
|
||||
},
|
||||
"json": func(v interface{}) string {
|
||||
a, _ := json.Marshal(v)
|
||||
return string(a)
|
||||
},
|
||||
"prettyjson": func(v interface{}) string {
|
||||
a, _ := json.MarshalIndent(v, "", " ")
|
||||
return string(a)
|
||||
},
|
||||
"first": func(a []string) string {
|
||||
return a[0]
|
||||
},
|
||||
"last": func(a []string) string {
|
||||
return a[len(a)-1]
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
for k, v := range sprig.TxtFuncMap() {
|
||||
ProtoHelpersFuncMap[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
type GenericTemplateBasedEncoder struct {
|
||||
templateDir string
|
||||
service *descriptor.ServiceDescriptorProto
|
||||
@ -146,7 +116,6 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
|
||||
// initialize template engine
|
||||
fullPath := filepath.Join(e.templateDir, templateFilename)
|
||||
templateName := filepath.Base(fullPath)
|
||||
|
||||
tmpl, err := template.New(templateName).Funcs(ProtoHelpersFuncMap).ParseFiles(fullPath)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
|
36
helpers.go
Normal file
36
helpers.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"text/template"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
)
|
||||
|
||||
var ProtoHelpersFuncMap = template.FuncMap{
|
||||
"string": func(i interface {
|
||||
String() string
|
||||
}) string {
|
||||
return i.String()
|
||||
},
|
||||
"json": func(v interface{}) string {
|
||||
a, _ := json.Marshal(v)
|
||||
return string(a)
|
||||
},
|
||||
"prettyjson": func(v interface{}) string {
|
||||
a, _ := json.MarshalIndent(v, "", " ")
|
||||
return string(a)
|
||||
},
|
||||
"first": func(a []string) string {
|
||||
return a[0]
|
||||
},
|
||||
"last": func(a []string) string {
|
||||
return a[len(a)-1]
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
for k, v := range sprig.TxtFuncMap() {
|
||||
ProtoHelpersFuncMap[k] = v
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user