Skip to content

Commit

Permalink
#54 Keep parameters in their relative lines when in multi-line config…
Browse files Browse the repository at this point in the history
…uration. (#61)

* 1. Migrate all directive parameters to the Parameter object
2. The Parameter object stores the relative line index to the directive, allowing the dump to retain the original layout
3. During parsing, obtain and store the relative line index of parameters to the directive
4. During dump, restore the original relative line index

* fix: resolve linter warnings in statement.go
  • Loading branch information
mofantor authored Dec 20, 2024
1 parent 78982de commit a841740
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 91 deletions.
12 changes: 6 additions & 6 deletions config/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBlock_FindDirectives(t *testing.T) {
Directives: []IDirective{
&Directive{
Name: "server_name",
Parameters: []string{"gonginx.dev"},
Parameters: []Parameter{{Value: "gonginx.dev"}},
},
},
},
Expand All @@ -40,7 +40,7 @@ func TestBlock_FindDirectives(t *testing.T) {
Directives: []IDirective{
&Directive{
Name: "server_name",
Parameters: []string{"gonginx2.dev"},
Parameters: []Parameter{{Value: "gonginx2.dev"}},
},
},
},
Expand All @@ -52,7 +52,7 @@ func TestBlock_FindDirectives(t *testing.T) {
Directives: []IDirective{
&Directive{
Name: "server_name",
Parameters: []string{"gonginx3.dev"},
Parameters: []Parameter{{Value: "gonginx3.dev"}},
},
},
},
Expand All @@ -67,7 +67,7 @@ func TestBlock_FindDirectives(t *testing.T) {
Directives: []IDirective{
&Directive{
Name: "server_name",
Parameters: []string{"gonginx.dev"},
Parameters: []Parameter{{Value: "gonginx.dev"}},
},
},
},
Expand All @@ -77,7 +77,7 @@ func TestBlock_FindDirectives(t *testing.T) {
Directives: []IDirective{
&Directive{
Name: "server_name",
Parameters: []string{"gonginx2.dev"},
Parameters: []Parameter{{Value: "gonginx2.dev"}},
},
},
},
Expand All @@ -87,7 +87,7 @@ func TestBlock_FindDirectives(t *testing.T) {
Directives: []IDirective{
&Directive{
Name: "server_name",
Parameters: []string{"gonginx3.dev"},
Parameters: []Parameter{{Value: "gonginx3.dev"}},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions config/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package config
type Directive struct {
Block IBlock
Name string
Parameters []string //TODO: Save parameters with their type
Parameters []Parameter //TODO: Save parameters with their type
Comment []string
DefaultInlineComment
Parent IBlock
Expand Down Expand Up @@ -42,7 +42,7 @@ func (d *Directive) GetName() string {
}

// GetParameters get all parameters of a directive
func (d *Directive) GetParameters() []string {
func (d *Directive) GetParameters() []Parameter {
return d.Parameters
}

Expand Down
4 changes: 2 additions & 2 deletions config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (h *HTTP) GetName() string { //the directive name.
}

// GetParameters get directive parameters if any
func (h *HTTP) GetParameters() []string {
return []string{}
func (h *HTTP) GetParameters() []Parameter {
return []Parameter{}
}

// GetDirectives get all directives in http
Expand Down
2 changes: 1 addition & 1 deletion config/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewInclude(dir IDirective) (*Include, error) {
}
include := &Include{
Directive: directive,
IncludePath: directive.Parameters[0],
IncludePath: directive.Parameters[0].GetValue(),
}

if len(directive.Parameters) > 1 {
Expand Down
6 changes: 3 additions & 3 deletions config/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func NewLocation(directive IDirective) (*Location, error) {
Directive: dir,
}
if len(dir.Parameters) == 1 {
location.Match = dir.Parameters[0]
location.Match = dir.Parameters[0].GetValue()
return location, nil
} else if len(dir.Parameters) == 2 {
location.Modifier = dir.Parameters[0]
location.Match = dir.Parameters[1]
location.Modifier = dir.Parameters[0].GetValue()
location.Match = dir.Parameters[1].GetValue()
return location, nil
}
return nil, errors.New("too many arguments for location directive")
Expand Down
4 changes: 2 additions & 2 deletions config/lua_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (lb *LuaBlock) GetName() string { //the directive name.
}

// GetParameters get directive parameters if any
func (lb *LuaBlock) GetParameters() []string {
return []string{}
func (lb *LuaBlock) GetParameters() []Parameter {
return []Parameter{}
}

// GetDirectives get all directives in lua block
Expand Down
4 changes: 2 additions & 2 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (s *Server) GetName() string { //the directive name.
}

// GetParameters get directive parameters if any
func (s *Server) GetParameters() []string {
return []string{}
func (s *Server) GetParameters() []Parameter {
return []Parameter{}
}

// GetBlock get block if any
Expand Down
33 changes: 32 additions & 1 deletion config/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type IBlock interface {
// IDirective represents any directive
type IDirective interface {
GetName() string //the directive name.
GetParameters() []string
GetParameters() []Parameter
GetBlock() IBlock
GetComment() []string
SetComment(comment []string)
Expand Down Expand Up @@ -53,3 +53,34 @@ type FileDirective interface {
type IncludeDirective interface {
FileDirective
}

// Parameter represents a parameter in a directive
type Parameter struct {
Value string
RelativeLineIndex int // relative line index to the directive
}

// String returns the value of the parameter
func (p *Parameter) String() string {
return p.Value
}

// SetValue sets the value of the parameter
func (p *Parameter) SetValue(v string) {
p.Value = v
}

// GetValue returns the value of the parameter
func (p *Parameter) GetValue() string {
return p.Value
}

// SetRelativeLineIndex sets the relative line index of the parameter
func (p *Parameter) SetRelativeLineIndex(i int) {
p.RelativeLineIndex = i
}

// GetRelativeLineIndex returns the relative line index of the parameter
func (p *Parameter) GetRelativeLineIndex() int {
return p.RelativeLineIndex
}
6 changes: 3 additions & 3 deletions config/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (us *Upstream) GetName() string {
}

// GetParameters upsrema parameters
func (us *Upstream) GetParameters() []string {
return []string{us.UpstreamName} //the only parameter for an upstream is its name
func (us *Upstream) GetParameters() []Parameter {
return []Parameter{{Value: us.UpstreamName}} //the only parameter for an upstream is its name
}

// GetBlock upstream does not have block
Expand Down Expand Up @@ -76,7 +76,7 @@ func (us *Upstream) GetDirectives() []IDirective {
func NewUpstream(directive IDirective) (*Upstream, error) {
parameters := directive.GetParameters()
us := &Upstream{
UpstreamName: parameters[0], //first parameter of the directive is the upstream name
UpstreamName: parameters[0].GetValue(), //first parameter of the directive is the upstream name
}

if directive.GetBlock() == nil {
Expand Down
20 changes: 11 additions & 9 deletions config/upstream_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (uss *UpstreamServer) GetBlock() IBlock {
}

// GetParameters block of an upstream, basically nil
func (uss *UpstreamServer) GetParameters() []string {
func (uss *UpstreamServer) GetParameters() []Parameter {
return uss.GetDirective().Parameters
}

Expand All @@ -67,12 +67,12 @@ func (uss *UpstreamServer) GetDirective() *Directive {
//First, generate a new directive from upstream server
directive := &Directive{
Name: "server",
Parameters: make([]string, 0),
Parameters: make([]Parameter, 0),
Block: nil,
}

//address it the first parameter of an upstream directive
directive.Parameters = append(directive.Parameters, uss.Address)
directive.Parameters = append(directive.Parameters, Parameter{Value: uss.Address})

//Iterations are random in golang maps https://blog.golang.org/maps#TOC_7.
//we sort keys in different slice and print them sorted.
Expand All @@ -85,11 +85,13 @@ func (uss *UpstreamServer) GetDirective() *Directive {

//append named parameters first
for _, k := range paramNames {
directive.Parameters = append(directive.Parameters, fmt.Sprintf("%s=%s", k, uss.Parameters[k]))
directive.Parameters = append(directive.Parameters, Parameter{Value: fmt.Sprintf("%s=%s", k, uss.Parameters[k])})
}

//append flags to the end of the directive.
directive.Parameters = append(directive.Parameters, uss.Flags...)
for _, flag := range uss.Flags {
directive.Parameters = append(directive.Parameters, Parameter{Value: flag})
}

directive.Comment = uss.GetComment()

Expand All @@ -106,14 +108,14 @@ func NewUpstreamServer(directive IDirective) (*UpstreamServer, error) {

for i, parameter := range directive.GetParameters() {
if i == 0 { // alright, we asuume that firstone should be a server address
uss.Address = parameter
uss.Address = parameter.GetValue()
continue
}
if strings.Contains(parameter, "=") { //a parameter like weight=5
s := strings.SplitN(parameter, "=", 2)
if strings.Contains(parameter.GetValue(), "=") { //a parameter like weight=5
s := strings.SplitN(parameter.GetValue(), "=", 2)
uss.Parameters[s[0]] = s[1]
} else {
uss.Flags = append(uss.Flags, parameter)
uss.Flags = append(uss.Flags, parameter.GetValue())
}
}

Expand Down
30 changes: 19 additions & 11 deletions dumper/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ func TestBlock_ToString(t *testing.T) {
fields: fields{
Directives: []config.IDirective{
&config.Directive{
Name: "user",
Parameters: []string{"nginx", "nginx"},
Name: "user",
Parameters: []config.Parameter{
{Value: "nginx"},
{Value: "nginx"},
},
},
&config.Directive{
Name: "worker_processes",
Parameters: []string{"5"},
Parameters: []config.Parameter{{Value: "5"}},
},
},
},
Expand All @@ -52,35 +55,40 @@ func TestBlock_ToString(t *testing.T) {
fields: fields{
Directives: []config.IDirective{
&config.Directive{
Name: "user",
Parameters: []string{"nginx", "nginx"},
Name: "user",
Parameters: []config.Parameter{
{Value: "nginx"},
{Value: "nginx"}},
},
&config.Directive{
Name: "worker_processes",
Parameters: []string{"5"},
Parameters: []config.Parameter{{Value: "5"}},
},
&config.Include{
Directive: &config.Directive{
Name: "include",
Parameters: []string{"/etc/nginx/conf/*.conf"},
Parameters: []config.Parameter{{Value: "/etc/nginx/conf/*.conf"}},
},
IncludePath: "/etc/nginx/conf/*.conf",
},
NewServerOrNill(&config.Directive{
Block: &config.Block{
Directives: []config.IDirective{
&config.Directive{
Name: "user",
Parameters: []string{"nginx", "nginx"},
Name: "user",
Parameters: []config.Parameter{
{Value: "nginx"},
{Value: "nginx"},
},
},
&config.Directive{
Name: "worker_processes",
Parameters: []string{"5"},
Parameters: []config.Parameter{{Value: "5"}},
},
&config.Include{
Directive: &config.Directive{
Name: "include",
Parameters: []string{"/etc/nginx/conf/*.conf"},
Parameters: []config.Parameter{{Value: "/etc/nginx/conf/*.conf"}},
},
IncludePath: "/etc/nginx/conf/*.conf",
},
Expand Down
11 changes: 7 additions & 4 deletions dumper/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ func TestConfig_ToString(t *testing.T) {
Block: &config.Block{
Directives: []config.IDirective{
&config.Directive{
Name: "user",
Parameters: []string{"nginx", "nginx"},
Name: "user",
Parameters: []config.Parameter{
{Value: "nginx"},
{Value: "nginx"},
},
},
&config.Directive{
Name: "worker_processes",
Parameters: []string{"5"},
Parameters: []config.Parameter{{Value: "5"}},
},
&config.Include{
Directive: &config.Directive{
Name: "include",
Parameters: []string{"/etc/nginx/conf/*.conf"},
Parameters: []config.Parameter{{Value: "/etc/nginx/conf/*.conf"}},
},
IncludePath: "/etc/nginx/conf/*.conf",
},
Expand Down
Loading

0 comments on commit a841740

Please # to comment.