Skip to content

Commit a3701e0

Browse files
committed
Switch to go-toml v2
We have been using `go-toml` for language files only. This commit makes it the only TOML library. It's spec compliant and very fast. A benchark building a site with 200 pages with TOML front matter: ```bash name old time/op new time/op delta SiteNew/Regular_TOML_front_matter-16 48.5ms ± 1% 47.1ms ± 1% -2.85% (p=0.029 n=4+4) name old alloc/op new alloc/op delta SiteNew/Regular_TOML_front_matter-16 16.9MB ± 0% 16.7MB ± 0% -1.56% (p=0.029 n=4+4) name old allocs/op new allocs/op delta SiteNew/Regular_TOML_front_matter-16 302k ± 0% 296k ± 0% -2.20% (p=0.029 n=4+4) ``` Note that the front matter unmarshaling is only a small part of building a site, so the above is very good. Fixes #8801
1 parent 40b6016 commit a3701e0

File tree

12 files changed

+50
-40
lines changed

12 files changed

+50
-40
lines changed

commands/commands_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestExecute(t *testing.T) {
121121
resp := Execute([]string{"new", "site", siteDir, "-e=staging"})
122122
c.Assert(resp.Err, qt.IsNil)
123123
config := readFileFrom(c, filepath.Join(siteDir, "config.toml"))
124-
c.Assert(config, qt.Contains, "baseURL = \"http://example.org/\"")
124+
c.Assert(config, qt.Contains, "baseURL = 'http://example.org/'")
125125
checkNewSiteInited(c, siteDir)
126126
})
127127
}

common/herrors/file_error_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func TestToLineNumberError(t *testing.T) {
3636
{errors.New(`template: _default/single.html:4:15: executing "_default/single.html" at <.Titles>: can't evaluate field Titles in type *hugolib.PageOutput`), 0, 4, 15},
3737
{errors.New("parse failed: template: _default/bundle-resource-meta.html:11: unexpected in operand"), 0, 11, 1},
3838
{errors.New(`failed:: template: _default/bundle-resource-meta.html:2:7: executing "main" at <.Titles>`), 0, 2, 7},
39-
{errors.New("error in front matter: Near line 32 (last key parsed 'title')"), 0, 32, 1},
4039
{errors.New(`failed to load translations: (6, 7): was expecting token =, but got "g" instead`), 0, 6, 7},
4140
} {
4241

common/herrors/line_number_extractors.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ package herrors
1616
import (
1717
"regexp"
1818
"strconv"
19+
20+
"github.com/pkg/errors"
21+
22+
"github.com/pelletier/go-toml/v2"
1923
)
2024

2125
var lineNumberExtractors = []lineNumberExtractor{
@@ -24,8 +28,7 @@ var lineNumberExtractors = []lineNumberExtractor{
2428
newLineNumberErrHandlerFromRegexp(".*:(\\d+):"),
2529

2630
// TOML parse errors
27-
newLineNumberErrHandlerFromRegexp(".*Near line (\\d+)(\\s.*)"),
28-
31+
tomlLineNumberExtractor,
2932
// YAML parse errors
3033
newLineNumberErrHandlerFromRegexp("line (\\d+):"),
3134

@@ -35,6 +38,14 @@ var lineNumberExtractors = []lineNumberExtractor{
3538

3639
type lineNumberExtractor func(e error) (int, int)
3740

41+
var tomlLineNumberExtractor = func(e error) (int, int) {
42+
e = errors.Cause(e)
43+
if terr, ok := e.(*toml.DecodeError); ok {
44+
return terr.Position()
45+
}
46+
return -1, -1
47+
}
48+
3849
func newLineNumberErrHandlerFromRegexp(expression string) lineNumberExtractor {
3950
re := regexp.MustCompile(expression)
4051
return extractLineNo(re)

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module github.com/gohugoio/hugo
22

33
require (
44
github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69
5-
github.com/BurntSushi/toml v0.3.1
65
github.com/PuerkitoBio/purell v1.1.1
76
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
87
github.com/alecthomas/chroma v0.9.2
@@ -44,7 +43,7 @@ require (
4443
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
4544
github.com/niklasfasching/go-org v1.5.0
4645
github.com/olekukonko/tablewriter v0.0.5
47-
github.com/pelletier/go-toml v1.9.3
46+
github.com/pelletier/go-toml/v2 v2.0.0-beta.3
4847
github.com/pkg/errors v0.9.1
4948
github.com/rogpeppe/go-internal v1.8.0
5049
github.com/russross/blackfriday v1.5.3-0.20200218234912-41c5fccfd6f6

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6
433433
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
434434
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
435435
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
436+
github.com/pelletier/go-toml/v2 v2.0.0-beta.3 h1:PNCTU4naEJ8mKal97P3A2qDU74QRQGlv4FXiL1XDqi4=
437+
github.com/pelletier/go-toml/v2 v2.0.0-beta.3/go.mod h1:aNseLYu/uKskg0zpr/kbr2z8yGuWtotWf/0BpGIAL2Y=
436438
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
437439
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
438440
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -490,8 +492,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
490492
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
491493
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
492494
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
493-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
494495
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
496+
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942 h1:t0lM6y/M5IiUZyvbBTcngso8SZEZICH7is9B6g/obVU=
497+
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
495498
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
496499
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
497500
github.com/tdewolff/minify/v2 v2.9.20 h1:Fut7w3T7nWfDOb/bOgyEvshQRRMt+xzi1T7spEEKXDw=

hugolib/config_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ name = "menu-theme"
287287
"pl1": "p1-en-main",
288288
},
289289
"menus": maps.Params{
290-
"main": []map[string]interface{}{
291-
{
290+
"main": []interface{}{
291+
map[string]interface{}{
292292
"name": "menu-lang-en-main",
293293
},
294294
},
295-
"theme": []map[string]interface{}{
296-
{
295+
"theme": []interface{}{
296+
map[string]interface{}{
297297
"name": "menu-lang-en-theme",
298298
},
299299
},
@@ -307,18 +307,18 @@ name = "menu-theme"
307307
"pl2": "p2-nb-theme",
308308
},
309309
"menus": maps.Params{
310-
"main": []map[string]interface{}{
311-
{
310+
"main": []interface{}{
311+
map[string]interface{}{
312312
"name": "menu-lang-nb-main",
313313
},
314314
},
315-
"theme": []map[string]interface{}{
316-
{
315+
"theme": []interface{}{
316+
map[string]interface{}{
317317
"name": "menu-lang-nb-theme",
318318
},
319319
},
320-
"top": []map[string]interface{}{
321-
{
320+
"top": []interface{}{
321+
map[string]interface{}{
322322
"name": "menu-lang-nb-top",
323323
},
324324
},
@@ -393,8 +393,8 @@ name = "menu-theme"
393393
"en": maps.Params{
394394
"languagename": "English",
395395
"menus": maps.Params{
396-
"main": []map[string]interface{}{
397-
{
396+
"main": []interface{}{
397+
map[string]interface{}{
398398
"name": "menu-theme",
399399
},
400400
},

hugolib/configdir_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ p3 = "p3params_no_production"
119119
c.Assert(cfg.GetString("params.p3"), qt.Equals, "p3params_development")
120120
c.Assert(cfg.GetString("languages.no.params.p3"), qt.Equals, "p3params_no_development")
121121

122-
c.Assert(len(cfg.Get("menus.docs").(([]map[string]interface{}))), qt.Equals, 2)
122+
c.Assert(len(cfg.Get("menus.docs").([]interface{})), qt.Equals, 2)
123123
noMenus := cfg.Get("languages.no.menus.docs")
124124
c.Assert(noMenus, qt.Not(qt.IsNil))
125-
c.Assert(len(noMenus.(([]map[string]interface{}))), qt.Equals, 1)
125+
c.Assert(len(noMenus.([]interface{})), qt.Equals, 1)
126126
}
127127

128128
func TestLoadConfigDirError(t *testing.T) {

langs/i18n/translationProvider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
"github.com/gohugoio/go-i18n/v2/i18n"
2727
"github.com/gohugoio/hugo/helpers"
28-
toml "github.com/pelletier/go-toml"
28+
toml "github.com/pelletier/go-toml/v2"
2929

3030
"github.com/gohugoio/hugo/deps"
3131
"github.com/gohugoio/hugo/hugofs"

parser/frontmatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
"github.com/gohugoio/hugo/parser/metadecoders"
2222

23-
"github.com/BurntSushi/toml"
23+
toml "github.com/pelletier/go-toml/v2"
2424

2525
yaml "gopkg.in/yaml.v2"
2626
)

parser/frontmatter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func TestInterfaceToConfig(t *testing.T) {
3131
// TOML
3232
{map[string]interface{}{}, metadecoders.TOML, nil, false},
3333
{
34-
map[string]interface{}{"title": "test 1"},
34+
map[string]interface{}{"title": "test' 1"},
3535
metadecoders.TOML,
36-
[]byte("title = \"test 1\"\n"),
36+
[]byte("title = \"test' 1\"\n"),
3737
false,
3838
},
3939

0 commit comments

Comments
 (0)