Skip to content

Commit

Permalink
Use internal funcs registration
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Oct 21, 2023
1 parent 362e4fe commit 212beec
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 91 deletions.
8 changes: 0 additions & 8 deletions caddy/db.go

This file was deleted.

15 changes: 1 addition & 14 deletions caddy/go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
module github.com/infogulch/xtemplate/caddy

go 1.21.0
go 1.21.3

require (
github.com/caddyserver/caddy/v2 v2.7.4
github.com/infogulch/watch v0.1.3
github.com/infogulch/xtemplate v0.1.4
github.com/mattn/go-sqlite3 v1.14.17
go.uber.org/zap/exp v0.2.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
modernc.org/sqlite v1.26.0
)

require (
Expand Down Expand Up @@ -62,7 +60,6 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/libdns/libdns v0.2.1 // indirect
Expand All @@ -87,7 +84,6 @@ require (
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.1 // indirect
github.com/quic-go/quic-go v0.37.5 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand Down Expand Up @@ -126,13 +122,4 @@ require (
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/libc v1.24.1 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)
34 changes: 0 additions & 34 deletions caddy/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,6 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
Expand Down Expand Up @@ -478,8 +476,6 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
Expand Down Expand Up @@ -600,8 +596,6 @@ github.com/quic-go/qtls-go1-20 v0.3.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58
github.com/quic-go/quic-go v0.37.5 h1:pzkYe8AgaxHi+7KJrYBMF+u2rLO5a9kwyCp2dAsljzk=
github.com/quic-go/quic-go v0.37.5/go.mod h1:YsbH1r4mSHPJcLF4k4zruUkLBqctEMBDR6VPvcYjIsU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand Down Expand Up @@ -1163,34 +1157,6 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=
lukechampine.com/uint128 v1.3.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.41.0 h1:QoR1Sn3YWlmA1T4vLaKZfawdVtSiGx8H+cEojbC7v1Q=
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
modernc.org/libc v1.24.1 h1:uvJSeCKL/AgzBo2yYIPPTy82v21KgGnizcGYfBHaNuM=
modernc.org/libc v1.24.1/go.mod h1:FmfO1RLrU3MHJfyi9eYYmZBfi/R+tqZ6+hQ3yQQUkak=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sqlite v1.26.0 h1:SocQdLRSYlA8W99V8YH0NES75thx19d9sB/aFc4R8Lw=
modernc.org/sqlite v1.26.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY=
modernc.org/tcl v1.15.2/go.mod h1:3+k/ZaEbKrC8ePv8zJWPtBSW0V7Gg9g8rkmhI1Kfs3c=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY=
modernc.org/z v1.7.3/go.mod h1:Ipv4tsdxZRbQyLq9Q1M6gdbkxYzdlrciF2Hi/lS7nWE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
22 changes: 3 additions & 19 deletions caddy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package xtemplate_caddy
import (
"database/sql"
"fmt"
"html/template"
"io/fs"
"net/http"
"os"
Expand Down Expand Up @@ -61,10 +60,6 @@ type XTemplateModule struct {
halt chan<- struct{}
}

type FuncsProvider interface {
Funcs() template.FuncMap
}

// Validate ensures t has a valid configuration. Implements caddy.Validator.
func (m *XTemplateModule) Validate() error {
if len(m.Delimiters) != 0 && len(m.Delimiters) != 2 {
Expand All @@ -73,15 +68,6 @@ func (m *XTemplateModule) Validate() error {
if m.Database.Driver != "" && slices.Index(sql.Drivers(), m.Database.Driver) == -1 {
return fmt.Errorf("database driver '%s' does not exist", m.Database.Driver)
}
for _, m := range m.FuncsModules {
mi, err := caddy.GetModule("xtemplate.funcs." + m)
if err != nil {
return fmt.Errorf("failed to find module 'xtemplate.funcs.%s': %v", m, err)
}
if _, ok := mi.New().(FuncsProvider); !ok {
return fmt.Errorf("module 'xtemplate.funcs.%s' does not implement FuncsProvider", m)
}
}
return nil
}

Expand Down Expand Up @@ -120,11 +106,9 @@ func (m *XTemplateModule) Provision(ctx caddy.Context) error {

// ExtraFuncs
{
for _, m := range m.FuncsModules {
mi, _ := caddy.GetModule("xtemplate.funcs." + m)
fm := mi.New().(FuncsProvider).Funcs()
log.Debug("got funcs from module", "module", "xtemplate.funcs."+m, "funcmap", fm)
config.WithExtraFuncs(fm)
_, err := config.WithRegisteredFuncMaps(m.FuncsModules...)
if err != nil {
return err
}
}

Expand Down
8 changes: 0 additions & 8 deletions cmd/db.go

This file was deleted.

53 changes: 46 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package xtemplate

import (
"database/sql"
"fmt"
"html/template"
"io/fs"
"log/slog"

"github.com/Masterminds/sprig/v3"
"github.com/infogulch/xtemplate/internal"

Check failure on line 11 in config.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/infogulch/xtemplate/internal; to add it:
"golang.org/x/exp/maps"
)

func New() *config {
c := &config{}
c.WithExtraFuncs(xtemplateFuncs)
c.WithExtraFuncs(sprig.HtmlFuncMap())
c.WithFuncMaps(xtemplateFuncs, sprig.HtmlFuncMap())
return c
}

Expand All @@ -25,19 +27,56 @@ func (c *config) WithTemplateFS(tfs fs.FS) *config {
return c
}

func (c *config) WithRegisteredTemplateFS(name string) (*config, error) {
cfs, ok := internal.RegisteredFS[name]
if !ok {
return c, fmt.Errorf("fs named '%s' not registered", name)
}
c.WithContextFS(cfs)
return c, nil
}

func (c *config) WithContextFS(cfs fs.FS) *config {
*c = append(*c, func(r *xtemplate) {
r.contextFS = cfs
})
return c
}

func (c *config) WithExtraFuncs(funcs template.FuncMap) *config {
*c = append(*c, func(r *xtemplate) {
for name, fn := range funcs {
r.funcs[name] = fn
func (c *config) WithRegisteredContextFS(name string) (*config, error) {
cfs, ok := internal.RegisteredFS[name]
if !ok {
return c, fmt.Errorf("fs named '%s' not registered", name)
}
c.WithContextFS(cfs)
return c, nil
}

func (c *config) WithFuncMaps(funcmaps ...template.FuncMap) *config {
for _, funcs := range funcmaps {
*c = append(*c, func(r *xtemplate) {
for name, fn := range funcs {
r.funcs[name] = fn
}
})
}
return c
}

func (c *config) WithRegisteredFuncMaps(names ...string) (*config, error) {
for _, name := range names {
funcs, ok := internal.RegisteredFuncMaps[name]
if !ok {
return c, fmt.Errorf("funcmap named '%s' not registered", name)
}
})
c.WithFuncMaps(funcs)
}
return c, nil
}

func (c *config) WithAllRegisteredFuncMaps() *config {
funcmaps := maps.Values(internal.RegisteredFuncMaps)
c.WithFuncMaps(funcmaps...)
return c
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/infogulch/xtemplate

go 1.21.0
go 1.21.3

require (
github.com/BurntSushi/toml v1.3.2
Expand Down
3 changes: 3 additions & 0 deletions internal/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/infogulch/xtemplate/internal

go 1.21.3
10 changes: 10 additions & 0 deletions internal/registered.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package internal

import (
"html/template"
"io/fs"
)

var RegisteredFuncMaps map[string]template.FuncMap = make(map[string]template.FuncMap)

var RegisteredFS map[string]fs.FS = make(map[string]fs.FS)
7 changes: 7 additions & 0 deletions register/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/infogulch/xtemplate/register

go 1.21.3

require github.com/infogulch/xtemplate/internal v0.0.0

replace github.com/infogulch/xtemplate/internal v0.0.0 => ../internal
23 changes: 23 additions & 0 deletions register/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package register

import (
"fmt"
"html/template"
"io/fs"

"github.com/infogulch/xtemplate/internal"
)

func RegisterFuncMap(name string, funcs template.FuncMap) {
if existing, ok := internal.RegisteredFuncMaps[name]; ok {
panic(fmt.Sprintf("funcmap named '%s' already registered as '%+v'", name, existing))
}
internal.RegisteredFuncMaps[name] = funcs
}

func RegisterFS(name string, fs fs.FS) {
if existing, ok := internal.RegisteredFS[name]; ok {
panic(fmt.Sprintf("fs named '%s' already registered as '%+v'", name, existing))
}
internal.RegisteredFS[name] = fs
}

0 comments on commit 212beec

Please sign in to comment.