diff --git a/internal/cnf/parser/nginx/encoder.go b/internal/cnf/parser/nginx/encoder.go index e3c17c0..0cf63cd 100644 --- a/internal/cnf/parser/nginx/encoder.go +++ b/internal/cnf/parser/nginx/encoder.go @@ -68,7 +68,6 @@ func (e *encoder) Encode(v interface{}) error { if line == nil { repr = append(repr, []byte("}\n\n")...) // Note: new line after section for readability - indent-- // 3. comments } else if line.Type == COMMENT { @@ -92,7 +91,7 @@ func (e *encoder) Encode(v interface{}) error { // push children lines in reverse order if len(line.Lines) > 0 { - for l := len(line.Lines); l > 0; l-- { + for l := len(line.Lines) - 1; l >= 0; l-- { stack = append(stack, line.Lines[l]) } } diff --git a/internal/cnf/parser/nginx/encoder_test.go b/internal/cnf/parser/nginx/encoder_test.go index 11a2cda..1590589 100644 --- a/internal/cnf/parser/nginx/encoder_test.go +++ b/internal/cnf/parser/nginx/encoder_test.go @@ -48,6 +48,14 @@ func TestDecodeEncode(t *testing.T) { {"; some comment \n", "; some comment\n"}, {"; some comment \t\n", "; some comment\n"}, {"\t; some comment {\n", "; some comment {\n"}, + + {"a{\n}\n", "a {\n}\n\n"}, + {"a{\n\n}\n", "a {\n}\n\n"}, + {"a{\n; some comment\n}\n", "a {\n\t; some comment\n}\n\n"}, + {"a{\n; some comment\n #another comment\n}\n", "a {\n\t; some comment\n\t#another comment\n}\n\n"}, + {"a{\nb{\n}\n}\n", "a {\n\tb {\n\t}\n\n}\n\n"}, + {"a{\n\tb{\n\t}\n}\n", "a {\n\tb {\n\t}\n\n}\n\n"}, + {"a{\nb{\nc{\n}\n}\n}\n", "a {\n\tb {\n\t\tc {\n\t\t}\n\n\t}\n\n}\n\n"}, } for i, test := range tests { @@ -73,7 +81,7 @@ func TestDecodeEncode(t *testing.T) { // check equality if w.String() != test.Output { - t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Output), escape(w.String())) + t.Errorf("[%d] expected '%s', got '%s'", i, (test.Output), (w.String())) } }