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 { if v == nil {
return ErrNullReceiver return ErrNullReceiver
} }
vcast, ok := v.(*nginx) vcast, ok := v.(*Model)
if !ok { if !ok {
return ErrInvalidReceiver return ErrInvalidReceiver
} }
@ -26,7 +26,7 @@ func (d *decoder) Decode(v interface{}) error {
n := -1 // line number n := -1 // line number
// regexes // 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+([^;#]+);$`) reAssign := regexp.MustCompile(`(?m)^([a-z0-9_-]+)\s+([^;#]+);$`)
reInclude := regexp.MustCompile(`(?m)^include\s+([^;#]+);$`) reInclude := regexp.MustCompile(`(?m)^include\s+([^;#]+);$`)
@ -72,7 +72,13 @@ func (d *decoder) Decode(v interface{}) error {
match := reSection.FindStringSubmatch(line) match := reSection.FindStringSubmatch(line)
if match != nil { if match != nil {
l.Type = SECTION 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) l.Lines = make([]*Line, 0)
stack = append(stack, l) stack = append(stack, l)
} }

View File

@ -34,6 +34,7 @@ func TestEachLineType(t *testing.T) {
{"section_name {\n}\n", []string{"section_name"}, SECTION}, {"section_name {\n}\n", []string{"section_name"}, SECTION},
{"sectionname { \n}\n", []string{"sectionname"}, SECTION}, {"sectionname { \n}\n", []string{"sectionname"}, SECTION},
{"sectionname {\t\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}, {"\tsectionname {\n}\n", []string{"sectionname"}, SECTION},
{" \t sectionname {\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)) decoder := NewDecoder(strings.NewReader(test.Raw))
// 2. Decode // 2. Decode
receiver := new(nginx) receiver := new(Model)
err := decoder.Decode(receiver) err := decoder.Decode(receiver)
if err != nil { if err != nil {
@ -116,7 +117,7 @@ func TestNestedSections(t *testing.T) {
decoder := NewDecoder(strings.NewReader(test.Raw)) decoder := NewDecoder(strings.NewReader(test.Raw))
// 2. Decode // 2. Decode
receiver := new(nginx) receiver := new(Model)
err := decoder.Decode(receiver) err := decoder.Decode(receiver)
if err != nil { if err != nil {

View File

@ -24,7 +24,7 @@ func (e *encoder) SetIndent(prefix, indent string) {
func (e *encoder) Encode(v interface{}) error { func (e *encoder) Encode(v interface{}) error {
// check 'v' // check 'v'
vcast, ok := v.(*nginx) vcast, ok := v.(*Model)
if !ok { if !ok {
return ErrInvalidReceiver return ErrInvalidReceiver
} }
@ -96,7 +96,12 @@ func (e *encoder) Encode(v interface{}) error {
} }
} }
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]))...) repr = append(repr, []byte(fmt.Sprintf("%s {\n", line.Components[0]))...)
}
indent++ indent++
} }

View File

@ -35,6 +35,7 @@ func TestDecodeEncode(t *testing.T) {
{"sectionname {\t\n}\n", "sectionname {\n}\n\n"}, {"sectionname {\t\n}\n", "sectionname {\n}\n\n"},
{"\tsectionname {\n}\n", "sectionname {\n}\n\n"}, {"\tsectionname {\n}\n", "sectionname {\n}\n\n"},
{" \t sectionname {\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 comment\n", "#some comment\n"},
{"#some\tcomment\n", "#some\tcomment\n"}, {"#some\tcomment\n", "#some\tcomment\n"},
@ -64,7 +65,7 @@ func TestDecodeEncode(t *testing.T) {
r, w := strings.NewReader(test.Input), &bytes.Buffer{} r, w := strings.NewReader(test.Input), &bytes.Buffer{}
// parse input // parse input
receiver := new(nginx) receiver := new(Model)
decoder := NewDecoder(r) decoder := NewDecoder(r)
if err := decoder.Decode(receiver); err != nil { if err := decoder.Decode(receiver); err != nil {
t.Errorf("[%d] unexpected error <%s>", i, err) t.Errorf("[%d] unexpected error <%s>", i, err)
@ -81,7 +82,7 @@ func TestDecodeEncode(t *testing.T) {
// check equality // check equality
if w.String() != test.Output { 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" "io"
) )
type nginx struct { type Model struct {
Lines []*Line Lines []*Line
} }
@ -20,7 +20,7 @@ func NewEncoder(w io.Writer) *encoder {
// Section returns the section with a given name at the root level // Section returns the section with a given name at the root level
// nil is returned if no match found // 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 { for _, elem := range l.Lines {
if elem.Type == SECTION && elem.Components[0] == name { if elem.Type == SECTION && elem.Components[0] == name {
return elem 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 // Get returns a pointer to the assignment line the given name as key
// at the root level // at the root level
func (l *nginx) Get(name string) *Line { func (l *Model) Get(name string) *Line {
for _, elem := range l.Lines { for _, elem := range l.Lines {
if elem.Type == ASSIGNMENT && elem.Components[0] == name { if elem.Type == ASSIGNMENT && elem.Components[0] == name {
return elem return elem