diff --git a/internal/cnf/parser/nginx/decoder.go b/internal/cnf/parser/nginx/decoder.go index 71d331e..858a167 100644 --- a/internal/cnf/parser/nginx/decoder.go +++ b/internal/cnf/parser/nginx/decoder.go @@ -16,7 +16,7 @@ func (d *decoder) Decode(v interface{}) error { if v == nil { return ErrNullReceiver } - vcast, ok := v.(*nginx) + vcast, ok := v.(*Model) if !ok { return ErrInvalidReceiver } @@ -26,7 +26,7 @@ func (d *decoder) Decode(v interface{}) error { n := -1 // line number // regexes - reSection := regexp.MustCompile(`(?m)^([a-z0-9_-]+)\s*\{$`) + reSection := regexp.MustCompile(`(?m)^([a-z0-9_-]+)\s*([^\{]*)\s*\{$`) reAssign := regexp.MustCompile(`(?m)^([a-z0-9_-]+)\s+([^;#]+);$`) reInclude := regexp.MustCompile(`(?m)^include\s+([^;#]+);$`) @@ -72,7 +72,13 @@ func (d *decoder) Decode(v interface{}) error { match := reSection.FindStringSubmatch(line) if match != nil { l.Type = SECTION - l.Components = match[1:] + l.Components = make([]string, 0) + l.Components = append(l.Components, match[1]) + + if len(match[2]) > 0 { + l.Components = append(l.Components, strings.Trim(match[2], " \t")) + } + l.Lines = make([]*Line, 0) stack = append(stack, l) } diff --git a/internal/cnf/parser/nginx/decoder_test.go b/internal/cnf/parser/nginx/decoder_test.go index 4c5789a..be2b8bc 100644 --- a/internal/cnf/parser/nginx/decoder_test.go +++ b/internal/cnf/parser/nginx/decoder_test.go @@ -34,6 +34,7 @@ func TestEachLineType(t *testing.T) { {"section_name {\n}\n", []string{"section_name"}, SECTION}, {"sectionname { \n}\n", []string{"sectionname"}, SECTION}, {"sectionname {\t\n}\n", []string{"sectionname"}, SECTION}, + {"sectionname ~ with-args {\t\n}\n", []string{"sectionname", "~ with-args"}, SECTION}, {"\tsectionname {\n}\n", []string{"sectionname"}, SECTION}, {" \t sectionname {\n}\n", []string{"sectionname"}, SECTION}, @@ -57,7 +58,7 @@ func TestEachLineType(t *testing.T) { decoder := NewDecoder(strings.NewReader(test.Raw)) // 2. Decode - receiver := new(nginx) + receiver := new(Model) err := decoder.Decode(receiver) if err != nil { @@ -116,7 +117,7 @@ func TestNestedSections(t *testing.T) { decoder := NewDecoder(strings.NewReader(test.Raw)) // 2. Decode - receiver := new(nginx) + receiver := new(Model) err := decoder.Decode(receiver) if err != nil { diff --git a/internal/cnf/parser/nginx/encoder.go b/internal/cnf/parser/nginx/encoder.go index 0cf63cd..d65b759 100644 --- a/internal/cnf/parser/nginx/encoder.go +++ b/internal/cnf/parser/nginx/encoder.go @@ -24,7 +24,7 @@ func (e *encoder) SetIndent(prefix, indent string) { func (e *encoder) Encode(v interface{}) error { // check 'v' - vcast, ok := v.(*nginx) + vcast, ok := v.(*Model) if !ok { return ErrInvalidReceiver } @@ -96,7 +96,12 @@ func (e *encoder) Encode(v interface{}) error { } } - repr = append(repr, []byte(fmt.Sprintf("%s {\n", line.Components[0]))...) + if len(line.Components) > 1 { + repr = append(repr, []byte(fmt.Sprintf("%s %s {\n", line.Components[0], line.Components[1]))...) + } else { + repr = append(repr, []byte(fmt.Sprintf("%s {\n", line.Components[0]))...) + } + indent++ } diff --git a/internal/cnf/parser/nginx/encoder_test.go b/internal/cnf/parser/nginx/encoder_test.go index 1590589..ac11ade 100644 --- a/internal/cnf/parser/nginx/encoder_test.go +++ b/internal/cnf/parser/nginx/encoder_test.go @@ -35,6 +35,7 @@ func TestDecodeEncode(t *testing.T) { {"sectionname {\t\n}\n", "sectionname {\n}\n\n"}, {"\tsectionname {\n}\n", "sectionname {\n}\n\n"}, {" \t sectionname {\n}\n", "sectionname {\n}\n\n"}, + {"sectionname ~ with-args {\t\n}\n", "sectionname ~ with-args {\n}\n\n"}, {"#some comment\n", "#some comment\n"}, {"#some\tcomment\n", "#some\tcomment\n"}, @@ -64,7 +65,7 @@ func TestDecodeEncode(t *testing.T) { r, w := strings.NewReader(test.Input), &bytes.Buffer{} // parse input - receiver := new(nginx) + receiver := new(Model) decoder := NewDecoder(r) if err := decoder.Decode(receiver); err != nil { t.Errorf("[%d] unexpected error <%s>", i, err) @@ -81,7 +82,7 @@ func TestDecodeEncode(t *testing.T) { // check equality if w.String() != test.Output { - t.Errorf("[%d] expected '%s', got '%s'", i, (test.Output), (w.String())) + t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Output), escape(w.String())) } } diff --git a/internal/cnf/parser/nginx/nginx.go b/internal/cnf/parser/nginx/nginx.go index 68127a4..d76b020 100644 --- a/internal/cnf/parser/nginx/nginx.go +++ b/internal/cnf/parser/nginx/nginx.go @@ -4,7 +4,7 @@ import ( "io" ) -type nginx struct { +type Model struct { Lines []*Line } @@ -20,7 +20,7 @@ func NewEncoder(w io.Writer) *encoder { // Section returns the section with a given name at the root level // nil is returned if no match found -func (l *nginx) Section(name string) *Line { +func (l *Model) Section(name string) *Line { for _, elem := range l.Lines { if elem.Type == SECTION && elem.Components[0] == name { return elem @@ -31,7 +31,7 @@ func (l *nginx) Section(name string) *Line { // Get returns a pointer to the assignment line the given name as key // at the root level -func (l *nginx) Get(name string) *Line { +func (l *Model) Get(name string) *Line { for _, elem := range l.Lines { if elem.Type == ASSIGNMENT && elem.Components[0] == name { return elem