Skip to content

Commit 284e9c9

Browse files
authored
Merge pull request #17 from gopherguides/doc.ids
assign documents ids
2 parents 32a0c39 + 7fccdeb commit 284e9c9

File tree

7 files changed

+68
-24
lines changed

7 files changed

+68
-24
lines changed

cmd/hype/cli/encode_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func Test_Encode_JSON(t *testing.T) {
6868
t.Run(tc.name, func(t *testing.T) {
6969
r := require.New(t)
7070

71+
p.DocIDGen = func() (string, error) {
72+
return tc.name, nil
73+
}
74+
7175
cmd := &Encode{
7276
Parser: p,
7377
}

cmd/hype/cli/testdata/encode/json/success/execute-file.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"id": "execute file",
23
"nodes": [
34
{
45
"file": "module.md",

cmd/hype/cli/testdata/encode/json/success/parse-file.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"id": "parse file",
23
"nodes": [
34
{
45
"file": "module.md",

document.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Document struct {
1717
fs.FS `json:"-"`
1818
sync.RWMutex `json:"-"`
1919

20+
ID string `json:"id,omitempty"`
2021
Nodes Nodes `json:"nodes,omitempty"`
2122
Parser *Parser `json:"parser,omitempty"` // Parser used to create the document
2223
Root string `json:"root,omitempty"`
@@ -31,6 +32,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
3132
}
3233

3334
x := struct {
35+
ID string `json:"id,omitempty"`
3436
Nodes Nodes `json:"nodes,omitempty"`
3537
Parser *Parser `json:"parser,omitempty"` // Parser used to create the document
3638
Root string `json:"root,omitempty"`
@@ -46,6 +48,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
4648
Snippets: doc.Snippets,
4749
Title: doc.Title,
4850
Nodes: doc.Nodes,
51+
ID: doc.ID,
4952
}
5053

5154
return json.Marshal(x)

go.mod

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ go 1.21
44

55
require (
66
github.com/gobuffalo/flect v1.0.2
7+
github.com/gofrs/uuid/v5 v5.0.0
78
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb
89
github.com/markbates/cleo v0.0.0-20230821202903-72220ef5f7f0
9-
github.com/markbates/fsx v1.2.1-0.20230825193402-1f8fe6c0541c
10-
github.com/markbates/garlic v0.0.0-20230114180117-2dc132acb41e
11-
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313
10+
github.com/markbates/fsx v1.3.0
11+
github.com/markbates/garlic v1.0.0
12+
github.com/markbates/iox v0.0.0-20230829013604-e0813da73cc6
1213
github.com/markbates/plugins v0.0.0-20230821202759-9443baa9b3df
1314
github.com/markbates/sweets v0.0.0-20210926032915-062eb9bcc0e5
1415
github.com/markbates/syncx v1.5.1
1516
github.com/markbates/table v0.0.0-20230314205021-441ed58296d1
1617
github.com/mattn/go-shellwords v1.0.12
1718
github.com/russross/blackfriday/v2 v2.1.0
18-
github.com/stretchr/testify v1.8.1
19-
golang.org/x/net v0.14.0
20-
golang.org/x/sync v0.3.0
19+
github.com/stretchr/testify v1.8.4
20+
golang.org/x/net v0.21.0
21+
golang.org/x/sync v0.6.0
2122
)
2223

2324
require (
2425
github.com/davecgh/go-spew v1.1.1 // indirect
2526
github.com/pmezard/go-difflib v1.0.0 // indirect
26-
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
27+
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect
2728
gopkg.in/yaml.v3 v3.0.1 // indirect
2829
)

go.sum

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
55
github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
6+
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
7+
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
68
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
79
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
810
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb h1:9V50gZSw997pZqgLCjhyApFsS8JA1sIZmD+3+MLJcl4=
911
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb/go.mod h1:5bD7elUD4b404zK6eOn4Q0wKJYb0mxL6yDeQZSRn9Hk=
1012
github.com/markbates/cleo v0.0.0-20230821202903-72220ef5f7f0 h1:x3wg9UbZFp4yB94Fm8MR9xKiJP0i1XeInUWFg/6/sqM=
1113
github.com/markbates/cleo v0.0.0-20230821202903-72220ef5f7f0/go.mod h1:c3X0dnyY5ctGk3G9/h9J/oSA+NX8nt9cLKkFgrWBJ8M=
12-
github.com/markbates/fsx v1.2.1-0.20230825193402-1f8fe6c0541c h1:IX3/9dWerTpiNjFiO8sUJ+MXioPRi/ON5lbGgR0UxwU=
13-
github.com/markbates/fsx v1.2.1-0.20230825193402-1f8fe6c0541c/go.mod h1:t7HbKJBmGY9+Wz+4kZI+67Utu3vYLcxKMjfUzz/eOA4=
14-
github.com/markbates/garlic v0.0.0-20230114180117-2dc132acb41e h1:Iq1xGoj427D6efggc+47p4Hk6SuJo4dh4SM6N3ZRIEo=
15-
github.com/markbates/garlic v0.0.0-20230114180117-2dc132acb41e/go.mod h1:k4nxVa4o0HPTVgFiL6IZV8JAfJP9xXqxOkbzEZWO+oQ=
16-
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313 h1:xZIGATqh50ydGM4YeYOoHNDAEnvlcERoLhgCmGqE/OA=
17-
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313/go.mod h1:q+P6BLqC21cbcmL82OdIO3A9VFCjq9fRQ+ije7T85ow=
14+
github.com/markbates/fsx v1.3.0 h1:eunGc5BKa7ELGDnsN1Fe8LBH1/ZSeDmAbqIx2DAQRtM=
15+
github.com/markbates/fsx v1.3.0/go.mod h1:t7HbKJBmGY9+Wz+4kZI+67Utu3vYLcxKMjfUzz/eOA4=
16+
github.com/markbates/garlic v1.0.0 h1:IOsPWAMbH2p/jnh2OYXTous891NSAkXQdosToJvlg+E=
17+
github.com/markbates/garlic v1.0.0/go.mod h1:k4nxVa4o0HPTVgFiL6IZV8JAfJP9xXqxOkbzEZWO+oQ=
18+
github.com/markbates/iox v0.0.0-20230829013604-e0813da73cc6 h1:z/d6wuXj40wjOVq9dnpYJVNlO7PFuBK0vz0eyzJ70lk=
19+
github.com/markbates/iox v0.0.0-20230829013604-e0813da73cc6/go.mod h1:q+P6BLqC21cbcmL82OdIO3A9VFCjq9fRQ+ije7T85ow=
1820
github.com/markbates/plugins v0.0.0-20230821202759-9443baa9b3df h1:FO5PuTGqpiILO5BUK0doGhzOF3Hyb9UZ5Ec/LjUBPPw=
1921
github.com/markbates/plugins v0.0.0-20230821202759-9443baa9b3df/go.mod h1:sW8lOXIwFRLa6PHo1sgWlVohCtYt1v+ZtgVPKM6fsVQ=
2022
github.com/markbates/sweets v0.0.0-20210926032915-062eb9bcc0e5 h1:od8GvggTptD2HUz8nGaQQe6n1zAHDbsG+82hfHPNExY=
@@ -36,14 +38,15 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
3638
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
3739
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3840
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
39-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
4041
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
41-
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
42-
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
43-
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
44-
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
45-
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
46-
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
42+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
43+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
44+
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 h1:/RIbNt/Zr7rVhIkQhooTxCxFcdWLGIKnZA4IXNFSrvo=
45+
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
46+
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
47+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
48+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
49+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
4750
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4851
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
4952
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

parser.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313
"time"
1414

15+
"github.com/gofrs/uuid/v5"
1516
"github.com/gopherguides/hype/binding"
1617
"github.com/markbates/syncx"
1718
"golang.org/x/net/html"
@@ -30,6 +31,7 @@ type Parser struct {
3031
Snippets Snippets `json:"snippets,omitempty"`
3132
Section int `json:"section,omitempty"`
3233
NowFn func() time.Time `json:"-"` // default: time.Now()
34+
DocIDGen func() (string, error) `json:"-"` // default: uuid.NewV4().String()
3335
Vars syncx.Map[string, any] `json:"vars,omitempty"`
3436

3537
fileName string
@@ -122,7 +124,11 @@ func (p *Parser) Parse(r io.Reader) (*Document, error) {
122124
return nil, p.wrapErr(err)
123125
}
124126

125-
doc := p.newDoc()
127+
doc, err := p.newDoc()
128+
if err != nil {
129+
return nil, p.wrapErr(err)
130+
}
131+
126132
doc.Nodes = Nodes{node}
127133
if len(doc.Title) == 0 {
128134
doc.Title = FindTitle(doc.Nodes)
@@ -259,7 +265,10 @@ func (p *Parser) ParseExecuteFragment(ctx context.Context, r io.Reader) (Nodes,
259265
return nil, p.wrapErr(err)
260266
}
261267

262-
doc := p.newDoc()
268+
doc, err := p.newDoc()
269+
if err != nil {
270+
return nil, p.wrapErr(err)
271+
}
263272
doc.Nodes = nodes
264273

265274
err = doc.Execute(ctx)
@@ -415,16 +424,38 @@ func NewParser(cab fs.FS) *Parser {
415424
}
416425
}
417426

418-
func (p *Parser) newDoc() *Document {
427+
func (p *Parser) newDoc() (*Document, error) {
428+
if p == nil {
429+
return nil, ErrIsNil("parser")
430+
}
431+
432+
p.mu.Lock()
433+
defer p.mu.Unlock()
434+
435+
if p.DocIDGen == nil {
436+
p.DocIDGen = func() (string, error) {
437+
id, err := uuid.NewV4()
438+
if err != nil {
439+
return "", err
440+
}
441+
return id.String(), nil
442+
}
443+
}
444+
445+
id, err := p.DocIDGen()
446+
if err != nil {
447+
return nil, p.wrapErr(err)
448+
}
419449
doc := &Document{
450+
ID: id,
420451
FS: p.FS,
421452
Parser: p,
422453
Root: p.Root,
423454
SectionID: p.Section,
424455
Snippets: p.Snippets,
425456
}
426457

427-
return doc
458+
return doc, nil
428459
}
429460

430461
func (p *Parser) wrapErr(err error) error {

0 commit comments

Comments
 (0)