github.com/daixiang0/gci@v0.13.4/pkg/section/parser.go (about)

     1  package section
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  func Parse(data []string) (SectionList, error) {
    10  	if len(data) == 0 {
    11  		return nil, nil
    12  	}
    13  
    14  	var list SectionList
    15  	var errString string
    16  	for _, d := range data {
    17  		s := strings.ToLower(d)
    18  		if len(s) == 0 {
    19  			return nil, nil
    20  		}
    21  
    22  		if s == "default" {
    23  			list = append(list, Default{})
    24  		} else if s == "standard" {
    25  			list = append(list, Standard{})
    26  		} else if s == "newline" {
    27  			list = append(list, NewLine{})
    28  		} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {
    29  			list = append(list, Custom{d[7 : len(d)-1]})
    30  		} else if strings.HasPrefix(s, "commentline(") && len(d) > 13 {
    31  			list = append(list, Custom{d[12 : len(d)-1]})
    32  		} else if s == "dot" {
    33  			list = append(list, Dot{})
    34  		} else if s == "blank" {
    35  			list = append(list, Blank{})
    36  		} else if s == "alias" {
    37  			list = append(list, Alias{})
    38  		} else if s == "localmodule" {
    39  			// pointer because we need to mutate the section at configuration time
    40  			list = append(list, &LocalModule{})
    41  		} else {
    42  			errString += fmt.Sprintf(" %s", s)
    43  		}
    44  	}
    45  	if errString != "" {
    46  		return nil, errors.New(fmt.Sprintf("invalid params:%s", errString))
    47  	}
    48  	return list, nil
    49  }