Skip to content

Commit

Permalink
inline comment (#51)
Browse files Browse the repository at this point in the history
* inline comment

* rename

* update go version

* make linter happy

* add test for inline comment

(cherry picked from commit a880235)

* fix go mod

---------

Co-authored-by: Tufan Barış Yıldırım <tufanbarisyildirim@gmail.com>
Co-authored-by: chenwei <chenwei@asants.com>
  • Loading branch information
3 people authored Sep 7, 2024
1 parent 5124d2e commit d38eb71
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.23.0

- name: Verify dependencies
run: go mod verify
Expand Down
3 changes: 2 additions & 1 deletion config/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type Directive struct {
Name string
Parameters []string //TODO: Save parameters with their type
Comment []string
Parent IBlock
DefaultInlineComment
Parent IBlock
}

// SetParent the parent block
Expand Down
4 changes: 3 additions & 1 deletion config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ type HTTP struct {
Servers []*Server
Directives []IDirective
Comment []string
Parent IBlock
DefaultInlineComment
Parent IBlock
}

// SetParent change the parent block
Expand Down Expand Up @@ -48,6 +49,7 @@ func NewHTTP(directive IDirective) (*HTTP, error) {
http.Directives = append(http.Directives, directive)
}
http.Comment = directive.GetComment()
http.InlineComment = directive.GetInlineComment()

return http, nil
}
Expand Down
6 changes: 4 additions & 2 deletions config/lua_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type LuaBlock struct {
Directives []IDirective
Name string
Comment []string
LuaCode string
Parent IBlock
DefaultInlineComment
LuaCode string
Parent IBlock
}

// NewLuaBlock creates a lua block
Expand All @@ -24,6 +25,7 @@ func NewLuaBlock(directive IDirective) (*LuaBlock, error) {

lb.Directives = append(lb.Directives, block.GetDirectives()...)
lb.Comment = directive.GetComment()
lb.InlineComment = directive.GetInlineComment()

return lb, nil
}
Expand Down
8 changes: 5 additions & 3 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
type Server struct {
Block IBlock
Comment []string
Parent IBlock
DefaultInlineComment
Parent IBlock
}

// SetParent change the parent block
Expand All @@ -35,8 +36,9 @@ func (s *Server) GetComment() []string {
func NewServer(directive IDirective) (*Server, error) {
if block := directive.GetBlock(); block != nil {
return &Server{
Block: block,
Comment: directive.GetComment(),
Block: block,
Comment: directive.GetComment(),
DefaultInlineComment: DefaultInlineComment{InlineComment: directive.GetInlineComment()},
}, nil
}
return nil, errors.New("server directive must have a block")
Expand Down
22 changes: 22 additions & 0 deletions config/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ type IDirective interface {
SetComment(comment []string)
SetParent(IBlock)
GetParent() IBlock
InlineCommenter
}

// InlineCommenter represents the inline comment holder
type InlineCommenter interface {
GetInlineComment() string
SetInlineComment(comment string)
}

// DefaultInlineComment represents the default inline comment holder
type DefaultInlineComment struct {
InlineComment string
}

// GetInlineComment returns the inline comment
func (d *DefaultInlineComment) GetInlineComment() string {
return d.InlineComment
}

// SetInlineComment sets the inline comment
func (d *DefaultInlineComment) SetInlineComment(comment string) {
d.InlineComment = comment
}

// FileDirective a statement that saves its own file
Expand Down
4 changes: 3 additions & 1 deletion config/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type Upstream struct {
//Directives Other directives in upstream (ip_hash; etc)
Directives []IDirective
Comment []string
Parent IBlock
DefaultInlineComment
Parent IBlock
}

// SetParent change the parent block
Expand Down Expand Up @@ -87,6 +88,7 @@ func NewUpstream(directive IDirective) (*Upstream, error) {
}

us.Comment = directive.GetComment()
us.InlineComment = directive.GetInlineComment()

return us, nil
}
Expand Down
4 changes: 3 additions & 1 deletion config/upstream_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type UpstreamServer struct {
Flags []string
Parameters map[string]string
Comment []string
Parent IBlock
DefaultInlineComment
Parent IBlock
}

// SetParent change the parent block
Expand Down Expand Up @@ -106,6 +107,7 @@ func NewUpstreamServer(directive IDirective) (*UpstreamServer, error) {
}

uss.Comment = directive.GetComment()
uss.InlineComment = directive.GetInlineComment()

return uss, nil
}
1 change: 1 addition & 0 deletions dumper/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func DumpDirective(d config.IDirective, style *Style) string {
if d.GetName() != "" {
buf.WriteRune(';')
}
buf.WriteString(d.GetInlineComment())
} else {
buf.WriteString(" {\n")
buf.WriteString(DumpBlock(d.GetBlock(), style.Iterate()))
Expand Down
4 changes: 2 additions & 2 deletions examples/formatting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
expires 30d;
expires 30d; # inline comment: expires 30d
} location / { proxy_pass http://127.0.0.1:8080; } }
# comment: big_server_com
# comment: upstream big_server_com
Expand All @@ -61,7 +61,7 @@ server 192.168.0.1:8001;
}
# comment: server
server { # comment: listen
listen 80;
listen 80; # inline comment: listen 80
server_name big.server.com;
# comment: access_log
access_log logs/big.server.access.log main;
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/tufanbarisyildirim/gonginx

go 1.19
go 1.23

require gotest.tools/v3 v3.4.0
require gotest.tools/v3 v3.5.1

require github.com/google/go-cmp v0.5.5 // indirect
require github.com/google/go-cmp v0.6.0 // indirect
32 changes: 4 additions & 28 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
8 changes: 5 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (p *Parser) parseBlock(inBlock bool, isSkipValidDirective bool) (*config.Bl
Directives: make([]config.IDirective, 0),
}
var s config.IDirective
var err error
var line int
parsingLoop:
for {
Expand All @@ -203,7 +204,7 @@ parsingLoop:
case p.curTokenIs(token.BlockEnd):
break parsingLoop
case p.curTokenIs(token.Keyword) || p.curTokenIs(token.QuotedString):
s, err := p.parseStatement(isSkipValidDirective)
s, err = p.parseStatement(isSkipValidDirective)
if err != nil {
return nil, err
}
Expand All @@ -214,14 +215,15 @@ parsingLoop:
if p.opts.skipComments {
break
}
p.commentBuffer = append(p.commentBuffer, p.currentToken.Literal)
// inline comment
if line == p.currentToken.Line {
if s == nil && len(context.Directives) > 0 {
s = context.Directives[len(context.Directives)-1]
}
s.SetComment(p.commentBuffer)
s.SetInlineComment(p.currentToken.Literal)
p.commentBuffer = nil
} else {
p.commentBuffer = append(p.commentBuffer, p.currentToken.Literal)
}

}
Expand Down
13 changes: 13 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"os"
"testing"

"github.com/tufanbarisyildirim/gonginx/config"
Expand Down Expand Up @@ -361,6 +362,18 @@ func TestParser_Issue32(t *testing.T) {
assert.NilError(t, err, "no error expected here")
}

func TestParser_Issue50(t *testing.T) {
t.Parallel()
p, err := NewParser("../testdata/issues/50.conf")
assert.NilError(t, err, "no error expected here")
data, err := os.ReadFile("../testdata/issues/50.conf")
assert.NilError(t, err, "no error expected here")
c, err := p.Parse()
assert.NilError(t, err, "no error expected here")
content := dumper.DumpConfig(c, dumper.IndentedStyle)
assert.Equal(t, content, string(data))
}

func TestParser_SkipBlock(t *testing.T) {
t.Parallel()
conf := `user root;
Expand Down
29 changes: 29 additions & 0 deletions testdata/issues/50.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
worker_processes 1;#global inlinecomment
events {
worker_connections 1024;#event inlinecomment
}
http {
include mime.types;#http inlinecomment
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;#server inlinecomment
server_name localhost;
location / {
root /usr/share/nginx/html;#location inlinecomment
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
listen 8000;
location / {
root html;
index index.html index.htm;
}
}
}

0 comments on commit d38eb71

Please sign in to comment.