Skip to content

Commit

Permalink
TemplateCache/customs: don't fail on missing keys (#538)
Browse files Browse the repository at this point in the history
If 'customs' returns an error for missing keys on changes,
it's difficult to add a new custom key with unreleased changes pending
and a template that uses 'customs'.

Change 'customs' to yield an empty string for a missing custom key.
This matches the behavior of accessing the missing key directly
via `$change.Custom.Foo`.

Resolves #536

* Prepare release v1.13.1

Co-authored-by: Ronnie <miniscruff@hey.com>
  • Loading branch information
abhinav and miniscruff committed Sep 16, 2023
1 parent 7620559 commit e43c821
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/v1.13.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## v1.13.1 on 2023-09-16

### Fixed

* [#536](https://github.com/miniscruff/changie/issues/536) customs template function returns an empty string instead of an error for missing keys.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [itself](https://github.com/miniscruff/changie).

## v1.13.1 on 2023-09-16

### Fixed

* [#536](https://github.com/miniscruff/changie/issues/536) customs template function returns an empty string instead of an error for missing keys.

## v1.13.0 on 2023-08-19

### Added
Expand Down
9 changes: 2 additions & 7 deletions core/templatecache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"fmt"
"io"
"text/template"
"time"
Expand Down Expand Up @@ -184,18 +183,14 @@ func (tc *TemplateCache) Times(changes []Change) ([]time.Time, error) {
}

// Customs will return all the values from the custom map by a key.
// If a key is missing from a change, it will be an empty string.
// example: yaml
// format: "{{ customs .Changes \"Author\" }} authors"
func (tc *TemplateCache) Customs(changes []Change, key string) ([]string, error) {
var ok bool

values := make([]string, len(changes))

for i, c := range changes {
values[i], ok = c.Custom[key]
if !ok {
return nil, fmt.Errorf("missing custom key: '%v'", key)
}
values[i] = c.Custom[key]
}

return values, nil
Expand Down
30 changes: 16 additions & 14 deletions core/templatecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ func TestCanGetCustoms(t *testing.T) {
then.Equals(t, "[miniscruff rocktavious emmyoop] authors", builder.String())
}

func TestCanGetCustomsMissingKey(t *testing.T) {
cache := NewTemplateCache()
builder := &strings.Builder{}
data := BatchData{
Changes: []Change{
{Custom: map[string]string{"Author": "miniscruff"}},
{Custom: map[string]string{"NotAuthor": "rocktavious"}},
{Custom: map[string]string{"Author": "emmyoop"}},
},
}

err := cache.Execute("{{ customs .Changes \"Author\" | compact }} authors", builder, data)
then.Nil(t, err)
then.Equals(t, "[miniscruff emmyoop] authors", builder.String())
}

func TestErrorBadTemplate(t *testing.T) {
cache := NewTemplateCache()
_, err := cache.Load("{{...___..__}}")
Expand All @@ -169,17 +185,3 @@ func TestErrorBadExecute(t *testing.T) {
then.NotNil(t, err)
then.Equals(t, builder.Len(), 0)
}

func TestErrorMissingKey(t *testing.T) {
cache := NewTemplateCache()
builder := &strings.Builder{}
data := BatchData{
Changes: []Change{
{Custom: map[string]string{"Author": "miniscruff"}},
},
}

err := cache.Execute("{{ customs .Changes \"MissingKey\" }}", builder, data)
then.NotNil(t, err)
then.Equals(t, builder.Len(), 0)
}
2 changes: 1 addition & 1 deletion docs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ summaryLength = 30
]
enable_anchor_link = true
issues_url = 'https://github.com/miniscruff/changie/issues/new'
version = 'v1.13.0'
version = 'v1.13.1'

[params.homepage_meta_tags]
meta_description = "Changie is a file based changelog management tool."
Expand Down
2 changes: 1 addition & 1 deletion docs/static/version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"latest": "v1.13.0"
"latest": "v1.13.1"
}

0 comments on commit e43c821

Please sign in to comment.