chore: apply review comments

pull/18779/head
Pavel Zavora 2020-09-12 11:11:19 +02:00
parent 7fc590fb6f
commit 60c4984f51
3 changed files with 9 additions and 12 deletions

View File

@ -34,7 +34,7 @@ func (a annotationComment) matches(comment string) bool {
return strings.HasPrefix(strings.ToLower(comment), a.prefix)
}
func createConstantOrConcatColumn(table *CsvTable, row []string) CsvTableColumn {
func createConstantOrConcatColumn(table *CsvTable, row []string, annotationName string) CsvTableColumn {
// adds a virtual column with constant value to all data rows
// supported types of constant annotation rows are:
// 1. "#constant,datatype,label,defaultValue"
@ -72,10 +72,10 @@ func createConstantOrConcatColumn(table *CsvTable, row []string) CsvTableColumn
if col.DefaultValue == "" && col.Label != "" {
// type 2,3,5,6
col.DefaultValue = col.Label
col.Label = "#constant " + col.DataType
col.Label = annotationName + " " + col.DataType
} else if col.Label == "" {
// setup a label if no label is supplied fo focused error messages
col.Label = "#constant " + col.DataType
// setup a label if no label is supplied for focused error messages
col.Label = annotationName + " " + col.DataType
}
}
// add a virtual column to the table
@ -84,7 +84,7 @@ func createConstantOrConcatColumn(table *CsvTable, row []string) CsvTableColumn
// constantSetupTable setups the supplied CSV table from #constant annotation
func constantSetupTable(table *CsvTable, row []string) error {
col := createConstantOrConcatColumn(table, row)
col := createConstantOrConcatColumn(table, row, "#constant")
// add a virtual column to the table
table.extraColumns = append(table.extraColumns, &col)
return nil
@ -95,14 +95,11 @@ var computedReplacer *regexp.Regexp = regexp.MustCompile(`\$\{[^}]+\}`)
// concatSetupTable setups the supplied CSV table from #concat annotation
func concatSetupTable(table *CsvTable, row []string) error {
col := createConstantOrConcatColumn(table, row)
col := createConstantOrConcatColumn(table, row, "#concat")
template := col.DefaultValue
col.ComputeValue = func(row []string) string {
return computedReplacer.ReplaceAllStringFunc(template, func(text string) string {
columnLabel := text[2 : len(text)-1] // ${columnLabel}
if columnLabel == "$" {
return "$" // ${$} is a way to print $, if it would require escaping
}
if placeholderColumn := table.Column(columnLabel); placeholderColumn != nil {
return placeholderColumn.Value(row)
}
@ -118,7 +115,7 @@ func concatSetupTable(table *CsvTable, row []string) error {
for _, placeholder := range placeholders {
columnLabel := placeholder[2 : len(placeholder)-1] // ${columnLabel}
if columnLabel == "$" {
return nil // ${$} is a way to print $
return nil
}
if placeholderColumn := table.Column(columnLabel); placeholderColumn == nil {
return CsvColumnError{

View File

@ -169,7 +169,7 @@ func Test_ConcatAnnotation(t *testing.T) {
{[]string{"dateTime", "3", ""}, "_", "3", linePartTime},
{[]string{"long", "fN", "fV"}, "fN", "fV", 0},
// concat values
{[]string{"string", "fN", "${$}-${b}-${a}"}, "fN", "$-2-1", 0},
{[]string{"string", "fN", "$-${b}-${a}"}, "fN", "$-2-1", 0},
}
exampleRow := []string{"1", "2"}
for i, test := range tests {

View File

@ -93,7 +93,7 @@ func escapeString(val string) string {
//
// For example, to get a strconv-parseable float from a Spanish value '3.494.826.157,123', use format ",." .
func normalizeNumberString(value string, format string, removeFraction bool) (normalized string, truncated bool) {
if format == "" {
if len(format) == 0 {
format = ". \n\t\r_"
}
if strings.ContainsAny(value, format) {