update nginx/decoder to read nginx-location sections (location ~ *.php {}) | update encoder | update tests

This commit is contained in:
xdrm-brackets 2018-11-12 17:07:30 +01:00
parent 02bf3d9103
commit 217cad09e0
5 changed files with 25 additions and 12 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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++
}

View File

@ -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()))
}
}

View File

@ -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