github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/utils.go (about)

     1  package generator
     2  
     3  import (
     4  	"regexp"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/mailru/activerecord/internal/pkg/arerror"
     9  )
    10  
    11  var tmplErrRx = regexp.MustCompile(TemplateName + `:(\d+):`)
    12  
    13  func getTmplErrorLine(lines []string, tmplerror string) (string, error) {
    14  	lineTmpl := tmplErrRx.FindStringSubmatch(tmplerror)
    15  	if len(lineTmpl) > 1 {
    16  		lineNum, errParse := strconv.ParseInt(lineTmpl[1], 10, 64)
    17  		if errParse != nil {
    18  			return "", arerror.ErrGeneragorGetTmplLine
    19  		} else if len(lines) == 0 {
    20  			return "", arerror.ErrGeneragorEmptyTmplLine
    21  		} else {
    22  			cntline := 3
    23  			startLine := int(lineNum) - cntline - 1
    24  			if startLine < 0 {
    25  				startLine = 0
    26  			}
    27  			stopLine := int(lineNum) + cntline
    28  			if stopLine > int(lineNum) {
    29  				stopLine = int(lineNum)
    30  			}
    31  			errorLines := lines[startLine:stopLine]
    32  			for num := range errorLines {
    33  				if num == cntline {
    34  					errorLines[num] = "-->> " + errorLines[num]
    35  				} else {
    36  					errorLines[num] = "     " + errorLines[num]
    37  				}
    38  			}
    39  			return "\n" + strings.Join(errorLines, ""), nil
    40  		}
    41  	} else {
    42  		return "", arerror.ErrGeneragorErrorLineNotFound
    43  	}
    44  }