moved Line into line.go | remove unused fields (Number, Indent)

This commit is contained in:
xdrm-brackets 2018-11-12 15:35:45 +01:00
parent 14f68cbf8e
commit 73b953c35b
3 changed files with 85 additions and 57 deletions

View File

@ -54,7 +54,7 @@ func (d *decoder) Decode(v interface{}) error {
for {
n++
l := &Line{Number: n, Type: NONE}
l := &Line{Type: NONE}
// 1. read line
notrim, err := r.ReadString('\n')
@ -71,13 +71,7 @@ func (d *decoder) Decode(v interface{}) error {
continue
}
// 3. get indentation
var firstChar int
for firstChar = 0; inArray(" \t", notrim[firstChar]); firstChar++ {
}
l.Indent = notrim[0:firstChar]
// 3. comment
// 2. comment
if line[0] == '#' {
l.Type = COMMENT
l.Components = []string{strings.Trim(line[1:], " \t")}
@ -91,7 +85,7 @@ func (d *decoder) Decode(v interface{}) error {
}
}
// 4. section
// 3. section
if l.Type == NONE {
match := reSection.FindStringSubmatch(line)
if match != nil {
@ -102,7 +96,7 @@ func (d *decoder) Decode(v interface{}) error {
}
}
// 5. include
// 4. include
if l.Type == NONE {
match := reInclude.FindStringSubmatch(line)
if match != nil {
@ -111,7 +105,7 @@ func (d *decoder) Decode(v interface{}) error {
}
}
// 6. assignment
// 5. assignment
if l.Type == NONE {
match := reAssign.FindStringSubmatch(line)
if match != nil {
@ -120,12 +114,12 @@ func (d *decoder) Decode(v interface{}) error {
}
}
// 7. invalid type
// 6. invalid type
if l.Type == NONE {
return &LineError{n, ErrInvalidSyntax}
}
// 8. pop section
// 7. pop section
if l.Type == SECTIONEND {
cur := stack[len(stack)-1] // pop
stack = stack[0 : len(stack)-1] // pop
@ -141,7 +135,7 @@ func (d *decoder) Decode(v interface{}) error {
continue
}
// 9. add to section / or receiver
// 8. add to section / or receiver
stacklen := len(stack)
if stacklen > 0 && l.Type == SECTION {
stacklen--

View File

@ -0,0 +1,55 @@
package nginx
// LineType enumerates available line types
type LineType byte
const (
// NONE is the default line type (invalid syntax)
NONE LineType = iota
// COMMENT represents a #-comment
COMMENT
// COLONCOMMENT represents a ;-comment
COLONCOMMENT
// ASSIGNMENT line
ASSIGNMENT
// INCLUDE line
INCLUDE
// SECTION start
SECTION
// SECTIONEND line '}'
SECTIONEND
)
// Line represents a meaningful line
type Line struct {
// Type of line
Type LineType
// Components of the line
Components []string
// Lines children of the current section (nil if not a section)
Lines []*Line
}
// Section returns the section with a given name *directly*
// inside the current line ; nil is returned if no match found
func (l *Line) Section(name string) *Line {
for _, elem := range l.Lines {
if elem.Type == SECTION && elem.Components[0] == name {
return elem
}
}
return nil
}
// Get returns a pointer to the assignment line the given name as key
// *directly* inside the current line ; nil is returned if no match found
func (l *Line) Get(name string) *Line {
for _, elem := range l.Lines {
if elem.Type == ASSIGNMENT && elem.Components[0] == name {
return elem
}
}
return nil
}

View File

@ -4,49 +4,6 @@ import (
"io"
)
// LineType enumerates available line types
type LineType byte
const (
// NONE is the default line type (invalid syntax)
NONE LineType = iota
// COMMENT represents a #-comment
COMMENT
// COLONCOMMENT represents a ;-comment
COLONCOMMENT
// ASSIGNMENT line
ASSIGNMENT
// INCLUDE line
INCLUDE
// SECTION start
SECTION
// SECTIONEND line '}'
SECTIONEND
)
// Line represents a meaningful line
type Line struct {
// Number of the line in the input file
Number int
// Type of line
Type LineType
// Path is the absolute dot-separated path to this line
// "" | at the root of the file
// "a.b" | inside the 'a' section inside the 'a' section
Path string
// Components of the line
Components []string
// Lines children of the current section (nil if not a section)
Lines []*Line
// Indent is the indentation characters
Indent string
}
type nginx struct {
Lines []*Line
}
@ -55,3 +12,25 @@ type nginx struct {
func (n *nginx) NewDecoder(r io.Reader) *decoder {
return &decoder{reader: r}
}
// 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 {
for _, elem := range l.Lines {
if elem.Type == SECTION && elem.Components[0] == name {
return elem
}
}
return nil
}
// Get returns a pointer to the assignment line the given name as key
// at the root level
func (l *nginx) Get(name string) *Line {
for _, elem := range l.Lines {
if elem.Type == ASSIGNMENT && elem.Components[0] == name {
return elem
}
}
return nil
}