From 1f9e8dcc6010abeea8abc7ba1a092c4dab9852f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 20 May 2017 11:28:33 +0300 Subject: [PATCH] tpl: Make the Namespace func signature explicit This makes it cleaner and avoids breaking client code, such as the docs helper JSON generator. --- tpl/cast/init.go | 2 +- tpl/cast/init_test.go | 2 +- tpl/collections/init.go | 2 +- tpl/collections/init_test.go | 2 +- tpl/compare/init.go | 2 +- tpl/compare/init_test.go | 2 +- tpl/crypto/init.go | 2 +- tpl/crypto/init_test.go | 2 +- tpl/data/init.go | 2 +- tpl/data/init_test.go | 2 +- tpl/encoding/init.go | 2 +- tpl/encoding/init_test.go | 2 +- tpl/fmt/init.go | 2 +- tpl/fmt/init_test.go | 2 +- tpl/images/init.go | 2 +- tpl/images/init_test.go | 2 +- tpl/inflect/init.go | 2 +- tpl/inflect/init_test.go | 2 +- tpl/internal/templatefuncsRegistry.go | 4 ++-- tpl/lang/init.go | 2 +- tpl/lang/init_test.go | 2 +- tpl/math/init.go | 2 +- tpl/math/init_test.go | 2 +- tpl/os/init.go | 2 +- tpl/os/init_test.go | 2 +- tpl/partials/init.go | 2 +- tpl/partials/init_test.go | 2 +- tpl/safe/init.go | 2 +- tpl/safe/init_test.go | 2 +- tpl/strings/init.go | 2 +- tpl/strings/init_test.go | 2 +- tpl/time/init.go | 6 +++--- tpl/time/init_test.go | 2 +- tpl/transform/init.go | 2 +- tpl/transform/init_test.go | 2 +- tpl/urls/init.go | 2 +- tpl/urls/init_test.go | 2 +- 37 files changed, 40 insertions(+), 40 deletions(-) diff --git a/tpl/cast/init.go b/tpl/cast/init.go index 1c737ee58..57573e188 100644 --- a/tpl/cast/init.go +++ b/tpl/cast/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.ToInt, diff --git a/tpl/cast/init_test.go b/tpl/cast/init_test.go index 22051495a..e52ef6750 100644 --- a/tpl/cast/init_test.go +++ b/tpl/cast/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/collections/init.go b/tpl/collections/init.go index 289228901..fa786684b 100644 --- a/tpl/collections/init.go +++ b/tpl/collections/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.After, diff --git a/tpl/collections/init_test.go b/tpl/collections/init_test.go index 1066caeaa..da2249678 100644 --- a/tpl/collections/init_test.go +++ b/tpl/collections/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/compare/init.go b/tpl/compare/init.go index 0285abff5..cc64dcf90 100644 --- a/tpl/compare/init.go +++ b/tpl/compare/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Default, diff --git a/tpl/compare/init_test.go b/tpl/compare/init_test.go index 610c0e5cb..3303e4f23 100644 --- a/tpl/compare/init_test.go +++ b/tpl/compare/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/crypto/init.go b/tpl/crypto/init.go index a47c3369f..08421e1b2 100644 --- a/tpl/crypto/init.go +++ b/tpl/crypto/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.MD5, diff --git a/tpl/crypto/init_test.go b/tpl/crypto/init_test.go index 20c0201e5..755f162d0 100644 --- a/tpl/crypto/init_test.go +++ b/tpl/crypto/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/data/init.go b/tpl/data/init.go index e9e4ca2f9..5235714e9 100644 --- a/tpl/data/init.go +++ b/tpl/data/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.GetCSV, diff --git a/tpl/data/init_test.go b/tpl/data/init_test.go index f912560c4..66bb0b2be 100644 --- a/tpl/data/init_test.go +++ b/tpl/data/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/encoding/init.go b/tpl/encoding/init.go index b46394203..22f38cb20 100644 --- a/tpl/encoding/init.go +++ b/tpl/encoding/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Base64Decode, diff --git a/tpl/encoding/init_test.go b/tpl/encoding/init_test.go index f10f4b1f8..7e40a6212 100644 --- a/tpl/encoding/init_test.go +++ b/tpl/encoding/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/fmt/init.go b/tpl/fmt/init.go index 98070b777..a9348f8bf 100644 --- a/tpl/fmt/init.go +++ b/tpl/fmt/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Print, diff --git a/tpl/fmt/init_test.go b/tpl/fmt/init_test.go index 9b71381d6..dbc6e9701 100644 --- a/tpl/fmt/init_test.go +++ b/tpl/fmt/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/images/init.go b/tpl/images/init.go index 8e829f300..4a367b00c 100644 --- a/tpl/images/init.go +++ b/tpl/images/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Config, diff --git a/tpl/images/init_test.go b/tpl/images/init_test.go index d4fc65484..2525fecb8 100644 --- a/tpl/images/init_test.go +++ b/tpl/images/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/inflect/init.go b/tpl/inflect/init.go index 50d012d35..71884c649 100644 --- a/tpl/inflect/init.go +++ b/tpl/inflect/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Humanize, diff --git a/tpl/inflect/init_test.go b/tpl/inflect/init_test.go index 741e8f797..b24d7af33 100644 --- a/tpl/inflect/init_test.go +++ b/tpl/inflect/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go index ced16e512..1ecdeca94 100644 --- a/tpl/internal/templatefuncsRegistry.go +++ b/tpl/internal/templatefuncsRegistry.go @@ -45,7 +45,7 @@ type TemplateFuncsNamespace struct { Name string // This is the method receiver. - Context interface{} + Context func(v ...interface{}) interface{} // Additional info, aliases and examples, per method name. MethodMappings map[string]TemplateFuncMethodMapping @@ -165,7 +165,7 @@ func (t *TemplateFuncsNamespace) toJSON() ([]byte, error) { buf.WriteString(fmt.Sprintf(`%q: {`, t.Name)) - ctx := t.Context.(func() interface{})() + ctx := t.Context() ctxType := reflect.TypeOf(ctx) for i := 0; i < ctxType.NumMethod(); i++ { method := ctxType.Method(i) diff --git a/tpl/lang/init.go b/tpl/lang/init.go index ea67afab0..d0efa6997 100644 --- a/tpl/lang/init.go +++ b/tpl/lang/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Translate, diff --git a/tpl/lang/init_test.go b/tpl/lang/init_test.go index 8a03d18eb..a32fa0ae4 100644 --- a/tpl/lang/init_test.go +++ b/tpl/lang/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/math/init.go b/tpl/math/init.go index 65b22c516..7b2d319f2 100644 --- a/tpl/math/init.go +++ b/tpl/math/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Add, diff --git a/tpl/math/init_test.go b/tpl/math/init_test.go index 70a8e3c96..3204b35e3 100644 --- a/tpl/math/init_test.go +++ b/tpl/math/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/os/init.go b/tpl/os/init.go index 264afd43a..e0bcda140 100644 --- a/tpl/os/init.go +++ b/tpl/os/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Getenv, diff --git a/tpl/os/init_test.go b/tpl/os/init_test.go index 53d4c6920..954e4cde2 100644 --- a/tpl/os/init_test.go +++ b/tpl/os/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/partials/init.go b/tpl/partials/init.go index 4ee3faf19..496deed89 100644 --- a/tpl/partials/init.go +++ b/tpl/partials/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Include, diff --git a/tpl/partials/init_test.go b/tpl/partials/init_test.go index b841f2151..f26067e3f 100644 --- a/tpl/partials/init_test.go +++ b/tpl/partials/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/safe/init.go b/tpl/safe/init.go index 94012a6ca..eb3ee6245 100644 --- a/tpl/safe/init.go +++ b/tpl/safe/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.CSS, diff --git a/tpl/safe/init_test.go b/tpl/safe/init_test.go index e18c5b12c..eee8523d0 100644 --- a/tpl/safe/init_test.go +++ b/tpl/safe/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/strings/init.go b/tpl/strings/init.go index 9f33bd176..e40fe9d39 100644 --- a/tpl/strings/init.go +++ b/tpl/strings/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Chomp, diff --git a/tpl/strings/init_test.go b/tpl/strings/init_test.go index 5b7616406..22f4ba21f 100644 --- a/tpl/strings/init_test.go +++ b/tpl/strings/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/time/init.go b/tpl/time/init.go index 8e21bca61..760986e5a 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(v ...interface{}) interface{} { + Context: func(args ...interface{}) interface{} { // Handle overlapping "time" namespace and func. // // If no args are passed to `time`, assume namespace usage and @@ -34,11 +34,11 @@ func init() { // // If args are passed, call AsTime(). - if len(v) == 0 { + if len(args) == 0 { return ctx } - t, err := ctx.AsTime(v[0]) + t, err := ctx.AsTime(args[0]) if err != nil { return err } diff --git a/tpl/time/init_test.go b/tpl/time/init_test.go index fd49dc4be..4f4ee5de5 100644 --- a/tpl/time/init_test.go +++ b/tpl/time/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func(v ...interface{}) interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/transform/init.go b/tpl/transform/init.go index 3483d1306..3262cec09 100644 --- a/tpl/transform/init.go +++ b/tpl/transform/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.Emojify, diff --git a/tpl/transform/init_test.go b/tpl/transform/init_test.go index 26f81d7f1..a6cb56561 100644 --- a/tpl/transform/init_test.go +++ b/tpl/transform/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) } diff --git a/tpl/urls/init.go b/tpl/urls/init.go index e32807f32..c0440490d 100644 --- a/tpl/urls/init.go +++ b/tpl/urls/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func() interface{} { return ctx }, + Context: func(args ...interface{}) interface{} { return ctx }, } ns.AddMethodMapping(ctx.AbsURL, diff --git a/tpl/urls/init_test.go b/tpl/urls/init_test.go index f619b2c00..5e53f3573 100644 --- a/tpl/urls/init_test.go +++ b/tpl/urls/init_test.go @@ -34,5 +34,5 @@ func TestInit(t *testing.T) { } require.True(t, found) - require.IsType(t, &Namespace{}, ns.Context.(func() interface{})()) + require.IsType(t, &Namespace{}, ns.Context()) }