Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feat/support-delims
Browse files Browse the repository at this point in the history
  • Loading branch information
parnic committed Dec 29, 2024
2 parents c7e3aaf + 7b561eb commit be6dff3
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 74 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/bearer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Bearer PR Check

on:
pull_request:
types: [opened, synchronize, reopened]

permissions:
contents: read
pull-requests: write

jobs:
rule_check:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: reviewdog/action-setup@v1
with:
reviewdog_version: latest

- name: Run Report
id: report
uses: bearer/bearer-action@v2
with:
format: rdjson
output: rd.json
diff: true

- name: Run reviewdog
if: always()
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cat rd.json | reviewdog -f=rdjson -reporter=github-pr-review
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-latest]
go: [1.18, 1.19, "1.20", 1.21, 1.22]
os: [ubuntu-latest, macos-latest]
go: [1.21, 1.22, 1.23]
include:
- os: ubuntu-latest
go-build: ~/.cache/go-build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
go-version-file: go.mod
check-latest: true
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
uses: goreleaser/goreleaser-action@v6
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
Expand Down
38 changes: 38 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
linters:
enable-all: false
disable-all: true
fast: false
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exhaustive
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- gofumpt

run:
timeout: 3m
48 changes: 47 additions & 1 deletion dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
filesTemplateType
globTemplateType
fsTemplateType
fsFuncTemplateType
stringTemplateType
stringFuncTemplateType
filesFuncTemplateType
Expand Down Expand Up @@ -70,6 +71,8 @@ func (tb templateBuilder) buildTemplate() *template.Template {
return template.Must(template.ParseGlob(tb.glob)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
case fsTemplateType:
return template.Must(template.ParseFS(tb.fsys, tb.files...)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
case fsFuncTemplateType:
return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Funcs(tb.funcMap).ParseFS(tb.fsys, tb.files...))
case stringTemplateType:
return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Parse(tb.templateString))
case stringFuncTemplateType:
Expand Down Expand Up @@ -114,13 +117,52 @@ func (r DynamicRender) AddFromGlob(name, glob string) *template.Template {
return builder.buildTemplate()
}

// AddFromFS adds a new template to the DynamicRender from the provided file system (fs.FS) and files.
// It allows you to specify a custom function map (funcMap) to be used within the template.
// The name parameter is used to associate the template with a key in the DynamicRender.
// The files parameter is a variadic list of file paths to be included in the template.
// - name: The name to associate with the template in the DynamicRender.
// - fsys: The file system (fs.FS) from which to read the template files.
// - files: A variadic list of file paths to be included in the template.
//
// Returns:
// - *template.Template: The constructed template.
func (r DynamicRender) AddFromFS(name string, fsys fs.FS, files ...string) *template.Template {
builder := &templateBuilder{templateName: name, fsys: fsys, files: files}
builder.buildType = fsTemplateType
r[name] = builder
return builder.buildTemplate()
}

// AddFromFSFuncs adds a new template to the DynamicRender from the provided file system (fs.FS) and files.
// It allows you to specify a custom function map (funcMap) to be used within the template.
//
// Parameters:
// - name: The name to associate with the template in the DynamicRender.
// - funcMap: A map of functions to be used within the template.
// - fsys: The file system (fs.FS) from which to read the template files.
// - files: A variadic list of file paths to be included in the template.
//
// Returns:
// - *template.Template: The constructed template.
func (r DynamicRender) AddFromFSFuncs(
name string,
funcMap template.FuncMap,
fsys fs.FS,
files ...string,
) *template.Template {
tname := filepath.Base(files[0])
builder := &templateBuilder{
templateName: tname,
funcMap: funcMap,
fsys: fsys,
files: files,
}
builder.buildType = fsFuncTemplateType
r[name] = builder
return builder.buildTemplate()
}

// AddFromString supply add template from strings
func (r DynamicRender) AddFromString(name, templateString string) *template.Template {
builder := &templateBuilder{templateName: name, templateString: templateString, options: *NewTemplateOptions()}
Expand All @@ -130,7 +172,11 @@ func (r DynamicRender) AddFromString(name, templateString string) *template.Temp
}

// AddFromStringsFuncs supply add template from strings
func (r DynamicRender) AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template {
func (r DynamicRender) AddFromStringsFuncs(
name string,
funcMap template.FuncMap,
templateStrings ...string,
) *template.Template {
builder := &templateBuilder{
templateName: name, funcMap: funcMap,
templateStrings: templateStrings,
Expand Down
39 changes: 32 additions & 7 deletions dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func createFromStringDynamic() Renderer {

func createFromStringsWithFuncsDynamic() Renderer {
r := NewRenderer()
r.AddFromStringsFuncs("index", template.FuncMap{}, `Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`)
r.AddFromStringsFuncs(
"index",
template.FuncMap{},
`Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`,
)

return r
}
Expand Down Expand Up @@ -79,7 +83,7 @@ func TestAddFromFilesDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
Expand All @@ -93,7 +97,7 @@ func TestAddFromGlobDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is login template\n", w.Body.String())
}
Expand All @@ -107,7 +111,7 @@ func TestAddFromFSDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
Expand All @@ -121,7 +125,7 @@ func TestAddFromStringDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
Expand All @@ -135,7 +139,7 @@ func TestAddFromStringsFruncsDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
Expand All @@ -149,7 +153,7 @@ func TestAddFromFilesFruncsDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template\n", w.Body.String())
}
Expand Down Expand Up @@ -192,3 +196,24 @@ func TestAddingTemplate(t *testing.T) {
createFromTemplatesDynamic()
})
}

func createFromFSFuncsDynamic() Renderer {
r := NewRenderer()
r.AddFromFSFuncs("index", template.FuncMap{}, os.DirFS("."), "tests/base.html", "tests/article.html")

return r
}

func TestAddFromFSFuncsDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromFSFuncsDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"title": "Test Multiple Template",
})
})

w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
4 changes: 2 additions & 2 deletions example/advanced/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func loadTemplates(templatesDir string) multitemplate.Renderer {
for _, include := range includes {
layoutCopy := make([]string, len(layouts))
copy(layoutCopy, layouts)
files := append(layoutCopy, include)
r.AddFromFiles(filepath.Base(include), files...)
layoutCopy = append(layoutCopy, include)
r.AddFromFiles(filepath.Base(include), layoutCopy...)
}
return r
}
8 changes: 4 additions & 4 deletions example/multibase/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func loadTemplates(templatesDir string) multitemplate.Renderer {
for _, article := range articles {
layoutCopy := make([]string, len(articleLayouts))
copy(layoutCopy, articleLayouts)
files := append(layoutCopy, article)
r.AddFromFiles(filepath.Base(article), files...)
layoutCopy = append(layoutCopy, article)
r.AddFromFiles(filepath.Base(article), layoutCopy...)
}

adminLayouts, err := filepath.Glob(templatesDir + "/layouts/admin-base.html")
Expand All @@ -62,8 +62,8 @@ func loadTemplates(templatesDir string) multitemplate.Renderer {
for _, admin := range admins {
layoutCopy := make([]string, len(adminLayouts))
copy(layoutCopy, adminLayouts)
files := append(layoutCopy, admin)
r.AddFromFiles(filepath.Base(admin), files...)
layoutCopy = append(layoutCopy, admin)
r.AddFromFiles(filepath.Base(admin), layoutCopy...)
}
return r
}
30 changes: 15 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
module github.com/gin-contrib/multitemplate

go 1.18
go 1.21.0

require (
github.com/gin-gonic/gin v1.9.1
github.com/gin-gonic/gin v1.10.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
golang.org/x/arch v0.12.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit be6dff3

Please sign in to comment.